diff options
author | Blaž Hrastnik | 2021-03-21 05:13:49 +0000 |
---|---|---|
committer | Blaž Hrastnik | 2021-03-21 05:13:49 +0000 |
commit | a32806b4901dfbb85fdc6c043515033afcbdb9a7 (patch) | |
tree | 0888cb1be221964a5ac0486953fc9f35d53e4c7a /helix-term/src/ui/prompt.rs | |
parent | f29f01858d1b8c9e54b3293879796a4650823f60 (diff) |
Improve completion: src/<tab> will now correctly complete to src/main.rs
Diffstat (limited to 'helix-term/src/ui/prompt.rs')
-rw-r--r-- | helix-term/src/ui/prompt.rs | 23 |
1 files changed, 8 insertions, 15 deletions
diff --git a/helix-term/src/ui/prompt.rs b/helix-term/src/ui/prompt.rs index 19885c02..35890b4c 100644 --- a/helix-term/src/ui/prompt.rs +++ b/helix-term/src/ui/prompt.rs @@ -3,16 +3,15 @@ use crossterm::event::{Event, KeyCode, KeyEvent, KeyModifiers}; use helix_core::Position; use helix_view::Editor; use helix_view::Theme; -use std::borrow::Cow; -use std::string::String; +use std::{borrow::Cow, ops::RangeFrom}; pub struct Prompt { prompt: String, pub line: String, cursor: usize, - completion: Vec<Cow<'static, str>>, + completion: Vec<(RangeFrom<usize>, Cow<'static, str>)>, completion_selection_index: Option<usize>, - completion_fn: Box<dyn FnMut(&str) -> Vec<Cow<'static, str>>>, + completion_fn: Box<dyn FnMut(&str) -> Vec<(RangeFrom<usize>, Cow<'static, str>)>>, callback_fn: Box<dyn FnMut(&mut Editor, &str, PromptEvent)>, } @@ -29,7 +28,7 @@ pub enum PromptEvent { impl Prompt { pub fn new( prompt: String, - mut completion_fn: impl FnMut(&str) -> Vec<Cow<'static, str>> + 'static, + mut completion_fn: impl FnMut(&str) -> Vec<(RangeFrom<usize>, Cow<'static, str>)> + 'static, callback_fn: impl FnMut(&mut Editor, &str, PromptEvent) + 'static, ) -> Prompt { Prompt { @@ -85,15 +84,9 @@ impl Prompt { self.completion_selection_index.map(|i| i + 1).unwrap_or(0) % self.completion.len(); self.completion_selection_index = Some(index); - let item = &self.completion[index]; + let (range, item) = &self.completion[index]; - // replace the last arg - if let Some(pos) = self.line.rfind(' ') { - self.line.replace_range(pos + 1.., item); - } else { - // need toowned_clone_into nightly feature to reuse allocation - self.line = item.to_string(); - } + self.line.replace_range(range.clone(), item); self.move_end(); // TODO: recalculate completion when completion item is accepted, (Enter) @@ -135,7 +128,7 @@ impl Prompt { Rect::new(0, area.height - col_height - 2, area.width, col_height), theme.get("ui.statusline"), ); - for (i, command) in self.completion.iter().enumerate() { + for (i, (_range, completion)) in self.completion.iter().enumerate() { let color = if self.completion_selection_index.is_some() && i == self.completion_selection_index.unwrap() { @@ -146,7 +139,7 @@ impl Prompt { surface.set_stringn( 1 + col * BASE_WIDTH, area.height - col_height - 2 + row, - &command, + &completion, BASE_WIDTH as usize - 1, color, ); |