on master help sign in
Add c-s to pick word under doc cursor to prompt line & search completion (#831)

* Add prompt shourtcut to book Add completions to search Add c-s to pick word under doc cursor to prompt line * limit 20 last items of search completion, update book * Update book/src/keymap.md Co-authored-by: Ivan Tham <pickfire@riseup.net> * limit search completions 200 Co-authored-by: Ivan Tham <pickfire@riseup.net>

commit39584cbccdb06b528220a13b643416f3fd5dc3c8 authorCossonLeo <20379044+cossonleo@users.noreply.github.com> dateThu 4 Nov 2021, 11:26 (5 years ago) parent70d21a9
4 files changed+86−3
book/src/keymap.md+22 helix-term/src/commands.rs+29 helix-term/src/ui/mod.rs+2−1 helix-term/src/ui/prompt.rs+33−2
book/src/keymap.md+22
@@ -258,3 +258,25 @@ Keys to use within picker. Remapping currently not supported.
258258| `Ctrl-s` | Open horizontally |
259259| `Ctrl-v` | Open vertically |
260260| `Escape`, `Ctrl-c` | Close picker |
261+
262+# Prompt
263+Keys to use within prompt, Remapping currently not supported.
264+| Key | Description |
265+| ----- | ------------- |
266+| `Escape`, `Ctrl-c` | Close prompt |
267+| `Alt-b`, `Alt-Left` | Backward a word |
268+| `Ctrl-b`, `Left` | Backward a char |
269+| `Alt-f`, `Alt-Right` | Forward a word |
270+| `Ctrl-f`, `Right` | Forward a char |
271+| `Ctrl-e`, `End` | move prompt end |
272+| `Ctrl-a`, `Home` | move prompt start |
273+| `Ctrl-w` | delete previous word |
274+| `Ctrl-k` | delete to end of line |
275+| `backspace` | delete previous char |
276+| `Ctrl-s` | insert a word under doc cursor, may be changed to Ctrl-r Ctrl-w later |
277+| `Ctrl-p`, `Up` | select previous history |
278+| `Ctrl-n`, `Down` | select next history |
279+| `Tab` | slect next completion item |
280+| `BackTab` | slect previous completion item |
281+| `Enter` | Open selected |
282+
helix-term/src/commands.rs+29
@@ -1087,6 +1087,7 @@ fn select_regex(cx: &mut Context) {
10871087 cx,
10881088 "select:".into(),
10891089 Some(reg),
1090+ |_input: &str| Vec::new(),
10901091 move |view, doc, regex, event| {
10911092 if event != PromptEvent::Update {
10921093 return;
@@ -1109,6 +1110,7 @@ fn split_selection(cx: &mut Context) {
11091110 cx,
11101111 "split:".into(),
11111112 Some(reg),
1113+ |_input: &str| Vec::new(),
11121114 move |view, doc, regex, event| {
11131115 if event != PromptEvent::Update {
11141116 return;
@@ -1172,6 +1174,15 @@ fn search_impl(doc: &mut Document, view: &mut View, contents: &str, regex: &Rege
11721174 };
11731175}
11741176
1177+fn search_completions(cx: &mut Context, reg: Option<char>) -> Vec<String> {
1178+ let mut items = reg
1179+ .and_then(|reg| cx.editor.registers.get(reg))
1180+ .map_or(Vec::new(), |reg| reg.read().iter().take(200).collect());
1181+ items.sort_unstable();
1182+ items.dedup();
1183+ items.into_iter().cloned().collect()
1184+}
1185+
11751186// TODO: use one function for search vs extend
11761187fn search(cx: &mut Context) {
11771188 let reg = cx.register.unwrap_or('/');
@@ -1182,11 +1193,19 @@ fn search(cx: &mut Context) {
11821193 // HAXX: sadly we can't avoid allocating a single string for the whole buffer since we can't
11831194 // feed chunks into the regex yet
11841195 let contents = doc.text().slice(..).to_string();
1196+ let completions = search_completions(cx, Some(reg));
11851197
11861198 let prompt = ui::regex_prompt(
11871199 cx,
11881200 "search:".into(),
11891201 Some(reg),
1202+ move |input: &str| {
1203+ completions
1204+ .iter()
1205+ .filter(|comp| comp.starts_with(input))
1206+ .map(|comp| (0.., std::borrow::Cow::Owned(comp.clone())))
1207+ .collect()
1208+ },
11901209 move |view, doc, regex, event| {
11911210 if event != PromptEvent::Update {
11921211 return;
@@ -1246,10 +1265,19 @@ fn global_search(cx: &mut Context) {
12461265 let (all_matches_sx, all_matches_rx) =
12471266 tokio::sync::mpsc::unbounded_channel::<(usize, PathBuf)>();
12481267 let smart_case = cx.editor.config.smart_case;
1268+
1269+ let completions = search_completions(cx, None);
12491270 let prompt = ui::regex_prompt(
12501271 cx,
12511272 "global search:".into(),
12521273 None,
1274+ move |input: &str| {
1275+ completions
1276+ .iter()
1277+ .filter(|comp| comp.starts_with(input))
1278+ .map(|comp| (0.., std::borrow::Cow::Owned(comp.clone())))
1279+ .collect()
1280+ },
12531281 move |_view, _doc, regex, event| {
12541282 if event != PromptEvent::Validate {
12551283 return;
@@ -4086,6 +4114,7 @@ fn keep_selections(cx: &mut Context) {
40864114 cx,
40874115 "keep:".into(),
40884116 Some(reg),
4117+ |_input: &str| Vec::new(),
40894118 move |view, doc, regex, event| {
40904119 if event != PromptEvent::Update {
40914120 return;
helix-term/src/ui/mod.rs+2−1
@@ -29,6 +29,7 @@ pub fn regex_prompt(
2929 cx: &mut crate::commands::Context,
3030 prompt: std::borrow::Cow<'static, str>,
3131 history_register: Option<char>,
32+ completion_fn: impl FnMut(&str) -> Vec<prompt::Completion> + 'static,
3233 fun: impl Fn(&mut View, &mut Document, Regex, PromptEvent) + 'static,
3334) -> Prompt {
3435 let (view, doc) = current!(cx.editor);
@@ -38,7 +39,7 @@ pub fn regex_prompt(
3839 Prompt::new(
3940 prompt,
4041 history_register,
41− |_input: &str| Vec::new(), // this is fine because Vec::new() doesn't allocate
42+ completion_fn,
4243 move |cx: &mut crate::compositor::Context, input: &str, event: PromptEvent| {
4344 match event {
4445 PromptEvent::Abort => {
helix-term/src/ui/prompt.rs+33−2
@@ -185,6 +185,11 @@ impl Prompt {
185185 self.exit_selection();
186186 }
187187
188+ pub fn insert_str(&mut self, s: &str) {
189+ self.line.insert_str(self.cursor, s);
190+ self.cursor += s.len();
191+ }
192+
188193 pub fn move_cursor(&mut self, movement: Movement) {
189194 let pos = self.eval_movement(movement);
190195 self.cursor = pos
@@ -473,6 +478,26 @@ impl Component for Prompt {
473478 self.delete_char_backwards();
474479 (self.callback_fn)(cx, &self.line, PromptEvent::Update);
475480 }
481+ KeyEvent {
482+ code: KeyCode::Char('s'),
483+ modifiers: KeyModifiers::CONTROL,
484+ } => {
485+ let (view, doc) = current!(cx.editor);
486+ let text = doc.text().slice(..);
487+
488+ use helix_core::textobject;
489+ let range = textobject::textobject_word(
490+ text,
491+ doc.selection(view.id).primary(),
492+ textobject::TextObject::Inside,
493+ 1,
494+ );
495+ let line = text.slice(range.from()..range.to()).to_string();
496+ if !line.is_empty() {
497+ self.insert_str(line.as_str());
498+ (self.callback_fn)(cx, &self.line, PromptEvent::Update);
499+ }
500+ }
476501 KeyEvent {
477502 code: KeyCode::Enter,
478503 ..
@@ -520,11 +545,17 @@ impl Component for Prompt {
520545 }
521546 KeyEvent {
522547 code: KeyCode::Tab, ..
523− } => self.change_completion_selection(CompletionDirection::Forward),
548+ } => {
549+ self.change_completion_selection(CompletionDirection::Forward);
550+ (self.callback_fn)(cx, &self.line, PromptEvent::Update)
551+ }
524552 KeyEvent {
525553 code: KeyCode::BackTab,
526554 ..
527− } => self.change_completion_selection(CompletionDirection::Backward),
555+ } => {
556+ self.change_completion_selection(CompletionDirection::Backward);
557+ (self.callback_fn)(cx, &self.line, PromptEvent::Update)
558+ }
528559 KeyEvent {
529560 code: KeyCode::Char('q'),
530561 modifiers: KeyModifiers::CONTROL,