mirror of https://github.com/helix-editor/helix
Preview the latest value for regular registers
This fixes a discrepancy between regular registers which are used for yanking multiple values (for example via `"ay`) and regular registers that store a history of values (for example `"a*`). Previously, the preview shown in `select_register`'s infobox would show the oldest value in history. It's intuitive and useful to see the most recent value pushed to the history though. We cannot simply switch the preview line from `values.first()` to `values.last()`: that would fix the preview for registers used for history but break the preview for registers used to yank multiple values. We could push to the beginning of the values with `Registers::push` but this is wasteful from a performance perspective. Instead we can have `Registers::read` return an iterator that returns elements in the reverse order and reverse the values in `Register::write`. This effectively means that `push` adds elements to the beginning of the register's values. For the sake of the preview, we can switch to `values.last()` and that is then correct for both usage- styles. This also needs a change to call-sites that read the latest history value to switch from `last` to `first`.pull/7793/head
parent
86a1f0177c
commit
2d838d729c
|
@ -1911,7 +1911,7 @@ fn search_next_or_prev_impl(cx: &mut Context, movement: Movement, direction: Dir
|
||||||
let register = cx.register.unwrap_or('/');
|
let register = cx.register.unwrap_or('/');
|
||||||
let config = cx.editor.config();
|
let config = cx.editor.config();
|
||||||
let scrolloff = config.scrolloff;
|
let scrolloff = config.scrolloff;
|
||||||
if let Some(query) = cx.editor.registers.last(register, cx.editor) {
|
if let Some(query) = cx.editor.registers.first(register, cx.editor) {
|
||||||
let doc = doc!(cx.editor);
|
let doc = doc!(cx.editor);
|
||||||
let contents = doc.text().slice(..).to_string();
|
let contents = doc.text().slice(..).to_string();
|
||||||
let search_config = &config.search;
|
let search_config = &config.search;
|
||||||
|
@ -1983,7 +1983,7 @@ fn search_selection(cx: &mut Context) {
|
||||||
|
|
||||||
fn make_search_word_bounded(cx: &mut Context) {
|
fn make_search_word_bounded(cx: &mut Context) {
|
||||||
let register = cx.register.unwrap_or('/');
|
let register = cx.register.unwrap_or('/');
|
||||||
let regex = match cx.editor.registers.last(register, cx.editor) {
|
let regex = match cx.editor.registers.first(register, cx.editor) {
|
||||||
Some(regex) => regex,
|
Some(regex) => regex,
|
||||||
None => return,
|
None => return,
|
||||||
};
|
};
|
||||||
|
|
|
@ -307,7 +307,7 @@ impl Prompt {
|
||||||
) {
|
) {
|
||||||
(self.callback_fn)(cx, &self.line, PromptEvent::Abort);
|
(self.callback_fn)(cx, &self.line, PromptEvent::Abort);
|
||||||
let mut values = match cx.editor.registers.read(register, cx.editor) {
|
let mut values = match cx.editor.registers.read(register, cx.editor) {
|
||||||
Some(values) if values.len() > 0 => values,
|
Some(values) if values.len() > 0 => values.rev(),
|
||||||
_ => return,
|
_ => return,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -473,7 +473,7 @@ impl Prompt {
|
||||||
// Show the most recently entered value as a suggestion.
|
// Show the most recently entered value as a suggestion.
|
||||||
if let Some(suggestion) = self
|
if let Some(suggestion) = self
|
||||||
.history_register
|
.history_register
|
||||||
.and_then(|reg| cx.editor.registers.last(reg, cx.editor))
|
.and_then(|reg| cx.editor.registers.first(reg, cx.editor))
|
||||||
{
|
{
|
||||||
surface.set_string(line_area.x, line_area.y, suggestion, suggestion_color);
|
surface.set_string(line_area.x, line_area.y, suggestion, suggestion_color);
|
||||||
}
|
}
|
||||||
|
@ -570,7 +570,7 @@ impl Component for Prompt {
|
||||||
} else {
|
} else {
|
||||||
let last_item = self
|
let last_item = self
|
||||||
.history_register
|
.history_register
|
||||||
.and_then(|reg| cx.editor.registers.last(reg, cx.editor))
|
.and_then(|reg| cx.editor.registers.first(reg, cx.editor))
|
||||||
.map(|entry| entry.to_string())
|
.map(|entry| entry.to_string())
|
||||||
.unwrap_or_else(|| String::from(""));
|
.unwrap_or_else(|| String::from(""));
|
||||||
|
|
||||||
|
|
|
@ -23,6 +23,10 @@ use crate::{
|
||||||
/// * Primary clipboard (`+`)
|
/// * Primary clipboard (`+`)
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Registers {
|
pub struct Registers {
|
||||||
|
/// The mapping of register to values.
|
||||||
|
/// Values are stored in reverse order when inserted with `Registers::write`.
|
||||||
|
/// The order is reversed again in `Registers::read`. This allows us to
|
||||||
|
/// efficiently prepend new values in `Registers::push`.
|
||||||
inner: HashMap<char, Vec<String>>,
|
inner: HashMap<char, Vec<String>>,
|
||||||
clipboard_provider: Box<dyn ClipboardProvider>,
|
clipboard_provider: Box<dyn ClipboardProvider>,
|
||||||
}
|
}
|
||||||
|
@ -77,11 +81,11 @@ impl Registers {
|
||||||
_ => self
|
_ => self
|
||||||
.inner
|
.inner
|
||||||
.get(&name)
|
.get(&name)
|
||||||
.map(|values| RegisterValues::new(values.iter().map(Cow::from))),
|
.map(|values| RegisterValues::new(values.iter().map(Cow::from).rev())),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn write(&mut self, name: char, values: Vec<String>) -> Result<()> {
|
pub fn write(&mut self, name: char, mut values: Vec<String>) -> Result<()> {
|
||||||
match name {
|
match name {
|
||||||
'_' => Ok(()),
|
'_' => Ok(()),
|
||||||
'#' | '.' | '%' => Err(anyhow::anyhow!("Register {name} does not support writing")),
|
'#' | '.' | '%' => Err(anyhow::anyhow!("Register {name} does not support writing")),
|
||||||
|
@ -94,17 +98,19 @@ impl Registers {
|
||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
},
|
},
|
||||||
)?;
|
)?;
|
||||||
|
values.reverse();
|
||||||
self.inner.insert(name, values);
|
self.inner.insert(name, values);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
|
values.reverse();
|
||||||
self.inner.insert(name, values);
|
self.inner.insert(name, values);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn push(&mut self, name: char, value: String) -> Result<()> {
|
pub fn push(&mut self, name: char, mut value: String) -> Result<()> {
|
||||||
match name {
|
match name {
|
||||||
'_' => Ok(()),
|
'_' => Ok(()),
|
||||||
'#' | '.' | '%' => Err(anyhow::anyhow!("Register {name} does not support pushing")),
|
'#' | '.' | '%' => Err(anyhow::anyhow!("Register {name} does not support pushing")),
|
||||||
|
@ -121,11 +127,13 @@ impl Registers {
|
||||||
anyhow::bail!("Failed to push to register {name}: clipboard does not match register contents");
|
anyhow::bail!("Failed to push to register {name}: clipboard does not match register contents");
|
||||||
}
|
}
|
||||||
|
|
||||||
saved_values.push(value);
|
saved_values.push(value.clone());
|
||||||
self.clipboard_provider.set_contents(
|
if !contents.is_empty() {
|
||||||
saved_values.join(NATIVE_LINE_ENDING.as_str()),
|
value.push_str(NATIVE_LINE_ENDING.as_str());
|
||||||
clipboard_type,
|
}
|
||||||
)?;
|
value.push_str(&contents);
|
||||||
|
self.clipboard_provider
|
||||||
|
.set_contents(value, clipboard_type)?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -150,7 +158,7 @@ impl Registers {
|
||||||
.filter(|(name, _)| !matches!(name, '*' | '+'))
|
.filter(|(name, _)| !matches!(name, '*' | '+'))
|
||||||
.map(|(name, values)| {
|
.map(|(name, values)| {
|
||||||
let preview = values
|
let preview = values
|
||||||
.first()
|
.last()
|
||||||
.and_then(|s| s.lines().next())
|
.and_then(|s| s.lines().next())
|
||||||
.unwrap_or("<empty>");
|
.unwrap_or("<empty>");
|
||||||
|
|
||||||
|
@ -222,7 +230,7 @@ fn read_from_clipboard<'a>(
|
||||||
let Some(values) = saved_values else { return RegisterValues::new(iter::once(contents.into())) };
|
let Some(values) = saved_values else { return RegisterValues::new(iter::once(contents.into())) };
|
||||||
|
|
||||||
if contents_are_saved(values, &contents) {
|
if contents_are_saved(values, &contents) {
|
||||||
RegisterValues::new(values.iter().map(Cow::from))
|
RegisterValues::new(values.iter().map(Cow::from).rev())
|
||||||
} else {
|
} else {
|
||||||
RegisterValues::new(iter::once(contents.into()))
|
RegisterValues::new(iter::once(contents.into()))
|
||||||
}
|
}
|
||||||
|
@ -243,7 +251,7 @@ fn read_from_clipboard<'a>(
|
||||||
|
|
||||||
fn contents_are_saved(saved_values: &[String], mut contents: &str) -> bool {
|
fn contents_are_saved(saved_values: &[String], mut contents: &str) -> bool {
|
||||||
let line_ending = NATIVE_LINE_ENDING.as_str();
|
let line_ending = NATIVE_LINE_ENDING.as_str();
|
||||||
let mut values = saved_values.iter();
|
let mut values = saved_values.iter().rev();
|
||||||
|
|
||||||
match values.next() {
|
match values.next() {
|
||||||
Some(first) if contents.starts_with(first) => {
|
Some(first) if contents.starts_with(first) => {
|
||||||
|
|
Loading…
Reference in New Issue