diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 91ad522c8..0da241e14 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -2365,7 +2365,10 @@ fn global_search(cx: &mut Context) { .boxed() }; - let mut picker = Picker::new( + let reg = cx.register.unwrap_or('/'); + cx.editor.registers.last_search_register = reg; + + let picker = Picker::new( columns, 1, // contents vec![], @@ -2401,16 +2404,9 @@ fn global_search(cx: &mut Context) { .with_preview(|_editor, FileResult { path, line_num, .. }| { Some((path.clone().into(), Some((*line_num, *line_num)))) }) + .with_history_register(Some(reg)) .with_dynamic_query(get_files, Some(275)); - if let Some((reg, line)) = cx - .register - .and_then(|reg| Some((reg, cx.editor.registers.first(reg, cx.editor)?))) - { - picker = picker.with_line(line.into_owned(), cx.editor); - cx.editor.registers.last_search_register = reg; - } - cx.push_layer(Box::new(overlaid(picker))); } diff --git a/helix-term/src/ui/picker.rs b/helix-term/src/ui/picker.rs index 7cd9fa0c8..ed750b6d3 100644 --- a/helix-term/src/ui/picker.rs +++ b/helix-term/src/ui/picker.rs @@ -394,9 +394,8 @@ impl Picker { self } - pub fn with_line(mut self, line: String, editor: &Editor) -> Self { - self.prompt.set_line(line, editor); - self.handle_prompt_change(); + pub fn with_history_register(mut self, history_register: Option) -> Self { + self.prompt.with_history_register(history_register); self } @@ -976,10 +975,21 @@ impl Component for Picker { - if let Some(option) = self.selection() { - (self.callback_fn)(ctx, option, Action::Replace); + // If the prompt has a history completion and is empty, use enter to accept + // that completion + if let Some(completion) = self + .prompt + .first_history_completion(ctx.editor) + .filter(|_| self.prompt.line().is_empty()) + { + self.prompt.set_line(completion.to_string(), ctx.editor); + self.handle_prompt_change(); + } else { + if let Some(option) = self.selection() { + (self.callback_fn)(ctx, option, Action::Replace); + } + return close_fn(self); } - return close_fn(self); } ctrl!('s') => { if let Some(option) = self.selection() { diff --git a/helix-term/src/ui/prompt.rs b/helix-term/src/ui/prompt.rs index be2e60c8f..fe6448e63 100644 --- a/helix-term/src/ui/prompt.rs +++ b/helix-term/src/ui/prompt.rs @@ -116,6 +116,19 @@ impl Prompt { &self.line } + pub fn with_history_register(&mut self, history_register: Option) -> &mut Self { + self.history_register = history_register; + self + } + + pub(crate) fn first_history_completion<'a>( + &'a self, + editor: &'a Editor, + ) -> Option> { + self.history_register + .and_then(|reg| editor.registers.first(reg, editor)) + } + pub fn recalculate_completion(&mut self, editor: &Editor) { self.exit_selection(); self.completion = (self.completion_fn)(editor, &self.line); @@ -480,10 +493,7 @@ impl Prompt { let line_area = area.clip_left(self.prompt.len() as u16).clip_top(line); if self.line.is_empty() { // Show the most recently entered value as a suggestion. - if let Some(suggestion) = self - .history_register - .and_then(|reg| cx.editor.registers.first(reg, cx.editor)) - { + if let Some(suggestion) = self.first_history_completion(cx.editor) { surface.set_string(line_area.x, line_area.y, suggestion, suggestion_color); } } else if let Some((language, loader)) = self.language.as_ref() { @@ -582,8 +592,7 @@ impl Component for Prompt { self.recalculate_completion(cx.editor); } else { let last_item = self - .history_register - .and_then(|reg| cx.editor.registers.first(reg, cx.editor)) + .first_history_completion(cx.editor) .map(|entry| entry.to_string()) .unwrap_or_else(|| String::from(""));