on master help sign in
Merge pull request #4 from helix-editor/completion-suggestion

Completion suggestion

commit2e12fc9a7cd221bb7b5f4b5c1ece599089770ccb authorBlaž Hrastnik <blaz@mxxn.io> dateWed 2 Dec 2020, 09:44 (6 years ago) parentbc2c6521a3c647
3 files changed+134−27
helix-term/src/application.rs+95−6 helix-view/src/commands.rs+3−14 helix-view/src/prompt.rs+36−7
helix-term/src/application.rs+95−6
@@ -34,6 +34,8 @@ type Terminal = tui::Terminal<CrosstermBackend<std::io::Stdout>>;
3434
3535static EX: smol::Executor = smol::Executor::new();
3636
37+const BASE_WIDTH: u16 = 30;
38+
3739pub struct Application {
3840 editor: Editor,
3941 prompt: Option<Prompt>,
@@ -235,7 +237,52 @@ impl Renderer {
235237 .set_string(1, self.size.1 - 2, mode, self.text_color);
236238 }
237239
238− pub fn render_prompt(&mut self, prompt: &Prompt) {
240+ pub fn render_prompt(&mut self, view: &View, prompt: &Prompt) {
241+ // completion
242+ if !prompt.completion.is_empty() {
243+ // TODO: find out better way of clearing individual lines of the screen
244+ let mut row = 0;
245+ let mut col = 0;
246+ let max_col = self.size.0 / BASE_WIDTH;
247+ let col_height = ((prompt.completion.len() as u16 + max_col - 1) / max_col);
248+
249+ for i in (3..col_height + 3) {
250+ self.surface.set_string(
251+ 0,
252+ self.size.1 - i as u16,
253+ " ".repeat(self.size.0 as usize),
254+ self.text_color,
255+ );
256+ }
257+ self.surface.set_style(
258+ Rect::new(0, self.size.1 - col_height - 2, self.size.0, col_height),
259+ view.theme.get("ui.statusline"),
260+ );
261+ for (i, command) in prompt.completion.iter().enumerate() {
262+ let color = if prompt.completion_selection_index.is_some()
263+ && i == prompt.completion_selection_index.unwrap()
264+ {
265+ Style::default().bg(Color::Rgb(104, 060, 232))
266+ } else {
267+ self.text_color
268+ };
269+ self.surface.set_stringn(
270+ 1 + col * BASE_WIDTH,
271+ self.size.1 - col_height - 2 + row,
272+ &command,
273+ BASE_WIDTH as usize - 1,
274+ color,
275+ );
276+ row += 1;
277+ if row > col_height - 1 {
278+ row = 0;
279+ col += 1;
280+ }
281+ if col > max_col {
282+ break;
283+ }
284+ }
285+ }
239286 // render buffer text
240287 self.surface
241288 .set_string(1, self.size.1 - 1, &prompt.prompt, self.text_color);
@@ -309,10 +356,13 @@ impl Application {
309356
310357 if let Some(view) = &mut self.editor.view {
311358 self.terminal.render_view(view, viewport);
312− }
313−
314− if let Some(prompt) = &self.prompt {
315− self.terminal.render_prompt(prompt);
359+ if let Some(prompt) = &self.prompt {
360+ if prompt.should_close {
361+ self.prompt = None;
362+ } else {
363+ self.terminal.render_prompt(view, prompt);
364+ }
365+ }
316366 }
317367
318368 self.terminal.draw();
@@ -383,7 +433,46 @@ impl Application {
383433 {
384434 let prompt = Prompt::new(
385435 ":".to_owned(),
386− |_input: &str| None, // completion
436+ |_input: &str| {
437+ // TODO: i need this duplicate list right now to avoid borrow checker issues
438+ let command_list = vec![
439+ String::from("q"),
440+ String::from("aaa"),
441+ String::from("bbb"),
442+ String::from("ccc"),
443+ String::from("ddd"),
444+ String::from("eee"),
445+ String::from("averylongcommandaverylongcommandaverylongcommandaverylongcommandaverylongcommand"),
446+ String::from("q"),
447+ String::from("aaa"),
448+ String::from("bbb"),
449+ String::from("ccc"),
450+ String::from("ddd"),
451+ String::from("eee"),
452+ String::from("q"),
453+ String::from("aaa"),
454+ String::from("bbb"),
455+ String::from("ccc"),
456+ String::from("ddd"),
457+ String::from("eee"),
458+ String::from("q"),
459+ String::from("aaa"),
460+ String::from("bbb"),
461+ String::from("ccc"),
462+ String::from("ddd"),
463+ String::from("eee"),
464+ String::from("q"),
465+ String::from("aaa"),
466+ String::from("bbb"),
467+ String::from("ccc"),
468+ String::from("ddd"),
469+ String::from("eee"),
470+ ];
471+ command_list
472+ .into_iter()
473+ .filter(|command| command.contains(_input))
474+ .collect()
475+ }, // completion
387476 |editor: &mut Editor, input: &str| match input {
388477 "q" => editor.should_close = true,
389478 _ => (),
helix-view/src/commands.rs+3−14
@@ -307,23 +307,12 @@ pub fn append_mode(view: &mut View, _count: usize) {
307307 })
308308}
309309
310−pub fn command_mode(_view: &mut View, _count: usize) {
311− use crate::Editor;
312−
313− let prompt = Prompt::new(
314− ":".to_owned(),
315− |_input: &str| None, // completion
316− |editor: &mut Editor, input: &str| match input {
317− "q" => editor.should_close = true,
318− _ => (),
319− },
320− );
310+// TODO: I, A, o and O can share a lot of the primitives.
321311
322− // set_prompt(prompt)
312+pub fn command_mode(_view: &mut View, _count: usize) {
313+ unimplemented!()
323314}
324315
325−// TODO: I, A, o and O can share a lot of the primitives.
326−
327316// calculate line numbers for each selection range
328317fn selection_lines(state: &State) -> Vec<usize> {
329318 let mut lines = state
helix-view/src/prompt.rs+36−7
@@ -1,5 +1,4 @@
1−use crate::commands;
2−use crate::{Editor, View};
1+use crate::Editor;
32use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
43use std::string::String;
54
@@ -7,20 +6,26 @@ pub struct Prompt {
76 pub prompt: String,
87 pub line: String,
98 pub cursor: usize,
10− completion_fn: Box<dyn FnMut(&str) -> Option<Vec<&str>>>,
9+ pub completion: Vec<String>,
10+ pub should_close: bool,
11+ pub completion_selection_index: Option<usize>,
12+ completion_fn: Box<dyn FnMut(&str) -> Vec<String>>,
1113 callback_fn: Box<dyn FnMut(&mut Editor, &str)>,
1214}
1315
1416impl Prompt {
1517 pub fn new(
1618 prompt: String,
17− completion_fn: impl FnMut(&str) -> Option<Vec<&str>> + 'static,
19+ mut completion_fn: impl FnMut(&str) -> Vec<String> + 'static,
1820 callback_fn: impl FnMut(&mut Editor, &str) + 'static,
1921 ) -> Prompt {
2022 Prompt {
2123 prompt,
2224 line: String::new(),
2325 cursor: 0,
26+ completion: completion_fn(""),
27+ should_close: false,
28+ completion_selection_index: None,
2429 completion_fn: Box::new(completion_fn),
2530 callback_fn: Box::new(callback_fn),
2631 }
@@ -29,10 +34,12 @@ impl Prompt {
2934 pub fn insert_char(&mut self, c: char) {
3035 self.line.insert(self.cursor, c);
3136 self.cursor += 1;
37+ self.completion = (self.completion_fn)(&self.line);
38+ self.exit_selection();
3239 }
3340
3441 pub fn move_char_left(&mut self) {
35− if self.cursor > 1 {
42+ if self.cursor > 0 {
3643 self.cursor -= 1;
3744 }
3845 }
@@ -55,7 +62,22 @@ impl Prompt {
5562 if self.cursor > 0 {
5663 self.line.remove(self.cursor - 1);
5764 self.cursor -= 1;
65+ self.completion = (self.completion_fn)(&self.line);
5866 }
67+ self.exit_selection();
68+ }
69+
70+ pub fn change_completion_selection(&mut self) {
71+ if self.completion.is_empty() {
72+ return;
73+ }
74+ let index =
75+ self.completion_selection_index.map(|i| i + 1).unwrap_or(0) % self.completion.len();
76+ self.completion_selection_index = Some(index);
77+ self.line = self.completion[index].clone();
78+ }
79+ pub fn exit_selection(&mut self) {
80+ self.completion_selection_index = None;
5981 }
6082
6183 pub fn handle_input(&mut self, key_event: KeyEvent, editor: &mut Editor) {
@@ -66,7 +88,7 @@ impl Prompt {
6688 } => self.insert_char(c),
6789 KeyEvent {
6890 code: KeyCode::Esc, ..
69− } => unimplemented!("Exit prompt!"),
91+ } => self.should_close = true,
7092 KeyEvent {
7193 code: KeyCode::Right,
7294 ..
@@ -85,12 +107,19 @@ impl Prompt {
85107 } => self.move_start(),
86108 KeyEvent {
87109 code: KeyCode::Backspace,
88− ..
110+ modifiers: KeyModifiers::NONE,
89111 } => self.delete_char_backwards(),
90112 KeyEvent {
91113 code: KeyCode::Enter,
92114 ..
93115 } => (self.callback_fn)(editor, &self.line),
116+ KeyEvent {
117+ code: KeyCode::Tab, ..
118+ } => self.change_completion_selection(),
119+ KeyEvent {
120+ code: KeyCode::Char('q'),
121+ modifiers: KeyModifiers::CONTROL,
122+ } => self.exit_selection(),
94123 _ => (),
95124 }
96125 }