From c485063e789689b0312854325e16ad802d45ba2b Mon Sep 17 00:00:00 2001 From: Roberto Vidal Date: Wed, 19 Jul 2023 08:18:52 +0200 Subject: [PATCH] add command history picker --- book/src/keymap.md | 1 + helix-term/src/commands.rs | 37 ++++++++++++++++++++++++++++++++ helix-term/src/commands/typed.rs | 12 ++++++++--- helix-term/src/keymap/default.rs | 1 + 4 files changed, 48 insertions(+), 3 deletions(-) diff --git a/book/src/keymap.md b/book/src/keymap.md index 402b83ef7..d75582753 100644 --- a/book/src/keymap.md +++ b/book/src/keymap.md @@ -282,6 +282,7 @@ This layer is a kludge of mappings, mostly pickers. | `F` | Open file picker at current working directory | `file_picker_in_current_directory` | | `b` | Open buffer picker | `buffer_picker` | | `j` | Open jumplist picker | `jumplist_picker` | +| `:` | Open command history picker | `command_history_picker` | | `g` | Open changed file picker | `changed_file_picker` | | `G` | Debug (experimental) | N/A | | `k` | Show documentation for item under cursor in a [popup](#popup) (**LSP**) | `hover` | diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 7e0bee92b..bdac73ee8 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -340,6 +340,7 @@ impl MappableCommand { code_action, "Perform code action", buffer_picker, "Open buffer picker", jumplist_picker, "Open jumplist picker", + command_history_picker, "Open command history picker", symbol_picker, "Open symbol picker", changed_file_picker, "Open changed file picker", select_references_to_symbol_under_cursor, "Select symbol references", @@ -2926,6 +2927,42 @@ fn buffer_picker(cx: &mut Context) { cx.push_layer(Box::new(overlaid(picker))); } +fn command_history_picker(cx: &mut Context) { + let command_history = cx.editor.registers.read(':', cx.editor); + + let items: Vec = command_history + .into_iter() + .flatten() + .map(|entry| Command { + args: entry.to_string(), + }) + .collect(); + + struct Command { + args: String, + } + + let columns = [PickerColumn::new("command", |command: &Command, _| { + format!(":{}", command.args).into() + })]; + + let picker = Picker::new(columns, 0, items, (), |cx, option, _action| { + let args = option.args.clone(); + + cx.jobs.callback(async move { + let callback = |editor: &mut Editor, compositor: &mut Compositor| { + let prompt = command_mode_prompt().with_line(args, editor); + + compositor.push(Box::new(prompt)); + }; + + Ok(Callback::EditorCompositor(Box::new(callback))) + }) + }); + + cx.push_layer(Box::new(overlaid(picker))); +} + fn jumplist_picker(cx: &mut Context) { struct JumpMeta { id: DocumentId, diff --git a/helix-term/src/commands/typed.rs b/helix-term/src/commands/typed.rs index 88ee22527..cc33c1979 100644 --- a/helix-term/src/commands/typed.rs +++ b/helix-term/src/commands/typed.rs @@ -3156,6 +3156,14 @@ pub static TYPABLE_COMMAND_MAP: Lazy Prompt { let mut prompt = Prompt::new( ":".into(), Some(':'), @@ -3246,9 +3254,7 @@ pub(super) fn command_mode(cx: &mut Context) { None }); - // Calculate initial completion - prompt.recalculate_completion(cx.editor); - cx.push_layer(Box::new(prompt)); + prompt } fn argument_number_of(shellwords: &Shellwords) -> usize { diff --git a/helix-term/src/keymap/default.rs b/helix-term/src/keymap/default.rs index 5a3e8eed4..6daad066e 100644 --- a/helix-term/src/keymap/default.rs +++ b/helix-term/src/keymap/default.rs @@ -223,6 +223,7 @@ pub fn default() -> HashMap { "F" => file_picker_in_current_directory, "b" => buffer_picker, "j" => jumplist_picker, + ":" => command_history_picker, "s" => symbol_picker, "S" => workspace_symbol_picker, "d" => diagnostics_picker,