From a4ff8cdd8a7f2fd39eb15ce6f591e4cea9868c39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kevin=20Sj=C3=B6berg?= Date: Sat, 5 Jun 2021 23:20:34 +0200 Subject: [PATCH] Allow moving backwards in completions --- helix-term/src/ui/prompt.rs | 26 ++++++++++++++++++++++---- 1 file 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,