Skip to content

Commit

Permalink
Preview the latest value for regular registers
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
the-mikedavis authored and archseer committed Jul 31, 2023
1 parent 86a1f01 commit 2d838d7
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 16 deletions.
4 changes: 2 additions & 2 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1911,7 +1911,7 @@ fn search_next_or_prev_impl(cx: &mut Context, movement: Movement, direction: Dir
let register = cx.register.unwrap_or('/');
let config = cx.editor.config();
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 contents = doc.text().slice(..).to_string();
let search_config = &config.search;
Expand Down Expand Up @@ -1983,7 +1983,7 @@ fn search_selection(cx: &mut Context) {

fn make_search_word_bounded(cx: &mut Context) {
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,
None => return,
};
Expand Down
6 changes: 3 additions & 3 deletions helix-term/src/ui/prompt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ impl Prompt {
) {
(self.callback_fn)(cx, &self.line, PromptEvent::Abort);
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,
};

Expand Down Expand Up @@ -473,7 +473,7 @@ impl Prompt {
// Show the most recently entered value as a suggestion.
if let Some(suggestion) = self
.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);
}
Expand Down Expand Up @@ -570,7 +570,7 @@ impl Component for Prompt {
} else {
let last_item = self
.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())
.unwrap_or_else(|| String::from(""));

Expand Down
30 changes: 19 additions & 11 deletions helix-view/src/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<char, Vec<String>>,
clipboard_provider: Box<dyn ClipboardProvider>,
}
Expand Down Expand Up @@ -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<String>) -> Result<()> {
pub fn write(&mut self, name: char, mut values: Vec<String>) -> Result<()> {
match name {
'_' => Ok(()),
'#' | '.' | '%' => Err(anyhow::anyhow!("Register {name} does not support writing")),
Expand All @@ -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")),
Expand All @@ -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(())
}
Expand All @@ -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("<empty>");

Expand Down Expand Up @@ -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()))
}
Expand All @@ -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) => {
Expand Down

0 comments on commit 2d838d7

Please sign in to comment.