@@ -3,15 +3,16 @@ use crossterm::event::{Event, KeyCode, KeyEvent, KeyModifiers};
3use helix_core::Position;
3use helix_core::Position;
4use helix_view::Editor;
4use helix_view::Editor;
5use helix_view::Theme;
5use helix_view::Theme;
6use std::string::String;
7use std::string::String;
8pub struct Prompt {
9pub struct Prompt {
9 pub prompt: String,
10 pub prompt: String,
10 pub line: String,
11 pub line: String,
11 pub cursor: usize,
12 pub cursor: usize,
12− pub completion: Vec<String>,
13+ pub completion: Vec<Cow<'static, str>>,
13 pub completion_selection_index: Option<usize>,
14 pub completion_selection_index: Option<usize>,
14− completion_fn: Box<dyn FnMut(&str) -> Vec<String>>,
15+ completion_fn: Box<dyn FnMut(&str) -> Vec<Cow<'static, str>>>,
15 callback_fn: Box<dyn FnMut(&mut Editor, &str, PromptEvent)>,
16 callback_fn: Box<dyn FnMut(&mut Editor, &str, PromptEvent)>,
@@ -28,7 +29,7 @@ pub enum PromptEvent {
28impl Prompt {
29impl Prompt {
29 pub fn new(
30 pub fn new(
30 prompt: String,
31 prompt: String,
31− mut completion_fn: impl FnMut(&str) -> Vec<String> + 'static,
32+ mut completion_fn: impl FnMut(&str) -> Vec<Cow<'static, str>> + 'static,
32 callback_fn: impl FnMut(&mut Editor, &str, PromptEvent) + 'static,
33 callback_fn: impl FnMut(&mut Editor, &str, PromptEvent) + 'static,
33 ) -> Prompt {
34 ) -> Prompt {
@@ -83,7 +84,19 @@ impl Prompt {
83 let index =
84 let index =
84 self.completion_selection_index.map(|i| i + 1).unwrap_or(0) % self.completion.len();
85 self.completion_selection_index.map(|i| i + 1).unwrap_or(0) % self.completion.len();
85 self.completion_selection_index = Some(index);
86 self.completion_selection_index = Some(index);
86− self.line = self.completion[index].clone();
88+ let item = &self.completion[index];
90+ // replace the last arg
91+ if let Some(pos) = self.line.rfind(' ') {
92+ self.line.replace_range(pos + 1.., item);
94+ // need toowned_clone_into nightly feature to reuse allocation
95+ self.line = item.to_string();
99+ // TODO: recalculate completion when completion item is accepted, (Enter)
88 pub fn exit_selection(&mut self) {
101 pub fn exit_selection(&mut self) {
89 self.completion_selection_index = None;
102 self.completion_selection_index = None;
@@ -175,9 +188,14 @@ impl Component for Prompt {
177 match event {
190 match event {
191+ // char or shift char
178 KeyEvent {
192 KeyEvent {
179 code: KeyCode::Char(c),
193 code: KeyCode::Char(c),
180 modifiers: KeyModifiers::NONE,
194 modifiers: KeyModifiers::NONE,
197+ code: KeyCode::Char(c),
198+ modifiers: KeyModifiers::SHIFT,
182 self.insert_char(c);
200 self.insert_char(c);
183 (self.callback_fn)(cx.editor, &self.line, PromptEvent::Update);
201 (self.callback_fn)(cx.editor, &self.line, PromptEvent::Update);