Allow moving backwards in completions
This commit is contained in:
parent
145bc1970a
commit
a4ff8cdd8a
1 changed files with 22 additions and 4 deletions
|
@ -28,6 +28,11 @@ pub enum PromptEvent {
|
||||||
Abort,
|
Abort,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub enum CompletionDirection {
|
||||||
|
Forward,
|
||||||
|
Backward,
|
||||||
|
}
|
||||||
|
|
||||||
impl Prompt {
|
impl Prompt {
|
||||||
pub fn new(
|
pub fn new(
|
||||||
prompt: String,
|
prompt: String,
|
||||||
|
@ -80,11 +85,20 @@ impl Prompt {
|
||||||
self.exit_selection();
|
self.exit_selection();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn change_completion_selection(&mut self) {
|
pub fn change_completion_selection(&mut self, direction: CompletionDirection) {
|
||||||
if self.completion.is_empty() {
|
if self.completion.is_empty() {
|
||||||
return;
|
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);
|
self.selection = Some(index);
|
||||||
|
|
||||||
let (range, item) = &self.completion[index];
|
let (range, item) = &self.completion[index];
|
||||||
|
@ -92,8 +106,8 @@ impl Prompt {
|
||||||
self.line.replace_range(range.clone(), item);
|
self.line.replace_range(range.clone(), item);
|
||||||
|
|
||||||
self.move_end();
|
self.move_end();
|
||||||
// TODO: recalculate completion when completion item is accepted, (Enter)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn exit_selection(&mut self) {
|
pub fn exit_selection(&mut self) {
|
||||||
self.selection = None;
|
self.selection = None;
|
||||||
}
|
}
|
||||||
|
@ -263,7 +277,11 @@ impl Component for Prompt {
|
||||||
}
|
}
|
||||||
KeyEvent {
|
KeyEvent {
|
||||||
code: KeyCode::Tab, ..
|
code: KeyCode::Tab, ..
|
||||||
} => self.change_completion_selection(),
|
} => self.change_completion_selection(CompletionDirection::Forward),
|
||||||
|
KeyEvent {
|
||||||
|
code: KeyCode::BackTab,
|
||||||
|
..
|
||||||
|
} => self.change_completion_selection(CompletionDirection::Backward),
|
||||||
KeyEvent {
|
KeyEvent {
|
||||||
code: KeyCode::Char('q'),
|
code: KeyCode::Char('q'),
|
||||||
modifiers: KeyModifiers::CONTROL,
|
modifiers: KeyModifiers::CONTROL,
|
||||||
|
|
Loading…
Reference in a new issue