on master help sign in
completion highlighting
8f37c26Jan Hrastnik
helix-term/src/application.rs+28−8
@@ -243,7 +243,7 @@ impl Renderer {
243 for i in (3..7) {
243 for i in (3..7) {
244 self.surface.set_string(
244 self.surface.set_string(
245 0,
245 0,
246− self.size.1 - i,
246+ self.size.1 - i as u16,
247 " ".repeat(self.size.0 as usize),
247 " ".repeat(self.size.0 as usize),
248 self.text_color,
248 self.text_color,
249 );
249 );
@@ -253,12 +253,23 @@ impl Renderer {
253 view.theme.get("ui.statusline"),
253 view.theme.get("ui.statusline"),
254 );
254 );
255 for i in (0..completion.len()) {
255 for i in (0..completion.len()) {
256+ if prompt.completion_selection_index.is_some()
257+ && i == prompt.completion_selection_index.unwrap()
258+ {
256− self.surface.set_string(
259+ self.surface.set_string(
260+ 1,
261+ self.size.1 - 6 + i as u16,
262+ &completion[i],
263+ Style::default().bg(Color::Rgb(104, 060, 232)),
264+ );
265+ } else {
266+ self.surface.set_string(
257− 1,
267+ 1,
258− self.size.1 - 6 + i as u16,
268+ self.size.1 - 6 + i as u16,
259− &completion[i],
269+ &completion[i],
260− self.text_color,
270+ self.text_color,
261− )
271+ );
272+ }
262 }
273 }
263 }
274 }
264 // render buffer text
275 // render buffer text
@@ -409,16 +420,24 @@ impl Application {
409 ..
420 ..
410 }] = keys.as_slice()
421 }] = keys.as_slice()
411 {
422 {
423+ let command_list = vec![
424+ String::from("q"),
425+ String::from("aaa"),
426+ String::from("bbb"),
427+ String::from("ccc"),
428+ ];
429+
412 let prompt = Prompt::new(
430 let prompt = Prompt::new(
413 ":".to_owned(),
431 ":".to_owned(),
414 |_input: &str| {
432 |_input: &str| {
433+ let mut matches = vec![];
434+ // TODO: i need this duplicate list right now to avoid borrow checker issues
415 let placeholder_list = vec![
435 let placeholder_list = vec![
416 String::from("q"),
436 String::from("q"),
417 String::from("aaa"),
437 String::from("aaa"),
418 String::from("bbb"),
438 String::from("bbb"),
419 String::from("ccc"),
439 String::from("ccc"),
420 ];
440 ];
421− let mut matches = vec![];
422 for command in placeholder_list {
441 for command in placeholder_list {
423 if command.contains(_input) {
442 if command.contains(_input) {
424 matches.push(command);
443 matches.push(command);
@@ -433,6 +452,7 @@ impl Application {
433 "q" => editor.should_close = true,
452 "q" => editor.should_close = true,
434 _ => (),
453 _ => (),
435 },
454 },
455+ command_list,
436 );
456 );
437
457
438 self.prompt = Some(prompt);
458 self.prompt = Some(prompt);
helix-view/src/prompt.rs+15−4
@@ -8,6 +8,7 @@ pub struct Prompt {
8 pub cursor: usize,
8 pub cursor: usize,
9 pub completion: Option<Vec<String>>,
9 pub completion: Option<Vec<String>>,
10 pub should_close: bool,
10 pub should_close: bool,
11+ pub completion_selection_index: Option<usize>,
11 completion_fn: Box<dyn FnMut(&str) -> Option<Vec<String>>>,
12 completion_fn: Box<dyn FnMut(&str) -> Option<Vec<String>>>,
12 callback_fn: Box<dyn FnMut(&mut Editor, &str)>,
13 callback_fn: Box<dyn FnMut(&mut Editor, &str)>,
13}
14}
@@ -17,13 +18,15 @@ impl Prompt {
17 prompt: String,
18 prompt: String,
18 completion_fn: impl FnMut(&str) -> Option<Vec<String>> + 'static,
19 completion_fn: impl FnMut(&str) -> Option<Vec<String>> + 'static,
19 callback_fn: impl FnMut(&mut Editor, &str) + 'static,
20 callback_fn: impl FnMut(&mut Editor, &str) + 'static,
21+ command_list: Vec<String>,
20 ) -> Prompt {
22 ) -> Prompt {
21 Prompt {
23 Prompt {
22 prompt,
24 prompt,
23 line: String::new(),
25 line: String::new(),
24 cursor: 0,
26 cursor: 0,
25− completion: None,
27+ completion: Some(command_list),
26 should_close: false,
28 should_close: false,
29+ completion_selection_index: None,
27 completion_fn: Box::new(completion_fn),
30 completion_fn: Box::new(completion_fn),
28 callback_fn: Box::new(callback_fn),
31 callback_fn: Box::new(callback_fn),
29 }
32 }
@@ -32,6 +35,7 @@ impl Prompt {
32 pub fn insert_char(&mut self, c: char) {
35 pub fn insert_char(&mut self, c: char) {
33 self.line.insert(self.cursor, c);
36 self.line.insert(self.cursor, c);
34 self.cursor += 1;
37 self.cursor += 1;
38+ self.completion = (self.completion_fn)(&self.line);
35 }
39 }
36
40
37 pub fn move_char_left(&mut self) {
41 pub fn move_char_left(&mut self) {
@@ -58,6 +62,15 @@ impl Prompt {
58 if self.cursor > 0 {
62 if self.cursor > 0 {
59 self.line.remove(self.cursor - 1);
63 self.line.remove(self.cursor - 1);
60 self.cursor -= 1;
64 self.cursor -= 1;
65+ self.completion = (self.completion_fn)(&self.line);
66+ }
67+ }
68+
69+ pub fn change_completion_selection(&mut self) {
70+ if self.completion_selection_index.is_none() {
71+ self.completion_selection_index = Some(0)
72+ } else {
73+ self.completion_selection_index = Some(self.completion_selection_index.unwrap() + 1)
61 }
74 }
62 }
75 }
63
76
@@ -96,9 +109,7 @@ impl Prompt {
96 } => (self.callback_fn)(editor, &self.line),
109 } => (self.callback_fn)(editor, &self.line),
97 KeyEvent {
110 KeyEvent {
98 code: KeyCode::Tab, ..
111 code: KeyCode::Tab, ..
99− } => {
112+ } => self.change_completion_selection(),
100− self.completion = (self.completion_fn)(&self.line);
101− }
102 _ => (),
113 _ => (),
103 }
114 }
104 }
115 }