on master help sign in
completion highlighting
commit8f37c26f350b6409d7e13eb7fee4ef241dd4af5c authorJan Hrastnik <jan.hrastnik2@gmail.com> dateTue 20 Oct 2020, 23:02 (6 years ago) parentf3ddb86
2 files changed+43−12
helix-term/src/application.rs+28−8 helix-view/src/prompt.rs+15−4
helix-term/src/application.rs+28−8
@@ -243,7 +243,7 @@ impl Renderer {
243243 for i in (3..7) {
244244 self.surface.set_string(
245245 0,
246− self.size.1 - i,
246+ self.size.1 - i as u16,
247247 " ".repeat(self.size.0 as usize),
248248 self.text_color,
249249 );
@@ -253,12 +253,23 @@ impl Renderer {
253253 view.theme.get("ui.statusline"),
254254 );
255255 for i in (0..completion.len()) {
256− self.surface.set_string(
257− 1,
258− self.size.1 - 6 + i as u16,
259− &completion[i],
260− self.text_color,
261− )
256+ if prompt.completion_selection_index.is_some()
257+ && i == prompt.completion_selection_index.unwrap()
258+ {
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(
267+ 1,
268+ self.size.1 - 6 + i as u16,
269+ &completion[i],
270+ self.text_color,
271+ );
272+ }
262273 }
263274 }
264275 // render buffer text
@@ -409,16 +420,24 @@ impl Application {
409420 ..
410421 }] = keys.as_slice()
411422 {
423+ let command_list = vec![
424+ String::from("q"),
425+ String::from("aaa"),
426+ String::from("bbb"),
427+ String::from("ccc"),
428+ ];
429+
412430 let prompt = Prompt::new(
413431 ":".to_owned(),
414432 |_input: &str| {
433+ let mut matches = vec![];
434+ // TODO: i need this duplicate list right now to avoid borrow checker issues
415435 let placeholder_list = vec![
416436 String::from("q"),
417437 String::from("aaa"),
418438 String::from("bbb"),
419439 String::from("ccc"),
420440 ];
421− let mut matches = vec![];
422441 for command in placeholder_list {
423442 if command.contains(_input) {
424443 matches.push(command);
@@ -433,6 +452,7 @@ impl Application {
433452 "q" => editor.should_close = true,
434453 _ => (),
435454 },
455+ command_list,
436456 );
437457
438458 self.prompt = Some(prompt);
helix-view/src/prompt.rs+15−4
@@ -8,6 +8,7 @@ pub struct Prompt {
88 pub cursor: usize,
99 pub completion: Option<Vec<String>>,
1010 pub should_close: bool,
11+ pub completion_selection_index: Option<usize>,
1112 completion_fn: Box<dyn FnMut(&str) -> Option<Vec<String>>>,
1213 callback_fn: Box<dyn FnMut(&mut Editor, &str)>,
1314}
@@ -17,13 +18,15 @@ impl Prompt {
1718 prompt: String,
1819 completion_fn: impl FnMut(&str) -> Option<Vec<String>> + 'static,
1920 callback_fn: impl FnMut(&mut Editor, &str) + 'static,
21+ command_list: Vec<String>,
2022 ) -> Prompt {
2123 Prompt {
2224 prompt,
2325 line: String::new(),
2426 cursor: 0,
25− completion: None,
27+ completion: Some(command_list),
2628 should_close: false,
29+ completion_selection_index: None,
2730 completion_fn: Box::new(completion_fn),
2831 callback_fn: Box::new(callback_fn),
2932 }
@@ -32,6 +35,7 @@ impl Prompt {
3235 pub fn insert_char(&mut self, c: char) {
3336 self.line.insert(self.cursor, c);
3437 self.cursor += 1;
38+ self.completion = (self.completion_fn)(&self.line);
3539 }
3640
3741 pub fn move_char_left(&mut self) {
@@ -58,6 +62,15 @@ impl Prompt {
5862 if self.cursor > 0 {
5963 self.line.remove(self.cursor - 1);
6064 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)
6174 }
6275 }
6376
@@ -96,9 +109,7 @@ impl Prompt {
96109 } => (self.callback_fn)(editor, &self.line),
97110 KeyEvent {
98111 code: KeyCode::Tab, ..
99− } => {
100− self.completion = (self.completion_fn)(&self.line);
101− }
112+ } => self.change_completion_selection(),
102113 _ => (),
103114 }
104115 }