mirror of
https://github.com/helix-editor/helix.git
synced 2025-01-19 21:47:07 +04:00
Allow moving backwards in completions
This commit is contained in:
parent
145bc1970a
commit
a4ff8cdd8a
@ -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 @@ pub fn delete_char_backwards(&mut self) {
|
|||||||
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 @@ pub fn change_completion_selection(&mut self) {
|
|||||||
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 @@ fn handle_event(&mut self, event: Event, cx: &mut Context) -> EventResult {
|
|||||||
}
|
}
|
||||||
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 New Issue
Block a user