From 2d838d729cca26a718e3536e8d75c60396420dbd Mon Sep 17 00:00:00 2001 From: Michael Davis Date: Mon, 10 Jul 2023 19:06:13 -0500 Subject: 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`. --- helix-view/src/register.rs | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) (limited to 'helix-view') diff --git a/helix-view/src/register.rs b/helix-view/src/register.rs index e82051dd..61912378 100644 --- a/helix-view/src/register.rs +++ b/helix-view/src/register.rs @@ -23,6 +23,10 @@ use crate::{ /// * Primary clipboard (`+`) #[derive(Debug)] 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>, clipboard_provider: Box, } @@ -77,11 +81,11 @@ impl Registers { _ => self .inner .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) -> Result<()> { + pub fn write(&mut self, name: char, mut values: Vec) -> Result<()> { match name { '_' => Ok(()), '#' | '.' | '%' => Err(anyhow::anyhow!("Register {name} does not support writing")), @@ -94,17 +98,19 @@ impl Registers { _ => unreachable!(), }, )?; + values.reverse(); self.inner.insert(name, values); Ok(()) } _ => { + values.reverse(); self.inner.insert(name, values); Ok(()) } } } - pub fn push(&mut self, name: char, value: String) -> Result<()> { + pub fn push(&mut self, name: char, mut value: String) -> Result<()> { match name { '_' => Ok(()), '#' | '.' | '%' => 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"); } - saved_values.push(value); - self.clipboard_provider.set_contents( - saved_values.join(NATIVE_LINE_ENDING.as_str()), - clipboard_type, - )?; + saved_values.push(value.clone()); + if !contents.is_empty() { + value.push_str(NATIVE_LINE_ENDING.as_str()); + } + value.push_str(&contents); + self.clipboard_provider + .set_contents(value, clipboard_type)?; Ok(()) } @@ -150,7 +158,7 @@ impl Registers { .filter(|(name, _)| !matches!(name, '*' | '+')) .map(|(name, values)| { let preview = values - .first() + .last() .and_then(|s| s.lines().next()) .unwrap_or(""); @@ -222,7 +230,7 @@ fn read_from_clipboard<'a>( let Some(values) = saved_values else { return RegisterValues::new(iter::once(contents.into())) }; if contents_are_saved(values, &contents) { - RegisterValues::new(values.iter().map(Cow::from)) + RegisterValues::new(values.iter().map(Cow::from).rev()) } else { 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 { let line_ending = NATIVE_LINE_ENDING.as_str(); - let mut values = saved_values.iter(); + let mut values = saved_values.iter().rev(); match values.next() { Some(first) if contents.starts_with(first) => { -- cgit v1.2.3-70-g09d2