1
0
Fork 0
mirror of https://github.com/helix-editor/helix synced 2024-06-07 09:46:06 +02:00

completion highlighting

This commit is contained in:
Jan Hrastnik 2020-10-20 23:02:02 +02:00
parent f3ddb8631f
commit 8f37c26f35
2 changed files with 43 additions and 12 deletions

View File

@ -243,7 +243,7 @@ pub fn render_prompt(&mut self, view: &View, prompt: &Prompt) {
for i in (3..7) {
self.surface.set_string(
0,
self.size.1 - i,
self.size.1 - i as u16,
" ".repeat(self.size.0 as usize),
self.text_color,
);
@ -253,12 +253,23 @@ pub fn render_prompt(&mut self, view: &View, prompt: &Prompt) {
view.theme.get("ui.statusline"),
);
for i in (0..completion.len()) {
self.surface.set_string(
1,
self.size.1 - 6 + i as u16,
&completion[i],
self.text_color,
)
if prompt.completion_selection_index.is_some()
&& i == prompt.completion_selection_index.unwrap()
{
self.surface.set_string(
1,
self.size.1 - 6 + i as u16,
&completion[i],
Style::default().bg(Color::Rgb(104, 060, 232)),
);
} else {
self.surface.set_string(
1,
self.size.1 - 6 + i as u16,
&completion[i],
self.text_color,
);
}
}
}
// render buffer text
@ -409,16 +420,24 @@ pub async fn event_loop(&mut self) {
..
}] = keys.as_slice()
{
let command_list = vec![
String::from("q"),
String::from("aaa"),
String::from("bbb"),
String::from("ccc"),
];
let prompt = Prompt::new(
":".to_owned(),
|_input: &str| {
let mut matches = vec![];
// TODO: i need this duplicate list right now to avoid borrow checker issues
let placeholder_list = vec![
String::from("q"),
String::from("aaa"),
String::from("bbb"),
String::from("ccc"),
];
let mut matches = vec![];
for command in placeholder_list {
if command.contains(_input) {
matches.push(command);
@ -433,6 +452,7 @@ pub async fn event_loop(&mut self) {
"q" => editor.should_close = true,
_ => (),
},
command_list,
);
self.prompt = Some(prompt);

View File

@ -8,6 +8,7 @@ pub struct Prompt {
pub cursor: usize,
pub completion: Option<Vec<String>>,
pub should_close: bool,
pub completion_selection_index: Option<usize>,
completion_fn: Box<dyn FnMut(&str) -> Option<Vec<String>>>,
callback_fn: Box<dyn FnMut(&mut Editor, &str)>,
}
@ -17,13 +18,15 @@ pub fn new(
prompt: String,
completion_fn: impl FnMut(&str) -> Option<Vec<String>> + 'static,
callback_fn: impl FnMut(&mut Editor, &str) + 'static,
command_list: Vec<String>,
) -> Prompt {
Prompt {
prompt,
line: String::new(),
cursor: 0,
completion: None,
completion: Some(command_list),
should_close: false,
completion_selection_index: None,
completion_fn: Box::new(completion_fn),
callback_fn: Box::new(callback_fn),
}
@ -32,6 +35,7 @@ pub fn new(
pub fn insert_char(&mut self, c: char) {
self.line.insert(self.cursor, c);
self.cursor += 1;
self.completion = (self.completion_fn)(&self.line);
}
pub fn move_char_left(&mut self) {
@ -58,6 +62,15 @@ pub fn delete_char_backwards(&mut self) {
if self.cursor > 0 {
self.line.remove(self.cursor - 1);
self.cursor -= 1;
self.completion = (self.completion_fn)(&self.line);
}
}
pub fn change_completion_selection(&mut self) {
if self.completion_selection_index.is_none() {
self.completion_selection_index = Some(0)
} else {
self.completion_selection_index = Some(self.completion_selection_index.unwrap() + 1)
}
}
@ -96,9 +109,7 @@ pub fn handle_input(&mut self, key_event: KeyEvent, editor: &mut Editor) {
} => (self.callback_fn)(editor, &self.line),
KeyEvent {
code: KeyCode::Tab, ..
} => {
self.completion = (self.completion_fn)(&self.line);
}
} => self.change_completion_selection(),
_ => (),
}
}