on master help sign in
Merge pull request #4 from helix-editor/completion-suggestion
2e12fc9Blaž Hrastnik
helix-term/src/application.rs+95−6
@@ -34,6 +34,8 @@ type Terminal = tui::Terminal<CrosstermBackend<std::io::Stdout>>;
34
34
35static EX: smol::Executor = smol::Executor::new();
35static EX: smol::Executor = smol::Executor::new();
36
36
37+const BASE_WIDTH: u16 = 30;
38+
37pub struct Application {
39pub struct Application {
38 editor: Editor,
40 editor: Editor,
39 prompt: Option<Prompt>,
41 prompt: Option<Prompt>,
@@ -235,7 +237,52 @@ impl Renderer {
235 .set_string(1, self.size.1 - 2, mode, self.text_color);
237 .set_string(1, self.size.1 - 2, mode, self.text_color);
236 }
238 }
237
239
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+ }
239 // render buffer text
286 // render buffer text
240 self.surface
287 self.surface
241 .set_string(1, self.size.1 - 1, &prompt.prompt, self.text_color);
288 .set_string(1, self.size.1 - 1, &prompt.prompt, self.text_color);
@@ -309,10 +356,13 @@ impl Application {
309
356
310 if let Some(view) = &mut self.editor.view {
357 if let Some(view) = &mut self.editor.view {
311 self.terminal.render_view(view, viewport);
358 self.terminal.render_view(view, viewport);
312− }
313−
314− if let Some(prompt) = &self.prompt {
359+ if let Some(prompt) = &self.prompt {
360+ if prompt.should_close {
361+ self.prompt = None;
362+ } else {
315− self.terminal.render_prompt(prompt);
363+ self.terminal.render_prompt(view, prompt);
364+ }
365+ }
316 }
366 }
317
367
318 self.terminal.draw();
368 self.terminal.draw();
@@ -383,7 +433,46 @@ impl Application {
383 {
433 {
384 let prompt = Prompt::new(
434 let prompt = Prompt::new(
385 ":".to_owned(),
435 ":".to_owned(),
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()
386− |_input: &str| None, // completion
475+ }, // completion
387 |editor: &mut Editor, input: &str| match input {
476 |editor: &mut Editor, input: &str| match input {
388 "q" => editor.should_close = true,
477 "q" => editor.should_close = true,
389 _ => (),
478 _ => (),
helix-view/src/commands.rs+3−14
@@ -307,23 +307,12 @@ pub fn append_mode(view: &mut View, _count: usize) {
307 })
307 })
308}
308}
309
309
310−pub fn command_mode(_view: &mut View, _count: usize) {
310+// TODO: I, A, o and O can share a lot of the primitives.
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− );
321
311
322− // set_prompt(prompt)
312+pub fn command_mode(_view: &mut View, _count: usize) {
313+ unimplemented!()
323}
314}
324
315
325−// TODO: I, A, o and O can share a lot of the primitives.
326−
327// calculate line numbers for each selection range
316// calculate line numbers for each selection range
328fn selection_lines(state: &State) -> Vec<usize> {
317fn selection_lines(state: &State) -> Vec<usize> {
329 let mut lines = state
318 let mut lines = state
helix-view/src/prompt.rs+36−7
@@ -1,5 +1,4 @@
1−use crate::commands;
1+use crate::Editor;
2−use crate::{Editor, View};
3use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
2use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
4use std::string::String;
3use std::string::String;
5
4
@@ -7,20 +6,26 @@ pub struct Prompt {
7 pub prompt: String,
6 pub prompt: String,
8 pub line: String,
7 pub line: String,
9 pub cursor: usize,
8 pub cursor: usize,
9+ pub completion: Vec<String>,
10+ pub should_close: bool,
11+ pub completion_selection_index: Option<usize>,
10− completion_fn: Box<dyn FnMut(&str) -> Option<Vec<&str>>>,
12+ completion_fn: Box<dyn FnMut(&str) -> Vec<String>>,
11 callback_fn: Box<dyn FnMut(&mut Editor, &str)>,
13 callback_fn: Box<dyn FnMut(&mut Editor, &str)>,
12}
14}
13
15
14impl Prompt {
16impl Prompt {
15 pub fn new(
17 pub fn new(
16 prompt: String,
18 prompt: String,
17− completion_fn: impl FnMut(&str) -> Option<Vec<&str>> + 'static,
19+ mut completion_fn: impl FnMut(&str) -> Vec<String> + 'static,
18 callback_fn: impl FnMut(&mut Editor, &str) + 'static,
20 callback_fn: impl FnMut(&mut Editor, &str) + 'static,
19 ) -> Prompt {
21 ) -> Prompt {
20 Prompt {
22 Prompt {
21 prompt,
23 prompt,
22 line: String::new(),
24 line: String::new(),
23 cursor: 0,
25 cursor: 0,
26+ completion: completion_fn(""),
27+ should_close: false,
28+ completion_selection_index: None,
24 completion_fn: Box::new(completion_fn),
29 completion_fn: Box::new(completion_fn),
25 callback_fn: Box::new(callback_fn),
30 callback_fn: Box::new(callback_fn),
26 }
31 }
@@ -29,10 +34,12 @@ impl Prompt {
29 pub fn insert_char(&mut self, c: char) {
34 pub fn insert_char(&mut self, c: char) {
30 self.line.insert(self.cursor, c);
35 self.line.insert(self.cursor, c);
31 self.cursor += 1;
36 self.cursor += 1;
37+ self.completion = (self.completion_fn)(&self.line);
38+ self.exit_selection();
32 }
39 }
33
40
34 pub fn move_char_left(&mut self) {
41 pub fn move_char_left(&mut self) {
35− if self.cursor > 1 {
42+ if self.cursor > 0 {
36 self.cursor -= 1;
43 self.cursor -= 1;
37 }
44 }
38 }
45 }
@@ -55,7 +62,22 @@ impl Prompt {
55 if self.cursor > 0 {
62 if self.cursor > 0 {
56 self.line.remove(self.cursor - 1);
63 self.line.remove(self.cursor - 1);
57 self.cursor -= 1;
64 self.cursor -= 1;
65+ self.completion = (self.completion_fn)(&self.line);
58 }
66 }
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;
59 }
81 }
60
82
61 pub fn handle_input(&mut self, key_event: KeyEvent, editor: &mut Editor) {
83 pub fn handle_input(&mut self, key_event: KeyEvent, editor: &mut Editor) {
@@ -66,7 +88,7 @@ impl Prompt {
66 } => self.insert_char(c),
88 } => self.insert_char(c),
67 KeyEvent {
89 KeyEvent {
68 code: KeyCode::Esc, ..
90 code: KeyCode::Esc, ..
69− } => unimplemented!("Exit prompt!"),
91+ } => self.should_close = true,
70 KeyEvent {
92 KeyEvent {
71 code: KeyCode::Right,
93 code: KeyCode::Right,
72 ..
94 ..
@@ -85,12 +107,19 @@ impl Prompt {
85 } => self.move_start(),
107 } => self.move_start(),
86 KeyEvent {
108 KeyEvent {
87 code: KeyCode::Backspace,
109 code: KeyCode::Backspace,
88− ..
110+ modifiers: KeyModifiers::NONE,
89 } => self.delete_char_backwards(),
111 } => self.delete_char_backwards(),
90 KeyEvent {
112 KeyEvent {
91 code: KeyCode::Enter,
113 code: KeyCode::Enter,
92 ..
114 ..
93 } => (self.callback_fn)(editor, &self.line),
115 } => (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(),
94 _ => (),
123 _ => (),
95 }
124 }
96 }
125 }