From 2bea5db7bdb3ad3fa029df830d824cd5c26a153f Mon Sep 17 00:00:00 2001 From: Blaž Hrastnik Date: Fri, 22 Jan 2021 17:13:14 +0900 Subject: commands: Implement select_on_matches. --- helix-term/src/commands.rs | 50 +++++++++++++++++----------------------------- helix-term/src/keymap.rs | 19 +++++++++--------- helix-term/src/ui/mod.rs | 41 +++++++++++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+), 41 deletions(-) (limited to 'helix-term/src') diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 4a461239..ff8704d8 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -282,6 +282,19 @@ pub fn extend_line_down(cx: &mut Context) { doc.set_selection(selection); } +pub fn select_regex(cx: &mut Context) { + let prompt = ui::regex_prompt(cx, "select:".to_string(), |doc, regex| { + let text = &doc.text().slice(..); + let selection = selection::select_on_matches(text, doc.selection(), ®ex); + doc.set_selection(selection); + }); + cx.callback = Some(Box::new( + move |compositor: &mut Compositor, editor: &mut Editor| { + compositor.push(Box::new(prompt)); + }, + )); +} + pub fn split_selection(cx: &mut Context) { // TODO: this needs to store initial selection state, revert on esc, confirm on enter // needs to also call the callback function per input change, not just final time. @@ -298,38 +311,11 @@ pub fn split_selection(cx: &mut Context) { let snapshot = cx.doc().state.clone(); - let prompt = Prompt::new( - "split:".to_string(), - |input: &str| Vec::new(), // this is fine because Vec::new() doesn't allocate - move |editor: &mut Editor, input: &str, event: PromptEvent| { - match event { - PromptEvent::Abort => { - // revert state - let doc = &mut editor.view_mut().doc; - doc.state = snapshot.clone(); - } - PromptEvent::Validate => { - // - } - PromptEvent::Update => { - match Regex::new(input) { - Ok(regex) => { - let doc = &mut editor.view_mut().doc; - - // revert state to what it was before the last update - doc.state = snapshot.clone(); - - let text = &doc.text().slice(..); - let selection = - selection::split_on_matches(text, doc.selection(), ®ex); - doc.set_selection(selection); - } - Err(_err) => (), // TODO: mark command line as error - } - } - } - }, - ); + let prompt = ui::regex_prompt(cx, "split:".to_string(), |doc, regex| { + let text = &doc.text().slice(..); + let selection = selection::split_on_matches(text, doc.selection(), ®ex); + doc.set_selection(selection); + }); cx.callback = Some(Box::new( move |compositor: &mut Compositor, editor: &mut Editor| { diff --git a/helix-term/src/keymap.rs b/helix-term/src/keymap.rs index 440a3efb..a27abf09 100644 --- a/helix-term/src/keymap.rs +++ b/helix-term/src/keymap.rs @@ -121,14 +121,14 @@ macro_rules! ctrl { }; } -// macro_rules! alt { -// ($ch:expr) => { -// Key { -// code: KeyCode::Char($ch), -// modifiers: Modifiers::ALT, -// } -// }; -// } +macro_rules! alt { + ($ch:expr) => { + Key { + code: KeyCode::Char($ch), + modifiers: Modifiers::ALT, + } + }; +} pub fn default() -> Keymaps { hashmap!( @@ -159,7 +159,8 @@ pub fn default() -> Keymaps { vec![key!('o')] => commands::open_below, vec![key!('d')] => commands::delete_selection, vec![key!('c')] => commands::change_selection, - vec![key!('s')] => commands::split_selection_on_newline, + vec![key!('s')] => commands::select_regex, + vec![alt!('s')] => commands::split_selection_on_newline, vec![shift!('S')] => commands::split_selection, vec![key!(';')] => commands::collapse_selection, // TODO should be alt(;) diff --git a/helix-term/src/ui/mod.rs b/helix-term/src/ui/mod.rs index 5ef967b0..bd40b249 100644 --- a/helix-term/src/ui/mod.rs +++ b/helix-term/src/ui/mod.rs @@ -9,12 +9,53 @@ pub use prompt::{Prompt, PromptEvent}; pub use tui::layout::Rect; pub use tui::style::{Color, Modifier, Style}; +use helix_core::regex::Regex; +use helix_view::{Document, Editor}; + // TODO: temp #[inline(always)] pub fn text_color() -> Style { Style::default().fg(Color::Rgb(219, 191, 239)) // lilac } +pub fn regex_prompt( + cx: &mut crate::commands::Context, + prompt: String, + fun: impl Fn(&mut Document, Regex) + 'static, +) -> Prompt { + let snapshot = cx.doc().state.clone(); + + Prompt::new( + prompt, + |input: &str| Vec::new(), // this is fine because Vec::new() doesn't allocate + move |editor: &mut Editor, input: &str, event: PromptEvent| { + match event { + PromptEvent::Abort => { + // revert state + let doc = &mut editor.view_mut().doc; + doc.state = snapshot.clone(); + } + PromptEvent::Validate => { + // + } + PromptEvent::Update => { + match Regex::new(input) { + Ok(regex) => { + let doc = &mut editor.view_mut().doc; + + // revert state to what it was before the last update + doc.state = snapshot.clone(); + + fun(doc, regex); + } + Err(_err) => (), // TODO: mark command line as error + } + } + } + }, + ) +} + use std::path::{Path, PathBuf}; pub fn file_picker(root: &str, ex: &'static smol::Executor) -> Picker { use ignore::Walk; -- cgit v1.2.3-70-g09d2