diff options
author | Kevin Sjöberg | 2021-06-05 21:20:34 +0000 |
---|---|---|
committer | Blaž Hrastnik | 2021-06-06 12:48:19 +0000 |
commit | a4ff8cdd8a7f2fd39eb15ce6f591e4cea9868c39 (patch) | |
tree | d3aac00ea3aff9697f464e4d16f8c11aff517f60 /helix-term/src/ui | |
parent | 145bc1970a49a7056b34d3a6f8278a6886acf373 (diff) |
Allow moving backwards in completions
Diffstat (limited to 'helix-term/src/ui')
-rw-r--r-- | helix-term/src/ui/prompt.rs | 26 |
1 files changed, 22 insertions, 4 deletions
diff --git a/helix-term/src/ui/prompt.rs b/helix-term/src/ui/prompt.rs index 0a88aa90..95c42fbc 100644 --- a/helix-term/src/ui/prompt.rs +++ b/helix-term/src/ui/prompt.rs @@ -28,6 +28,11 @@ pub enum PromptEvent { Abort, } +pub enum CompletionDirection { + Forward, + Backward, +} + impl Prompt { pub fn new( prompt: String, @@ -80,11 +85,20 @@ impl Prompt { self.exit_selection(); } - pub fn change_completion_selection(&mut self) { + pub fn change_completion_selection(&mut self, direction: CompletionDirection) { if self.completion.is_empty() { return; } - let index = self.selection.map_or(0, |i| i + 1) % self.completion.len(); + + let index = match direction { + CompletionDirection::Forward => { + self.selection.map_or(0, |i| i + 1) % self.completion.len() + } + CompletionDirection::Backward => { + (self.selection.unwrap_or(0) + self.completion.len() - 1) % self.completion.len() + } + }; + self.selection = Some(index); let (range, item) = &self.completion[index]; @@ -92,8 +106,8 @@ impl Prompt { self.line.replace_range(range.clone(), item); self.move_end(); - // TODO: recalculate completion when completion item is accepted, (Enter) } + pub fn exit_selection(&mut self) { self.selection = None; } @@ -263,7 +277,11 @@ impl Component for Prompt { } KeyEvent { code: KeyCode::Tab, .. - } => self.change_completion_selection(), + } => self.change_completion_selection(CompletionDirection::Forward), + KeyEvent { + code: KeyCode::BackTab, + .. + } => self.change_completion_selection(CompletionDirection::Backward), KeyEvent { code: KeyCode::Char('q'), modifiers: KeyModifiers::CONTROL, |