on master help sign in
wip: Use prompt for interactive commands.
ed6a4c4Blaž Hrastnik
helix-term/src/commands.rs+33
@@ -248,6 +248,39 @@ pub fn extend_line_down(cx: &mut Context) {
248 cx.view.doc.set_selection(selection);
248 cx.view.doc.set_selection(selection);
249}
249}
250
250
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+
251pub fn split_selection_on_newline(cx: &mut Context) {
284pub fn split_selection_on_newline(cx: &mut Context) {
252 let text = &cx.view.doc.text().slice(..);
285 let text = &cx.view.doc.text().slice(..);
253 // only compile the regex once
286 // only compile the regex once
helix-term/src/keymap.rs+1
@@ -156,6 +156,7 @@ pub fn default() -> Keymaps {
156 vec![key!('o')] => commands::open_below,
156 vec![key!('o')] => commands::open_below,
157 vec![key!('d')] => commands::delete_selection,
157 vec![key!('d')] => commands::delete_selection,
158 vec![key!('c')] => commands::change_selection,
158 vec![key!('c')] => commands::change_selection,
159+ vec![shift!('S')] => commands::split_selection,
159 vec![key!('s')] => commands::split_selection_on_newline,
160 vec![key!('s')] => commands::split_selection_on_newline,
160 vec![key!(';')] => commands::collapse_selection,
161 vec![key!(';')] => commands::collapse_selection,
161 // TODO should be alt(;)
162 // TODO should be alt(;)
helix-term/src/ui/prompt.rs+1−5
@@ -10,7 +10,6 @@ pub struct Prompt {
10 pub line: String,
10 pub line: String,
11 pub cursor: usize,
11 pub cursor: usize,
12 pub completion: Vec<String>,
12 pub completion: Vec<String>,
13− pub should_close: bool,
14 pub completion_selection_index: Option<usize>,
13 pub completion_selection_index: Option<usize>,
15 completion_fn: Box<dyn FnMut(&str) -> Vec<String>>,
14 completion_fn: Box<dyn FnMut(&str) -> Vec<String>>,
16 callback_fn: Box<dyn FnMut(&mut Editor, &str)>,
15 callback_fn: Box<dyn FnMut(&mut Editor, &str)>,
@@ -27,7 +26,6 @@ impl Prompt {
27 line: String::new(),
26 line: String::new(),
28 cursor: 0,
27 cursor: 0,
29 completion: completion_fn(""),
28 completion: completion_fn(""),
30− should_close: false,
31 completion_selection_index: None,
29 completion_selection_index: None,
32 completion_fn: Box::new(completion_fn),
30 completion_fn: Box::new(completion_fn),
33 callback_fn: Box::new(callback_fn),
31 callback_fn: Box::new(callback_fn),
@@ -42,9 +40,7 @@ impl Prompt {
42 }
40 }
43
41
44 pub fn move_char_left(&mut self) {
42 pub fn move_char_left(&mut self) {
45− if self.cursor > 0 {
46− self.cursor -= 1;
43+ self.cursor = self.cursor.saturating_sub(1)
47− }
48 }
44 }
49
45
50 pub fn move_char_right(&mut self) {
46 pub fn move_char_right(&mut self) {