From 26eabd67f6e5c6022ade7953053bbcd8c257ab54 Mon Sep 17 00:00:00 2001 From: cossonleo Date: Tue, 12 Oct 2021 18:05:30 +0800 Subject: [PATCH 1/4] Add prompt shourtcut to book Add completions to search Add c-s to pick word under doc cursor to prompt line --- book/src/keymap.md | 22 ++++++++++++++++++++ helix-term/src/commands.rs | 40 +++++++++++++++++++++++++++++++++++++ helix-term/src/ui/mod.rs | 3 ++- helix-term/src/ui/prompt.rs | 35 ++++++++++++++++++++++++++++++-- 4 files changed, 97 insertions(+), 3 deletions(-) diff --git a/book/src/keymap.md b/book/src/keymap.md index 9ed35f771471..49c794e480ed 100644 --- a/book/src/keymap.md +++ b/book/src/keymap.md @@ -258,3 +258,25 @@ Keys to use within picker. Remapping currently not supported. | `Ctrl-s` | Open horizontally | | `Ctrl-v` | Open vertically | | `Escape`, `Ctrl-c` | Close picker | + +# Prompt +Keys to use within prompt, Remapping currently not supported. +| Key | Description | +| ----- | ------------- | +| `Escape`, `Ctrl-c` | Close prompt | +| `Alt-b`, `Alt-Left` | Backward a word | +| `Ctrl-b`, `Left` | Backward a char | +| `Alt-f`, `Alt-Right` | Forward a word | +| `Ctrl-f`, `Right` | Forward a char | +| `Ctrl-e`, `End` | move prompt end | +| `Ctrl-a`, `Home` | move prompt start | +| `Ctrl-w` | delete previous word | +| `Ctrl-k` | delete to end of line | +| `backspace` | delete previous char | +| `Ctrl-s` | insert a word under doc cursor | +| `Ctrl-p`, `Up` | select previous history | +| `Ctrl-n`, `Down` | select next history | +| `Tab` | slect next completion item | +| `BackTab` | slect previous completion item | +| `Enter` | Open selected | + diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 28657865b580..f467377373e0 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -1087,6 +1087,7 @@ fn select_regex(cx: &mut Context) { cx, "select:".into(), Some(reg), + |_input: &str| Vec::new(), move |view, doc, regex, event| { if event != PromptEvent::Update { return; @@ -1109,6 +1110,7 @@ fn split_selection(cx: &mut Context) { cx, "split:".into(), Some(reg), + |_input: &str| Vec::new(), move |view, doc, regex, event| { if event != PromptEvent::Update { return; @@ -1169,6 +1171,26 @@ fn search_impl(doc: &mut Document, view: &mut View, contents: &str, regex: &Rege }; } +fn search_completions(cx: &mut Context, reg: Option) -> Vec { + let mut exist_elems = std::collections::BTreeSet::new(); + reg.and_then(|reg| cx.editor.registers.get(reg)) + .map_or(Vec::new(), |reg| { + reg.read() + .iter() + .filter(|&item| { + if exist_elems.contains(item) { + false + } else { + exist_elems.insert(item); + true + } + }) + .take(10) + .cloned() + .collect() + }) +} + // TODO: use one function for search vs extend fn search(cx: &mut Context) { let reg = cx.register.unwrap_or('/'); @@ -1179,11 +1201,19 @@ fn search(cx: &mut Context) { // HAXX: sadly we can't avoid allocating a single string for the whole buffer since we can't // feed chunks into the regex yet let contents = doc.text().slice(..).to_string(); + let completions = search_completions(cx, Some(reg)); let prompt = ui::regex_prompt( cx, "search:".into(), Some(reg), + move |input: &str| { + completions + .iter() + .filter(|comp| comp.starts_with(input)) + .map(|comp| (0.., std::borrow::Cow::Owned(comp.clone()))) + .collect() + }, move |view, doc, regex, event| { if event != PromptEvent::Update { return; @@ -1243,10 +1273,19 @@ fn global_search(cx: &mut Context) { let (all_matches_sx, all_matches_rx) = tokio::sync::mpsc::unbounded_channel::<(usize, PathBuf)>(); let smart_case = cx.editor.config.smart_case; + + let completions = search_completions(cx, None); let prompt = ui::regex_prompt( cx, "global search:".into(), None, + move |input: &str| { + completions + .iter() + .filter(|comp| comp.starts_with(input)) + .map(|comp| (0.., std::borrow::Cow::Owned(comp.clone()))) + .collect() + }, move |_view, _doc, regex, event| { if event != PromptEvent::Validate { return; @@ -4083,6 +4122,7 @@ fn keep_selections(cx: &mut Context) { cx, "keep:".into(), Some(reg), + |_input: &str| Vec::new(), move |view, doc, regex, event| { if event != PromptEvent::Update { return; diff --git a/helix-term/src/ui/mod.rs b/helix-term/src/ui/mod.rs index 30a9ec6bce92..24eb7acdf1bd 100644 --- a/helix-term/src/ui/mod.rs +++ b/helix-term/src/ui/mod.rs @@ -29,6 +29,7 @@ pub fn regex_prompt( cx: &mut crate::commands::Context, prompt: std::borrow::Cow<'static, str>, history_register: Option, + completion_fn: impl FnMut(&str) -> Vec + 'static, fun: impl Fn(&mut View, &mut Document, Regex, PromptEvent) + 'static, ) -> Prompt { let (view, doc) = current!(cx.editor); @@ -38,7 +39,7 @@ pub fn regex_prompt( Prompt::new( prompt, history_register, - |_input: &str| Vec::new(), // this is fine because Vec::new() doesn't allocate + completion_fn, move |cx: &mut crate::compositor::Context, input: &str, event: PromptEvent| { match event { PromptEvent::Abort => { diff --git a/helix-term/src/ui/prompt.rs b/helix-term/src/ui/prompt.rs index 853adfc2a9bf..c999ba140209 100644 --- a/helix-term/src/ui/prompt.rs +++ b/helix-term/src/ui/prompt.rs @@ -185,6 +185,11 @@ impl Prompt { self.exit_selection(); } + pub fn insert_str(&mut self, s: &str) { + self.line.insert_str(self.cursor, s); + self.cursor += s.len(); + } + pub fn move_cursor(&mut self, movement: Movement) { let pos = self.eval_movement(movement); self.cursor = pos @@ -473,6 +478,26 @@ impl Component for Prompt { self.delete_char_backwards(); (self.callback_fn)(cx, &self.line, PromptEvent::Update); } + KeyEvent { + code: KeyCode::Char('s'), + modifiers: KeyModifiers::CONTROL, + } => { + let (view, doc) = current!(cx.editor); + let text = doc.text().slice(..); + + use helix_core::textobject; + let range = textobject::textobject_word( + text, + doc.selection(view.id).primary(), + textobject::TextObject::Inside, + 1, + ); + let line = text.slice(range.from()..range.to()).to_string(); + if !line.is_empty() { + self.insert_str(line.as_str()); + (self.callback_fn)(cx, &self.line, PromptEvent::Update); + } + } KeyEvent { code: KeyCode::Enter, .. @@ -520,11 +545,17 @@ impl Component for Prompt { } KeyEvent { code: KeyCode::Tab, .. - } => self.change_completion_selection(CompletionDirection::Forward), + } => { + self.change_completion_selection(CompletionDirection::Forward); + (self.callback_fn)(cx, &self.line, PromptEvent::Update) + } KeyEvent { code: KeyCode::BackTab, .. - } => self.change_completion_selection(CompletionDirection::Backward), + } => { + self.change_completion_selection(CompletionDirection::Backward); + (self.callback_fn)(cx, &self.line, PromptEvent::Update) + } KeyEvent { code: KeyCode::Char('q'), modifiers: KeyModifiers::CONTROL, From e9b3e27cdc098e3765d19804ee1a62a09ca1175f Mon Sep 17 00:00:00 2001 From: cossonleo Date: Tue, 2 Nov 2021 18:16:34 +0800 Subject: [PATCH 2/4] limit 20 last items of search completion, update book --- book/src/keymap.md | 36 ++++++++++++++++++------------------ helix-term/src/commands.rs | 3 ++- 2 files changed, 20 insertions(+), 19 deletions(-) diff --git a/book/src/keymap.md b/book/src/keymap.md index 49c794e480ed..6397278dcb66 100644 --- a/book/src/keymap.md +++ b/book/src/keymap.md @@ -261,22 +261,22 @@ Keys to use within picker. Remapping currently not supported. # Prompt Keys to use within prompt, Remapping currently not supported. -| Key | Description | -| ----- | ------------- | -| `Escape`, `Ctrl-c` | Close prompt | -| `Alt-b`, `Alt-Left` | Backward a word | -| `Ctrl-b`, `Left` | Backward a char | -| `Alt-f`, `Alt-Right` | Forward a word | -| `Ctrl-f`, `Right` | Forward a char | -| `Ctrl-e`, `End` | move prompt end | -| `Ctrl-a`, `Home` | move prompt start | -| `Ctrl-w` | delete previous word | -| `Ctrl-k` | delete to end of line | -| `backspace` | delete previous char | -| `Ctrl-s` | insert a word under doc cursor | -| `Ctrl-p`, `Up` | select previous history | -| `Ctrl-n`, `Down` | select next history | -| `Tab` | slect next completion item | -| `BackTab` | slect previous completion item | -| `Enter` | Open selected | +| Key | Description | +| ----- | ------------- | +| `Escape`, `Ctrl-c` | Close prompt | +| `Alt-b`, `Alt-Left` | Backward a word | +| `Ctrl-b`, `Left` | Backward a char | +| `Alt-f`, `Alt-Right` | Forward a word | +| `Ctrl-f`, `Right` | Forward a char | +| `Ctrl-e`, `End` | move prompt end | +| `Ctrl-a`, `Home` | move prompt start | +| `Ctrl-w` | delete previous word | +| `Ctrl-k` | delete to end of line | +| `backspace` | delete previous char | +| `Ctrl-s` | insert a word under doc cursor, maybe changed to Ctrl-r Ctrl-w | +| `Ctrl-p`, `Up` | select previous history | +| `Ctrl-n`, `Down` | select next history | +| `Tab` | slect next completion item | +| `BackTab` | slect previous completion item | +| `Enter` | Open selected | diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index f467377373e0..c0305ef27cec 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -1177,6 +1177,7 @@ fn search_completions(cx: &mut Context, reg: Option) -> Vec { .map_or(Vec::new(), |reg| { reg.read() .iter() + .rev() .filter(|&item| { if exist_elems.contains(item) { false @@ -1185,7 +1186,7 @@ fn search_completions(cx: &mut Context, reg: Option) -> Vec { true } }) - .take(10) + .take(20) .cloned() .collect() }) From 719099c7112067c6507848a2cfead38d0a9b432a Mon Sep 17 00:00:00 2001 From: CossonLeo <20379044+cossonleo@users.noreply.github.com> Date: Wed, 3 Nov 2021 10:33:12 +0800 Subject: [PATCH 3/4] Update book/src/keymap.md Co-authored-by: Ivan Tham --- book/src/keymap.md | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/book/src/keymap.md b/book/src/keymap.md index 6397278dcb66..2c21d83a49f3 100644 --- a/book/src/keymap.md +++ b/book/src/keymap.md @@ -261,22 +261,22 @@ Keys to use within picker. Remapping currently not supported. # Prompt Keys to use within prompt, Remapping currently not supported. -| Key | Description | -| ----- | ------------- | -| `Escape`, `Ctrl-c` | Close prompt | -| `Alt-b`, `Alt-Left` | Backward a word | -| `Ctrl-b`, `Left` | Backward a char | -| `Alt-f`, `Alt-Right` | Forward a word | -| `Ctrl-f`, `Right` | Forward a char | -| `Ctrl-e`, `End` | move prompt end | -| `Ctrl-a`, `Home` | move prompt start | -| `Ctrl-w` | delete previous word | -| `Ctrl-k` | delete to end of line | -| `backspace` | delete previous char | -| `Ctrl-s` | insert a word under doc cursor, maybe changed to Ctrl-r Ctrl-w | -| `Ctrl-p`, `Up` | select previous history | -| `Ctrl-n`, `Down` | select next history | -| `Tab` | slect next completion item | -| `BackTab` | slect previous completion item | -| `Enter` | Open selected | +| Key | Description | +| ----- | ------------- | +| `Escape`, `Ctrl-c` | Close prompt | +| `Alt-b`, `Alt-Left` | Backward a word | +| `Ctrl-b`, `Left` | Backward a char | +| `Alt-f`, `Alt-Right` | Forward a word | +| `Ctrl-f`, `Right` | Forward a char | +| `Ctrl-e`, `End` | move prompt end | +| `Ctrl-a`, `Home` | move prompt start | +| `Ctrl-w` | delete previous word | +| `Ctrl-k` | delete to end of line | +| `backspace` | delete previous char | +| `Ctrl-s` | insert a word under doc cursor, may be changed to Ctrl-r Ctrl-w later | +| `Ctrl-p`, `Up` | select previous history | +| `Ctrl-n`, `Down` | select next history | +| `Tab` | slect next completion item | +| `BackTab` | slect previous completion item | +| `Enter` | Open selected | From 7e92dbfa2a5ccc0361bd6d87e4e9cefcf573e887 Mon Sep 17 00:00:00 2001 From: cossonleo Date: Wed, 3 Nov 2021 14:51:54 +0800 Subject: [PATCH 4/4] limit search completions 200 --- helix-term/src/commands.rs | 24 ++++++------------------ 1 file changed, 6 insertions(+), 18 deletions(-) diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index c0305ef27cec..e91b51aa8092 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -1172,24 +1172,12 @@ fn search_impl(doc: &mut Document, view: &mut View, contents: &str, regex: &Rege } fn search_completions(cx: &mut Context, reg: Option) -> Vec { - let mut exist_elems = std::collections::BTreeSet::new(); - reg.and_then(|reg| cx.editor.registers.get(reg)) - .map_or(Vec::new(), |reg| { - reg.read() - .iter() - .rev() - .filter(|&item| { - if exist_elems.contains(item) { - false - } else { - exist_elems.insert(item); - true - } - }) - .take(20) - .cloned() - .collect() - }) + let mut items = reg + .and_then(|reg| cx.editor.registers.get(reg)) + .map_or(Vec::new(), |reg| reg.read().iter().take(200).collect()); + items.sort_unstable(); + items.dedup(); + items.into_iter().cloned().collect() } // TODO: use one function for search vs extend