on master help sign in
Make the select prompt interactive.
commit8f0b28aeb872797e4be3f07575e628f5f93e74e0 authorBlaž Hrastnik <blaz@mxxn.io> dateTue 15 Dec 2020, 19:29 (6 years ago) parent1a843b6
3 files changed+59−17
helix-term/src/commands.rs+36−11 helix-term/src/ui/mod.rs+1−1 helix-term/src/ui/prompt.rs+22−5
helix-term/src/commands.rs+36−11
@@ -10,7 +10,7 @@ use helix_core::{
1010use once_cell::sync::Lazy;
1111
1212use crate::compositor::Compositor;
13−use crate::ui::Prompt;
13+use crate::ui::{Prompt, PromptEvent};
1414
1515use helix_view::{
1616 document::Mode,
@@ -262,18 +262,37 @@ pub fn split_selection(cx: &mut Context) {
262262 // # update state
263263 // }
264264
265+ let snapshot = cx.view.doc.state.clone();
266+
265267 let prompt = Prompt::new(
266268 "split:".to_string(),
267269 |input: &str| Vec::new(), // this is fine because Vec::new() doesn't allocate
268− |editor: &mut Editor, input: &str| {
269− match Regex::new(input) {
270− Ok(regex) => {
270+ move |editor: &mut Editor, input: &str, event: PromptEvent| {
271+ match event {
272+ PromptEvent::Abort => {
273+ // revert state
271274 let view = editor.view_mut().unwrap();
272− let text = &view.doc.text().slice(..);
273− let selection = selection::split_on_matches(text, view.doc.selection(), &regex);
274− view.doc.set_selection(selection);
275+ view.doc.state = snapshot.clone();
276+ }
277+ PromptEvent::Validate => {
278+ //
279+ }
280+ PromptEvent::Update => {
281+ match Regex::new(input) {
282+ Ok(regex) => {
283+ let view = editor.view_mut().unwrap();
284+
285+ // revert state to what it was before the last update
286+ view.doc.state = snapshot.clone();
287+
288+ let text = &view.doc.text().slice(..);
289+ let selection =
290+ selection::split_on_matches(text, view.doc.selection(), &regex);
291+ view.doc.set_selection(selection);
292+ }
293+ Err(_) => (), // TODO: mark command line as error
294+ }
275295 }
276− Err(_) => (), // TODO: mark command line as error
277296 }
278297 },
279298 );
@@ -416,9 +435,15 @@ pub fn command_mode(cx: &mut Context) {
416435 .filter(|command| command.contains(_input))
417436 .collect()
418437 }, // completion
419− |editor: &mut Editor, input: &str| match input {
420− "q" => editor.should_close = true,
421− _ => (),
438+ |editor: &mut Editor, input: &str, event: PromptEvent| {
439+ if event != PromptEvent::Validate {
440+ return;
441+ }
442+
443+ match input {
444+ "q" => editor.should_close = true,
445+ _ => (),
446+ }
422447 },
423448 );
424449 compositor.push(Box::new(prompt));
helix-term/src/ui/mod.rs+1−1
@@ -2,7 +2,7 @@ mod editor;
22mod prompt;
33
44pub use editor::EditorView;
5−pub use prompt::Prompt;
5+pub use prompt::{Prompt, PromptEvent};
66
77pub use tui::layout::Rect;
88pub use tui::style::{Color, Modifier, Style};
helix-term/src/ui/prompt.rs+22−5
@@ -12,14 +12,24 @@ pub struct Prompt {
1212 pub completion: Vec<String>,
1313 pub completion_selection_index: Option<usize>,
1414 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)>,
16+}
17+
18+#[derive(PartialEq)]
19+pub enum PromptEvent {
20+ /// The prompt input has been updated.
21+ Update,
22+ /// Validate and finalize the change.
23+ Validate,
24+ /// Abort the change, reverting to the initial state.
25+ Abort,
1626}
1727
1828impl Prompt {
1929 pub fn new(
2030 prompt: String,
2131 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,
2333 ) -> Prompt {
2434 Prompt {
2535 prompt,
@@ -160,10 +170,14 @@ impl Component for Prompt {
160170 KeyEvent {
161171 code: KeyCode::Char(c),
162172 modifiers: KeyModifiers::NONE,
163− } => self.insert_char(c),
173+ } => {
174+ self.insert_char(c);
175+ (self.callback_fn)(cx.editor, &self.line, PromptEvent::Update);
176+ }
164177 KeyEvent {
165178 code: KeyCode::Esc, ..
166179 } => {
180+ (self.callback_fn)(cx.editor, &self.line, PromptEvent::Abort);
167181 return close_fn;
168182 }
169183 KeyEvent {
@@ -185,12 +199,15 @@ impl Component for Prompt {
185199 KeyEvent {
186200 code: KeyCode::Backspace,
187201 modifiers: KeyModifiers::NONE,
188− } => self.delete_char_backwards(),
202+ } => {
203+ self.delete_char_backwards();
204+ (self.callback_fn)(cx.editor, &self.line, PromptEvent::Update);
205+ }
189206 KeyEvent {
190207 code: KeyCode::Enter,
191208 ..
192209 } => {
193− (self.callback_fn)(cx.editor, &self.line);
210+ (self.callback_fn)(cx.editor, &self.line, PromptEvent::Validate);
194211 return close_fn;
195212 }
196213 KeyEvent {