on master help sign in
Allow moving backwards in completions
a4ff8cdKevin Sjöberg
helix-term/src/ui/prompt.rs+22−4
@@ -28,6 +28,11 @@ pub enum PromptEvent {
28 Abort,
28 Abort,
29}
29}
30
30
31+pub enum CompletionDirection {
32+ Forward,
33+ Backward,
34+}
35+
31impl Prompt {
36impl Prompt {
32 pub fn new(
37 pub fn new(
33 prompt: String,
38 prompt: String,
@@ -80,11 +85,20 @@ impl Prompt {
80 self.exit_selection();
85 self.exit_selection();
81 }
86 }
82
87
83− pub fn change_completion_selection(&mut self) {
88+ pub fn change_completion_selection(&mut self, direction: CompletionDirection) {
84 if self.completion.is_empty() {
89 if self.completion.is_empty() {
85 return;
90 return;
86 }
91 }
92+
93+ let index = match direction {
94+ CompletionDirection::Forward => {
87− let index = self.selection.map_or(0, |i| i + 1) % self.completion.len();
95+ self.selection.map_or(0, |i| i + 1) % self.completion.len()
96+ }
97+ CompletionDirection::Backward => {
98+ (self.selection.unwrap_or(0) + self.completion.len() - 1) % self.completion.len()
99+ }
100+ };
101+
88 self.selection = Some(index);
102 self.selection = Some(index);
89
103
90 let (range, item) = &self.completion[index];
104 let (range, item) = &self.completion[index];
@@ -92,8 +106,8 @@ impl Prompt {
92 self.line.replace_range(range.clone(), item);
106 self.line.replace_range(range.clone(), item);
93
107
94 self.move_end();
108 self.move_end();
95− // TODO: recalculate completion when completion item is accepted, (Enter)
96 }
109 }
110+
97 pub fn exit_selection(&mut self) {
111 pub fn exit_selection(&mut self) {
98 self.selection = None;
112 self.selection = None;
99 }
113 }
@@ -263,7 +277,11 @@ impl Component for Prompt {
263 }
277 }
264 KeyEvent {
278 KeyEvent {
265 code: KeyCode::Tab, ..
279 code: KeyCode::Tab, ..
266− } => self.change_completion_selection(),
280+ } => self.change_completion_selection(CompletionDirection::Forward),
281+ KeyEvent {
282+ code: KeyCode::BackTab,
283+ ..
284+ } => self.change_completion_selection(CompletionDirection::Backward),
267 KeyEvent {
285 KeyEvent {
268 code: KeyCode::Char('q'),
286 code: KeyCode::Char('q'),
269 modifiers: KeyModifiers::CONTROL,
287 modifiers: KeyModifiers::CONTROL,