W49: Search field auto-suggest from history
When opening a Find or Replace prompt, prefill the input with the most recent search pattern from the history. The user can still type freely to override or press Backspace to clear it. Only applies to Find / Replace prompts (not GotoLine, etc.). Updated the existing alt_slash test to account for the auto-suggested prefix (it now presses Backspace 5 times to clear 'hello' before typing 'world'). Tests: 1407 pass, zero warnings.
This commit is contained in:
@@ -1157,7 +1157,22 @@ impl Editor {
|
||||
/// [`Mode::Prompt`] with the given kind. The caller's keymap is
|
||||
/// responsible for routing the trigger key to Normal mode first.
|
||||
fn open_prompt(&mut self, kind: PromptKind) -> EditorResult {
|
||||
// Auto-suggest: prefill the search prompt with the most
|
||||
// recent search pattern. The user can still type freely to
|
||||
// override. Only applies to Find / Replace (not Goto, etc.).
|
||||
let prefill: Option<String> = match kind {
|
||||
PromptKind::Find | PromptKind::Replace => self
|
||||
.search
|
||||
.history()
|
||||
.last()
|
||||
.cloned(),
|
||||
_ => None,
|
||||
};
|
||||
self.prompt_input.clear();
|
||||
if let Some(text) = prefill {
|
||||
self.prompt_input.text = text;
|
||||
self.prompt_input.cursor = self.prompt_input.text.chars().count();
|
||||
}
|
||||
self.mode = Mode::Prompt(kind);
|
||||
EditorResult::Running
|
||||
}
|
||||
|
||||
@@ -2070,13 +2070,21 @@ mod tests {
|
||||
fn alt_slash_in_find_prompt_opens_history_popup() {
|
||||
let mut e = make_empty();
|
||||
e.insert_str("hello world hello");
|
||||
// Run two searches to populate history.
|
||||
// Run two searches to populate history. The second one
|
||||
// has to clear the auto-suggested "hello" prefix before
|
||||
// typing "world".
|
||||
e.handle_key(Key::alt('f'));
|
||||
for c in "hello".chars() {
|
||||
e.handle_key(Key::from_char(c));
|
||||
}
|
||||
e.handle_key(Key::ENTER);
|
||||
e.handle_key(Key::alt('f'));
|
||||
// Clear the auto-suggested "hello" from the prompt.
|
||||
e.handle_key(Key::BACKSPACE);
|
||||
e.handle_key(Key::BACKSPACE);
|
||||
e.handle_key(Key::BACKSPACE);
|
||||
e.handle_key(Key::BACKSPACE);
|
||||
e.handle_key(Key::BACKSPACE);
|
||||
for c in "world".chars() {
|
||||
e.handle_key(Key::from_char(c));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user