Skip to content

Commit

Permalink
global_search: Save only the primary query to the history register (h…
Browse files Browse the repository at this point in the history
…elix-editor#11216)

Two changes from the parent commit:

* Save only the `Picker::primary_query` - so you don't save other parts
  of the query, for example `%path foo.rs` while in `global_search`.
* Move the saving out of the `if let Some(option) = self.selection()`
  block. So when you hit enter you save to history whether you have a
  selection or not. If you want to close the picker without saving to
  the register you can use C-c or Esc instead.
  • Loading branch information
the-mikedavis authored and mxxntype committed Aug 14, 2024
1 parent bb3a634 commit 592a41d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
10 changes: 9 additions & 1 deletion helix-term/src/ui/picker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1031,9 +1031,17 @@ impl<I: 'static + Send + Sync, D: 'static + Send + Sync> Component for Picker<I,
self.handle_prompt_change();
} else {
if let Some(option) = self.selection() {
self.prompt.save_line_to_history(ctx.editor);
(self.callback_fn)(ctx, option, Action::Replace);
}
if let Some(history_register) = self.prompt.history_register() {
if let Err(err) = ctx
.editor
.registers
.push(history_register, self.primary_query().to_string())
{
ctx.editor.set_error(err.to_string());
}
}
return close_fn(self);
}
}
Expand Down
23 changes: 12 additions & 11 deletions helix-term/src/ui/prompt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ impl Prompt {
self
}

pub(crate) fn history_register(&self) -> Option<char> {
self.history_register
}

pub(crate) fn first_history_completion<'a>(
&'a self,
editor: &'a Editor,
Expand Down Expand Up @@ -516,16 +520,6 @@ impl Prompt {
surface.set_string(line_area.x, line_area.y, self.line.clone(), prompt_color);
}
}

/// Saves the current line to the configured history register, if there is one.
pub(crate) fn save_line_to_history(&self, editor: &mut Editor) {
let Some(register) = self.history_register else {
return;
};
if let Err(err) = editor.registers.push(register, self.line.clone()) {
editor.set_error(err.to_string());
}
}
}

impl Component for Prompt {
Expand Down Expand Up @@ -613,7 +607,14 @@ impl Component for Prompt {
&last_item
} else {
if last_item != self.line {
self.save_line_to_history(cx.editor);
// store in history
if let Some(register) = self.history_register {
if let Err(err) =
cx.editor.registers.push(register, self.line.clone())
{
cx.editor.set_error(err.to_string());
}
};
}

&self.line
Expand Down

0 comments on commit 592a41d

Please sign in to comment.