diff options
-rw-r--r-- | helix-term/src/commands.rs | 33 | ||||
-rw-r--r-- | helix-term/src/keymap.rs | 1 | ||||
-rw-r--r-- | helix-term/src/ui/prompt.rs | 6 |
3 files changed, 35 insertions, 5 deletions
diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index b345d2e8..7001aa11 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -248,6 +248,39 @@ pub fn extend_line_down(cx: &mut Context) { cx.view.doc.set_selection(selection); } +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. + // could cheat and put it into completion_fn + // + // kakoune does it like this: + // # save state to register + // { + // # restore state from register + // # if event == abort, return early + // # add to history if enabled + // # update state + // } + + let prompt = Prompt::new( + "split:".to_string(), + |input: &str| Vec::new(), // TODO: use Option here? + |editor: &mut Editor, input: &str| { + match Regex::new(input) { + Ok(regex) => { + let view = editor.view_mut().unwrap(); + let text = &view.doc.text().slice(..); + let selection = selection::split_on_matches(text, view.doc.selection(), ®ex); + view.doc.set_selection(selection); + } + Err(_) => (), // TODO: mark command line as error + } + }, + ); + + unimplemented!() +} + pub fn split_selection_on_newline(cx: &mut Context) { let text = &cx.view.doc.text().slice(..); // only compile the regex once diff --git a/helix-term/src/keymap.rs b/helix-term/src/keymap.rs index af46f7a4..304a173c 100644 --- a/helix-term/src/keymap.rs +++ b/helix-term/src/keymap.rs @@ -156,6 +156,7 @@ pub fn default() -> Keymaps { vec![key!('o')] => commands::open_below, vec![key!('d')] => commands::delete_selection, vec![key!('c')] => commands::change_selection, + vec![shift!('S')] => commands::split_selection, vec![key!('s')] => commands::split_selection_on_newline, vec![key!(';')] => commands::collapse_selection, // TODO should be alt(;) diff --git a/helix-term/src/ui/prompt.rs b/helix-term/src/ui/prompt.rs index ce00a129..d1d92975 100644 --- a/helix-term/src/ui/prompt.rs +++ b/helix-term/src/ui/prompt.rs @@ -10,7 +10,6 @@ pub struct Prompt { pub line: String, pub cursor: usize, pub completion: Vec<String>, - pub should_close: bool, pub completion_selection_index: Option<usize>, completion_fn: Box<dyn FnMut(&str) -> Vec<String>>, callback_fn: Box<dyn FnMut(&mut Editor, &str)>, @@ -27,7 +26,6 @@ impl Prompt { line: String::new(), cursor: 0, completion: completion_fn(""), - should_close: false, completion_selection_index: None, completion_fn: Box::new(completion_fn), callback_fn: Box::new(callback_fn), @@ -42,9 +40,7 @@ impl Prompt { } pub fn move_char_left(&mut self) { - if self.cursor > 0 { - self.cursor -= 1; - } + self.cursor = self.cursor.saturating_sub(1) } pub fn move_char_right(&mut self) { |