@@ -12,14 +12,24 @@ pub struct Prompt {
12 pub completion: Vec<String>,
12 pub completion: Vec<String>,
13 pub completion_selection_index: Option<usize>,
13 pub completion_selection_index: Option<usize>,
14 completion_fn: Box<dyn FnMut(&str) -> Vec<String>>,
14 completion_fn: Box<dyn FnMut(&str) -> Vec<String>>,
15− callback_fn: Box<dyn FnMut(&mut Editor, &str)>,
15+ callback_fn: Box<dyn FnMut(&mut Editor, &str, PromptEvent)>,
19+pub enum PromptEvent {
20+ /// The prompt input has been updated.
22+ /// Validate and finalize the change.
24+ /// Abort the change, reverting to the initial state.
18impl Prompt {
28impl Prompt {
19 pub fn new(
29 pub fn new(
20 prompt: String,
30 prompt: String,
21 mut completion_fn: impl FnMut(&str) -> Vec<String> + 'static,
31 mut completion_fn: impl FnMut(&str) -> Vec<String> + 'static,
22− callback_fn: impl FnMut(&mut Editor, &str) + 'static,
32+ callback_fn: impl FnMut(&mut Editor, &str, PromptEvent) + 'static,
23 ) -> Prompt {
33 ) -> Prompt {
@@ -160,10 +170,14 @@ impl Component for Prompt {
160 KeyEvent {
170 KeyEvent {
161 code: KeyCode::Char(c),
171 code: KeyCode::Char(c),
162 modifiers: KeyModifiers::NONE,
172 modifiers: KeyModifiers::NONE,
163− } => self.insert_char(c),
174+ self.insert_char(c);
175+ (self.callback_fn)(cx.editor, &self.line, PromptEvent::Update);
164 KeyEvent {
177 KeyEvent {
165 code: KeyCode::Esc, ..
178 code: KeyCode::Esc, ..
180+ (self.callback_fn)(cx.editor, &self.line, PromptEvent::Abort);
167 return close_fn;
181 return close_fn;
169 KeyEvent {
183 KeyEvent {
@@ -185,12 +199,15 @@ impl Component for Prompt {
185 KeyEvent {
199 KeyEvent {
186 code: KeyCode::Backspace,
200 code: KeyCode::Backspace,
187 modifiers: KeyModifiers::NONE,
201 modifiers: KeyModifiers::NONE,
188− } => self.delete_char_backwards(),
203+ self.delete_char_backwards();
204+ (self.callback_fn)(cx.editor, &self.line, PromptEvent::Update);
189 KeyEvent {
206 KeyEvent {
190 code: KeyCode::Enter,
207 code: KeyCode::Enter,
193− (self.callback_fn)(cx.editor, &self.line);
210+ (self.callback_fn)(cx.editor, &self.line, PromptEvent::Validate);
194 return close_fn;
211 return close_fn;
196 KeyEvent {
213 KeyEvent {