diff options
author | Blaž Hrastnik | 2021-03-11 01:44:38 +0000 |
---|---|---|
committer | Blaž Hrastnik | 2021-03-11 01:44:38 +0000 |
commit | 62c991230f511b2dc11f8d1701260511800429d4 (patch) | |
tree | 0cb7cc0102134a973c5f600e61bca350ad39d74d /helix-term | |
parent | 90f9cd6d6275048db664afa4139e7f2bdfaeacb0 (diff) |
find-till (f) prototype, on_next_key mode implementation.
Diffstat (limited to 'helix-term')
-rw-r--r-- | helix-term/src/commands.rs | 43 | ||||
-rw-r--r-- | helix-term/src/keymap.rs | 2 | ||||
-rw-r--r-- | helix-term/src/ui/editor.rs | 73 |
3 files changed, 85 insertions, 33 deletions
diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 47a61b7f..f60f646e 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -3,7 +3,7 @@ use helix_core::{ indent::TAB_WIDTH, object, regex::{self, Regex}, - register, selection, + register, search, selection, state::{coords_at_pos, pos_at_coords, Direction, Granularity, State}, Change, ChangeSet, Position, Range, Selection, Tendril, Transaction, }; @@ -19,12 +19,15 @@ use helix_view::{ Document, Editor, }; +use crossterm::event::{KeyCode, KeyEvent}; + pub struct Context<'a> { pub count: usize, pub editor: &'a mut Editor, pub executor: &'static smol::Executor<'static>, pub callback: Option<crate::compositor::Callback>, + pub on_next_key_callback: Option<Box<dyn FnOnce(&mut Context, KeyEvent)>>, } impl<'a> Context<'a> { @@ -49,6 +52,14 @@ impl<'a> Context<'a> { }, )); } + + #[inline] + pub fn on_next_key( + &mut self, + on_next_key_callback: impl FnOnce(&mut Context, KeyEvent) + 'static, + ) { + self.on_next_key_callback = Some(Box::new(on_next_key_callback)); + } } /// A command is a function that takes the current state and a count, and does a side-effect on the @@ -225,6 +236,36 @@ pub fn extend_next_word_end(cx: &mut Context) { doc.set_selection(selection); } +pub fn find_next_char(cx: &mut Context) { + // TODO: count is reset to 1 before next key so we move it into the closure here. + // Would be nice to carry over. + let count = cx.count; + + // need to wait for next key + cx.on_next_key(move |cx, event| { + if let KeyEvent { + code: KeyCode::Char(ch), + .. + } = event + { + let doc = cx.doc(); + let text = doc.text().slice(..); + + let selection = doc.selection().transform(|mut range| { + if let Some(pos) = search::find_nth_next(text, ch, range.head, count) { + Range::new(range.head, pos) + // or (range.anchor, pos) for extend + // or (pos, pos) to move to found val + } else { + range + } + }); + + doc.set_selection(selection); + } + }) +} + fn scroll(view: &mut View, offset: usize, direction: Direction) { use Direction::*; let text = view.doc.text().slice(..); diff --git a/helix-term/src/keymap.rs b/helix-term/src/keymap.rs index 3be92fcc..d956679a 100644 --- a/helix-term/src/keymap.rs +++ b/helix-term/src/keymap.rs @@ -138,7 +138,7 @@ pub fn default() -> Keymaps { key!('l') => commands::move_char_right, // key!('t') => commands::till_next_char, - // key!('f') => commands::find_next_char, + key!('f') => commands::find_next_char, // key!('T') => commands::till_prev_char, // key!('f') => commands::find_prev_char, // and matching set for select mode (extend) diff --git a/helix-term/src/ui/editor.rs b/helix-term/src/ui/editor.rs index 0bfc1a33..7a5e4aa5 100644 --- a/helix-term/src/ui/editor.rs +++ b/helix-term/src/ui/editor.rs @@ -20,6 +20,7 @@ use tui::{ pub struct EditorView { keymap: Keymaps, + on_next_key: Option<Box<dyn FnOnce(&mut commands::Context, KeyEvent)>>, } const OFFSET: u16 = 7; // 1 diagnostic + 5 linenr + 1 gutter @@ -28,6 +29,7 @@ impl EditorView { pub fn new() -> Self { Self { keymap: keymap::default(), + on_next_key: None, } } pub fn render_view( @@ -366,46 +368,55 @@ impl Component for EditorView { editor: &mut cx.editor, count: 1, callback: None, + on_next_key_callback: None, }; - match mode { - Mode::Insert => { - if let Some(command) = self.keymap[&Mode::Insert].get(&event) { - command(&mut cxt); - } else if let KeyEvent { - code: KeyCode::Char(c), - .. - } = event - { - commands::insert::insert_char(&mut cxt, c); - } - } - mode => { - match event { - KeyEvent { - code: KeyCode::Char(i @ '0'..='9'), - modifiers: KeyModifiers::NONE, - } => { - let i = i.to_digit(10).unwrap() as usize; - cxt.editor.count = Some(cxt.editor.count.map_or(i, |c| c * 10 + i)); + if let Some(on_next_key) = self.on_next_key.take() { + // if there's a command waiting input, do that first + on_next_key(&mut cxt, event); + } else { + match mode { + Mode::Insert => { + if let Some(command) = self.keymap[&Mode::Insert].get(&event) { + command(&mut cxt); + } else if let KeyEvent { + code: KeyCode::Char(c), + .. + } = event + { + commands::insert::insert_char(&mut cxt, c); } - _ => { - // set the count - cxt.count = cxt.editor.count.take().unwrap_or(1); - // TODO: edge case: 0j -> reset to 1 - // if this fails, count was Some(0) - // debug_assert!(cxt.count != 0); - - if let Some(command) = self.keymap[&mode].get(&event) { - command(&mut cxt); - - // TODO: simplistic ensure cursor in view for now + } + mode => { + match event { + KeyEvent { + code: KeyCode::Char(i @ '0'..='9'), + modifiers: KeyModifiers::NONE, + } => { + let i = i.to_digit(10).unwrap() as usize; + cxt.editor.count = + Some(cxt.editor.count.map_or(i, |c| c * 10 + i)); + } + _ => { + // set the count + cxt.count = cxt.editor.count.take().unwrap_or(1); + // TODO: edge case: 0j -> reset to 1 + // if this fails, count was Some(0) + // debug_assert!(cxt.count != 0); + + if let Some(command) = self.keymap[&mode].get(&event) { + command(&mut cxt); + + // TODO: simplistic ensure cursor in view for now + } } } } } } + self.on_next_key = cxt.on_next_key_callback.take(); + // appease borrowck let callback = cxt.callback.take(); drop(cxt); |