on master help sign in
Add c-s to pick word under doc cursor to prompt line & search completion (#831)
39584cbCossonLeo
book/src/keymap.md+22
@@ -258,3 +258,25 @@ Keys to use within picker. Remapping currently not supported.
258| `Ctrl-s` | Open horizontally |
258| `Ctrl-s` | Open horizontally |
259| `Ctrl-v` | Open vertically |
259| `Ctrl-v` | Open vertically |
260| `Escape`, `Ctrl-c` | Close picker |
260| `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) {
1087 cx,
1087 cx,
1088 "select:".into(),
1088 "select:".into(),
1089 Some(reg),
1089 Some(reg),
1090+ |_input: &str| Vec::new(),
1090 move |view, doc, regex, event| {
1091 move |view, doc, regex, event| {
1091 if event != PromptEvent::Update {
1092 if event != PromptEvent::Update {
1092 return;
1093 return;
@@ -1109,6 +1110,7 @@ fn split_selection(cx: &mut Context) {
1109 cx,
1110 cx,
1110 "split:".into(),
1111 "split:".into(),
1111 Some(reg),
1112 Some(reg),
1113+ |_input: &str| Vec::new(),
1112 move |view, doc, regex, event| {
1114 move |view, doc, regex, event| {
1113 if event != PromptEvent::Update {
1115 if event != PromptEvent::Update {
1114 return;
1116 return;
@@ -1172,6 +1174,15 @@ fn search_impl(doc: &mut Document, view: &mut View, contents: &str, regex: &Rege
1172 };
1174 };
1173}
1175}
1174
1176
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+
1175// TODO: use one function for search vs extend
1186// TODO: use one function for search vs extend
1176fn search(cx: &mut Context) {
1187fn search(cx: &mut Context) {
1177 let reg = cx.register.unwrap_or('/');
1188 let reg = cx.register.unwrap_or('/');
@@ -1182,11 +1193,19 @@ fn search(cx: &mut Context) {
1182 // HAXX: sadly we can't avoid allocating a single string for the whole buffer since we can't
1193 // HAXX: sadly we can't avoid allocating a single string for the whole buffer since we can't
1183 // feed chunks into the regex yet
1194 // feed chunks into the regex yet
1184 let contents = doc.text().slice(..).to_string();
1195 let contents = doc.text().slice(..).to_string();
1196+ let completions = search_completions(cx, Some(reg));
1185
1197
1186 let prompt = ui::regex_prompt(
1198 let prompt = ui::regex_prompt(
1187 cx,
1199 cx,
1188 "search:".into(),
1200 "search:".into(),
1189 Some(reg),
1201 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+ },
1190 move |view, doc, regex, event| {
1209 move |view, doc, regex, event| {
1191 if event != PromptEvent::Update {
1210 if event != PromptEvent::Update {
1192 return;
1211 return;
@@ -1246,10 +1265,19 @@ fn global_search(cx: &mut Context) {
1246 let (all_matches_sx, all_matches_rx) =
1265 let (all_matches_sx, all_matches_rx) =
1247 tokio::sync::mpsc::unbounded_channel::<(usize, PathBuf)>();
1266 tokio::sync::mpsc::unbounded_channel::<(usize, PathBuf)>();
1248 let smart_case = cx.editor.config.smart_case;
1267 let smart_case = cx.editor.config.smart_case;
1268+
1269+ let completions = search_completions(cx, None);
1249 let prompt = ui::regex_prompt(
1270 let prompt = ui::regex_prompt(
1250 cx,
1271 cx,
1251 "global search:".into(),
1272 "global search:".into(),
1252 None,
1273 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+ },
1253 move |_view, _doc, regex, event| {
1281 move |_view, _doc, regex, event| {
1254 if event != PromptEvent::Validate {
1282 if event != PromptEvent::Validate {
1255 return;
1283 return;
@@ -4086,6 +4114,7 @@ fn keep_selections(cx: &mut Context) {
4086 cx,
4114 cx,
4087 "keep:".into(),
4115 "keep:".into(),
4088 Some(reg),
4116 Some(reg),
4117+ |_input: &str| Vec::new(),
4089 move |view, doc, regex, event| {
4118 move |view, doc, regex, event| {
4090 if event != PromptEvent::Update {
4119 if event != PromptEvent::Update {
4091 return;
4120 return;
helix-term/src/ui/mod.rs+2−1
@@ -29,6 +29,7 @@ pub fn regex_prompt(
29 cx: &mut crate::commands::Context,
29 cx: &mut crate::commands::Context,
30 prompt: std::borrow::Cow<'static, str>,
30 prompt: std::borrow::Cow<'static, str>,
31 history_register: Option<char>,
31 history_register: Option<char>,
32+ completion_fn: impl FnMut(&str) -> Vec<prompt::Completion> + 'static,
32 fun: impl Fn(&mut View, &mut Document, Regex, PromptEvent) + 'static,
33 fun: impl Fn(&mut View, &mut Document, Regex, PromptEvent) + 'static,
33) -> Prompt {
34) -> Prompt {
34 let (view, doc) = current!(cx.editor);
35 let (view, doc) = current!(cx.editor);
@@ -38,7 +39,7 @@ pub fn regex_prompt(
38 Prompt::new(
39 Prompt::new(
39 prompt,
40 prompt,
40 history_register,
41 history_register,
41− |_input: &str| Vec::new(), // this is fine because Vec::new() doesn't allocate
42+ completion_fn,
42 move |cx: &mut crate::compositor::Context, input: &str, event: PromptEvent| {
43 move |cx: &mut crate::compositor::Context, input: &str, event: PromptEvent| {
43 match event {
44 match event {
44 PromptEvent::Abort => {
45 PromptEvent::Abort => {
helix-term/src/ui/prompt.rs+33−2
@@ -185,6 +185,11 @@ impl Prompt {
185 self.exit_selection();
185 self.exit_selection();
186 }
186 }
187
187
188+ pub fn insert_str(&mut self, s: &str) {
189+ self.line.insert_str(self.cursor, s);
190+ self.cursor += s.len();
191+ }
192+
188 pub fn move_cursor(&mut self, movement: Movement) {
193 pub fn move_cursor(&mut self, movement: Movement) {
189 let pos = self.eval_movement(movement);
194 let pos = self.eval_movement(movement);
190 self.cursor = pos
195 self.cursor = pos
@@ -473,6 +478,26 @@ impl Component for Prompt {
473 self.delete_char_backwards();
478 self.delete_char_backwards();
474 (self.callback_fn)(cx, &self.line, PromptEvent::Update);
479 (self.callback_fn)(cx, &self.line, PromptEvent::Update);
475 }
480 }
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+ }
476 KeyEvent {
501 KeyEvent {
477 code: KeyCode::Enter,
502 code: KeyCode::Enter,
478 ..
503 ..
@@ -520,11 +545,17 @@ impl Component for Prompt {
520 }
545 }
521 KeyEvent {
546 KeyEvent {
522 code: KeyCode::Tab, ..
547 code: KeyCode::Tab, ..
548+ } => {
523− } => self.change_completion_selection(CompletionDirection::Forward),
549+ self.change_completion_selection(CompletionDirection::Forward);
550+ (self.callback_fn)(cx, &self.line, PromptEvent::Update)
551+ }
524 KeyEvent {
552 KeyEvent {
525 code: KeyCode::BackTab,
553 code: KeyCode::BackTab,
526 ..
554 ..
555+ } => {
527− } => self.change_completion_selection(CompletionDirection::Backward),
556+ self.change_completion_selection(CompletionDirection::Backward);
557+ (self.callback_fn)(cx, &self.line, PromptEvent::Update)
558+ }
528 KeyEvent {
559 KeyEvent {
529 code: KeyCode::Char('q'),
560 code: KeyCode::Char('q'),
530 modifiers: KeyModifiers::CONTROL,
561 modifiers: KeyModifiers::CONTROL,