on master help sign in
wip: Use prompt for interactive commands.
commited6a4c4bd240630355d1c61a1c05c4e22bf62a86 authorBlaž Hrastnik <blaz@mxxn.io> dateMon 14 Dec 2020, 14:12 (6 years ago) parent07801b6
3 files changed+35−5
helix-term/src/commands.rs+33 helix-term/src/keymap.rs+1 helix-term/src/ui/prompt.rs+1−5
helix-term/src/commands.rs+33
@@ -248,6 +248,39 @@ pub fn extend_line_down(cx: &mut Context) {
248248 cx.view.doc.set_selection(selection);
249249}
250250
251+pub fn split_selection(cx: &mut Context) {
252+ // TODO: this needs to store initial selection state, revert on esc, confirm on enter
253+ // needs to also call the callback function per input change, not just final time.
254+ // could cheat and put it into completion_fn
255+ //
256+ // kakoune does it like this:
257+ // # save state to register
258+ // {
259+ // # restore state from register
260+ // # if event == abort, return early
261+ // # add to history if enabled
262+ // # update state
263+ // }
264+
265+ let prompt = Prompt::new(
266+ "split:".to_string(),
267+ |input: &str| Vec::new(), // TODO: use Option here?
268+ |editor: &mut Editor, input: &str| {
269+ match Regex::new(input) {
270+ Ok(regex) => {
271+ 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+ }
276+ Err(_) => (), // TODO: mark command line as error
277+ }
278+ },
279+ );
280+
281+ unimplemented!()
282+}
283+
251284pub fn split_selection_on_newline(cx: &mut Context) {
252285 let text = &cx.view.doc.text().slice(..);
253286 // only compile the regex once
helix-term/src/keymap.rs+1
@@ -156,6 +156,7 @@ pub fn default() -> Keymaps {
156156 vec![key!('o')] => commands::open_below,
157157 vec![key!('d')] => commands::delete_selection,
158158 vec![key!('c')] => commands::change_selection,
159+ vec![shift!('S')] => commands::split_selection,
159160 vec![key!('s')] => commands::split_selection_on_newline,
160161 vec![key!(';')] => commands::collapse_selection,
161162 // TODO should be alt(;)
helix-term/src/ui/prompt.rs+1−5
@@ -10,7 +10,6 @@ pub struct Prompt {
1010 pub line: String,
1111 pub cursor: usize,
1212 pub completion: Vec<String>,
13− pub should_close: bool,
1413 pub completion_selection_index: Option<usize>,
1514 completion_fn: Box<dyn FnMut(&str) -> Vec<String>>,
1615 callback_fn: Box<dyn FnMut(&mut Editor, &str)>,
@@ -27,7 +26,6 @@ impl Prompt {
2726 line: String::new(),
2827 cursor: 0,
2928 completion: completion_fn(""),
30− should_close: false,
3129 completion_selection_index: None,
3230 completion_fn: Box::new(completion_fn),
3331 callback_fn: Box::new(callback_fn),
@@ -42,9 +40,7 @@ impl Prompt {
4240 }
4341
4442 pub fn move_char_left(&mut self) {
45− if self.cursor > 0 {
46− self.cursor -= 1;
47− }
43+ self.cursor = self.cursor.saturating_sub(1)
4844 }
4945
5046 pub fn move_char_right(&mut self) {