global_search: Suggest latest '/' register value

Michael Davis 4 months ago
parent d5e4cf8009
commit b1d90d1931
No known key found for this signature in database

@ -2365,7 +2365,10 @@ fn global_search(cx: &mut Context) {
.boxed() .boxed()
}; };
let mut picker = Picker::new( let reg = cx.register.unwrap_or('/');
cx.editor.registers.last_search_register = reg;
let picker = Picker::new(
columns, columns,
1, // contents 1, // contents
vec![], vec![],
@ -2401,16 +2404,9 @@ fn global_search(cx: &mut Context) {
.with_preview(|_editor, FileResult { path, line_num, .. }| { .with_preview(|_editor, FileResult { path, line_num, .. }| {
Some((path.clone().into(), Some((*line_num, *line_num)))) Some((path.clone().into(), Some((*line_num, *line_num))))
}) })
.with_history_register(Some(reg))
.with_dynamic_query(get_files, Some(275)); .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))); cx.push_layer(Box::new(overlaid(picker)));
} }

@ -394,9 +394,8 @@ impl<T: 'static + Send + Sync, D: 'static + Send + Sync> Picker<T, D> {
self self
} }
pub fn with_line(mut self, line: String, editor: &Editor) -> Self { pub fn with_history_register(mut self, history_register: Option<char>) -> Self {
self.prompt.set_line(line, editor); self.prompt.with_history_register(history_register);
self.handle_prompt_change();
self self
} }
@ -976,10 +975,21 @@ impl<I: 'static + Send + Sync, D: 'static + Send + Sync> Component for Picker<I,
} }
} }
key!(Enter) => { key!(Enter) => {
if let Some(option) = self.selection() { // If the prompt has a history completion and is empty, use enter to accept
(self.callback_fn)(ctx, option, Action::Replace); // 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') => { ctrl!('s') => {
if let Some(option) = self.selection() { if let Some(option) = self.selection() {

@ -116,6 +116,19 @@ impl Prompt {
&self.line &self.line
} }
pub fn with_history_register(&mut self, history_register: Option<char>) -> &mut Self {
self.history_register = history_register;
self
}
pub(crate) fn first_history_completion<'a>(
&'a self,
editor: &'a Editor,
) -> Option<Cow<'a, str>> {
self.history_register
.and_then(|reg| editor.registers.first(reg, editor))
}
pub fn recalculate_completion(&mut self, editor: &Editor) { pub fn recalculate_completion(&mut self, editor: &Editor) {
self.exit_selection(); self.exit_selection();
self.completion = (self.completion_fn)(editor, &self.line); 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); let line_area = area.clip_left(self.prompt.len() as u16).clip_top(line);
if self.line.is_empty() { if self.line.is_empty() {
// 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.first_history_completion(cx.editor) {
.history_register
.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);
} }
} else if let Some((language, loader)) = self.language.as_ref() { } else if let Some((language, loader)) = self.language.as_ref() {
@ -582,8 +592,7 @@ impl Component for Prompt {
self.recalculate_completion(cx.editor); self.recalculate_completion(cx.editor);
} else { } else {
let last_item = self let last_item = self
.history_register .first_history_completion(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(""));

Loading…
Cancel
Save