aboutsummaryrefslogtreecommitdiff
path: root/helix-view
diff options
context:
space:
mode:
authorBlaž Hrastnik2020-12-02 00:44:51 +0000
committerGitHub2020-12-02 00:44:51 +0000
commit2e12fc9a7cd221bb7b5f4b5c1ece599089770ccb (patch)
treeb419e4ac033207d20ec6ca176b20da072944f996 /helix-view
parentbc2c652fe885c7e7953224268688a6d7b6dea80e (diff)
parent1a3c647adf1f252478bf38537bde87f7e5f14ab0 (diff)
Merge pull request #4 from helix-editor/completion-suggestion
Completion suggestion
Diffstat (limited to 'helix-view')
-rw-r--r--helix-view/src/commands.rs17
-rw-r--r--helix-view/src/prompt.rs43
2 files changed, 39 insertions, 21 deletions
diff --git a/helix-view/src/commands.rs b/helix-view/src/commands.rs
index 33fc06e4..1d7737f0 100644
--- a/helix-view/src/commands.rs
+++ b/helix-view/src/commands.rs
@@ -307,23 +307,12 @@ pub fn append_mode(view: &mut View, _count: usize) {
})
}
-pub fn command_mode(_view: &mut View, _count: usize) {
- use crate::Editor;
-
- let prompt = Prompt::new(
- ":".to_owned(),
- |_input: &str| None, // completion
- |editor: &mut Editor, input: &str| match input {
- "q" => editor.should_close = true,
- _ => (),
- },
- );
+// TODO: I, A, o and O can share a lot of the primitives.
- // set_prompt(prompt)
+pub fn command_mode(_view: &mut View, _count: usize) {
+ unimplemented!()
}
-// TODO: I, A, o and O can share a lot of the primitives.
-
// calculate line numbers for each selection range
fn selection_lines(state: &State) -> Vec<usize> {
let mut lines = state
diff --git a/helix-view/src/prompt.rs b/helix-view/src/prompt.rs
index a3feaeec..e2a9c80d 100644
--- a/helix-view/src/prompt.rs
+++ b/helix-view/src/prompt.rs
@@ -1,5 +1,4 @@
-use crate::commands;
-use crate::{Editor, View};
+use crate::Editor;
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use std::string::String;
@@ -7,20 +6,26 @@ pub struct Prompt {
pub prompt: String,
pub line: String,
pub cursor: usize,
- completion_fn: Box<dyn FnMut(&str) -> Option<Vec<&str>>>,
+ pub completion: Vec<String>,
+ pub should_close: bool,
+ pub completion_selection_index: Option<usize>,
+ completion_fn: Box<dyn FnMut(&str) -> Vec<String>>,
callback_fn: Box<dyn FnMut(&mut Editor, &str)>,
}
impl Prompt {
pub fn new(
prompt: String,
- completion_fn: impl FnMut(&str) -> Option<Vec<&str>> + 'static,
+ mut completion_fn: impl FnMut(&str) -> Vec<String> + 'static,
callback_fn: impl FnMut(&mut Editor, &str) + 'static,
) -> Prompt {
Prompt {
prompt,
line: String::new(),
cursor: 0,
+ completion: completion_fn(""),
+ should_close: false,
+ completion_selection_index: None,
completion_fn: Box::new(completion_fn),
callback_fn: Box::new(callback_fn),
}
@@ -29,10 +34,12 @@ impl Prompt {
pub fn insert_char(&mut self, c: char) {
self.line.insert(self.cursor, c);
self.cursor += 1;
+ self.completion = (self.completion_fn)(&self.line);
+ self.exit_selection();
}
pub fn move_char_left(&mut self) {
- if self.cursor > 1 {
+ if self.cursor > 0 {
self.cursor -= 1;
}
}
@@ -55,7 +62,22 @@ impl Prompt {
if self.cursor > 0 {
self.line.remove(self.cursor - 1);
self.cursor -= 1;
+ self.completion = (self.completion_fn)(&self.line);
}
+ self.exit_selection();
+ }
+
+ pub fn change_completion_selection(&mut self) {
+ if self.completion.is_empty() {
+ return;
+ }
+ let index =
+ self.completion_selection_index.map(|i| i + 1).unwrap_or(0) % self.completion.len();
+ self.completion_selection_index = Some(index);
+ self.line = self.completion[index].clone();
+ }
+ pub fn exit_selection(&mut self) {
+ self.completion_selection_index = None;
}
pub fn handle_input(&mut self, key_event: KeyEvent, editor: &mut Editor) {
@@ -66,7 +88,7 @@ impl Prompt {
} => self.insert_char(c),
KeyEvent {
code: KeyCode::Esc, ..
- } => unimplemented!("Exit prompt!"),
+ } => self.should_close = true,
KeyEvent {
code: KeyCode::Right,
..
@@ -85,12 +107,19 @@ impl Prompt {
} => self.move_start(),
KeyEvent {
code: KeyCode::Backspace,
- ..
+ modifiers: KeyModifiers::NONE,
} => self.delete_char_backwards(),
KeyEvent {
code: KeyCode::Enter,
..
} => (self.callback_fn)(editor, &self.line),
+ KeyEvent {
+ code: KeyCode::Tab, ..
+ } => self.change_completion_selection(),
+ KeyEvent {
+ code: KeyCode::Char('q'),
+ modifiers: KeyModifiers::CONTROL,
+ } => self.exit_selection(),
_ => (),
}
}