on master help sign in
find-till (f) prototype, on_next_key mode implementation.
commit62c991230f511b2dc11f8d1701260511800429d4 authorBlaž Hrastnik <blaz@mxxn.io> dateThu 11 Mar 2021, 10:44 (6 years ago) parent90f9cd6
6 files changed+117−86
helix-core/src/lib.rs+1 helix-core/src/search.rs+20−50 helix-core/src/state.rs+11−3 helix-term/src/commands.rs+42−1 helix-term/src/keymap.rs+1−1 helix-term/src/ui/editor.rs+42−31
helix-core/src/lib.rs+1
@@ -8,6 +8,7 @@ pub mod macros;
88pub mod object;
99mod position;
1010pub mod register;
11+pub mod search;
1112pub mod selection;
1213pub mod state;
1314pub mod syntax;
helix-core/src/search.rs+20−50
@@ -1,69 +1,39 @@
11use crate::RopeSlice;
22
3−pub fn find_nth_next(text: RopeSlice, ch: char, pos: usize, n: usize) -> Option<usize> {
3+pub fn find_nth_next(text: RopeSlice, ch: char, mut pos: usize, n: usize) -> Option<usize> {
44 // start searching right after pos
5− let mut byte_idx = text.char_to_byte(pos + 1);
6−
7− let (mut chunks, mut chunk_byte_idx, _chunk_char_idx, _chunk_line_idx) =
8− text.chunks_at_byte(byte_idx);
9−
10− let mut chunk = chunks.next().unwrap_or("");
11−
12− chunk = &chunk[(byte_idx - chunk_byte_idx)..];
5+ let mut chars = text.chars_at(pos + 1);
136
147 for _ in 0..n {
158 loop {
16− match chunk.find(ch) {
17− Some(pos) => {
18− byte_idx += pos;
19− chunk = &chunk[pos + 1..];
20− break;
21− }
22− None => match chunks.next() {
23− Some(next_chunk) => {
24− byte_idx += chunk.len();
25− chunk = next_chunk;
26− }
27− None => {
28− log::info!("no more chunks");
29− return None;
30− }
31− },
9+ let c = chars.next()?;
10+
11+ pos += 1;
12+
13+ if c == ch {
14+ break;
3215 }
3316 }
3417 }
35− Some(text.byte_to_char(byte_idx))
18+
19+ Some(pos)
3620}
3721
38−pub fn find_nth_prev(text: RopeSlice, ch: char, pos: usize, n: usize) -> Option<usize> {
22+pub fn find_nth_prev(text: RopeSlice, ch: char, mut pos: usize, n: usize) -> Option<usize> {
3923 // start searching right before pos
40− let mut byte_idx = text.char_to_byte(pos.saturating_sub(1));
41−
42− let (mut chunks, mut chunk_byte_idx, _chunk_char_idx, _chunk_line_idx) =
43− text.chunks_at_byte(byte_idx);
44−
45− let mut chunk = chunks.prev().unwrap_or("");
46−
47− // start searching from pos
48− chunk = &chunk[..=byte_idx - chunk_byte_idx];
24+ let mut chars = text.chars_at(pos.saturating_sub(1));
4925
5026 for _ in 0..n {
5127 loop {
52− match chunk.rfind(ch) {
53− Some(pos) => {
54− byte_idx = chunk_byte_idx + pos;
55− chunk = &chunk[..pos];
56− break;
57− }
58− None => match chunks.prev() {
59− Some(prev_chunk) => {
60− chunk_byte_idx -= chunk.len();
61− chunk = prev_chunk;
62− }
63− None => return None,
64− },
28+ let c = chars.prev()?;
29+
30+ pos = pos.saturating_sub(1);
31+
32+ if c == ch {
33+ break;
6534 }
6635 }
6736 }
68− Some(text.byte_to_char(byte_idx))
37+
38+ Some(pos)
6939}
helix-core/src/state.rs+11−3
@@ -123,6 +123,8 @@ impl State {
123123 pub fn move_next_word_start(slice: RopeSlice, mut pos: usize, count: usize) -> usize {
124124 // TODO: confirm it's fine without using graphemes, I think it should be
125125 for _ in 0..count {
126+ // TODO: if end return end
127+
126128 let ch = slice.char(pos);
127129 let next = slice.char(pos.saturating_add(1));
128130 if categorize(ch) != categorize(next) {
@@ -148,8 +150,12 @@ impl State {
148150 pub fn move_prev_word_start(slice: RopeSlice, mut pos: usize, count: usize) -> usize {
149151 // TODO: confirm it's fine without using graphemes, I think it should be
150152 for _ in 0..count {
153+ if pos == 0 {
154+ return pos;
155+ }
156+
151157 let ch = slice.char(pos);
152− let prev = slice.char(pos.saturating_sub(1)); // TODO: just return original pos if at start
158+ let prev = slice.char(pos - 1);
153159
154160 if categorize(ch) != categorize(prev) {
155161 pos -= 1;
@@ -176,6 +182,8 @@ impl State {
176182
177183 pub fn move_next_word_end(slice: RopeSlice, mut pos: usize, count: usize) -> usize {
178184 for _ in 0..count {
185+ // TODO: if end return end
186+
179187 // TODO: confirm it's fine without using graphemes, I think it should be
180188 let ch = slice.char(pos);
181189 let next = slice.char(pos.saturating_add(1));
@@ -303,7 +311,7 @@ where
303311 if !fun(ch) {
304312 break;
305313 }
306− *pos += 1;
314+ *pos += 1; // TODO: can go 1 over end of doc
307315 }
308316}
309317
@@ -319,7 +327,7 @@ where
319327 if !fun(ch) {
320328 break;
321329 }
322− *pos -= 1;
330+ *pos -= pos.saturating_sub(1);
323331 }
324332}
325333
helix-term/src/commands.rs+42−1
@@ -3,7 +3,7 @@ use helix_core::{
33 indent::TAB_WIDTH,
44 object,
55 regex::{self, Regex},
6− register, selection,
6+ register, search, selection,
77 state::{coords_at_pos, pos_at_coords, Direction, Granularity, State},
88 Change, ChangeSet, Position, Range, Selection, Tendril, Transaction,
99};
@@ -19,12 +19,15 @@ use helix_view::{
1919 Document, Editor,
2020};
2121
22+use crossterm::event::{KeyCode, KeyEvent};
23+
2224pub struct Context<'a> {
2325 pub count: usize,
2426 pub editor: &'a mut Editor,
2527 pub executor: &'static smol::Executor<'static>,
2628
2729 pub callback: Option<crate::compositor::Callback>,
30+ pub on_next_key_callback: Option<Box<dyn FnOnce(&mut Context, KeyEvent)>>,
2831}
2932
3033impl<'a> Context<'a> {
@@ -49,6 +52,14 @@ impl<'a> Context<'a> {
4952 },
5053 ));
5154 }
55+
56+ #[inline]
57+ pub fn on_next_key(
58+ &mut self,
59+ on_next_key_callback: impl FnOnce(&mut Context, KeyEvent) + 'static,
60+ ) {
61+ self.on_next_key_callback = Some(Box::new(on_next_key_callback));
62+ }
5263}
5364
5465/// 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) {
225236 doc.set_selection(selection);
226237}
227238
239+pub fn find_next_char(cx: &mut Context) {
240+ // TODO: count is reset to 1 before next key so we move it into the closure here.
241+ // Would be nice to carry over.
242+ let count = cx.count;
243+
244+ // need to wait for next key
245+ cx.on_next_key(move |cx, event| {
246+ if let KeyEvent {
247+ code: KeyCode::Char(ch),
248+ ..
249+ } = event
250+ {
251+ let doc = cx.doc();
252+ let text = doc.text().slice(..);
253+
254+ let selection = doc.selection().transform(|mut range| {
255+ if let Some(pos) = search::find_nth_next(text, ch, range.head, count) {
256+ Range::new(range.head, pos)
257+ // or (range.anchor, pos) for extend
258+ // or (pos, pos) to move to found val
259+ } else {
260+ range
261+ }
262+ });
263+
264+ doc.set_selection(selection);
265+ }
266+ })
267+}
268+
228269fn scroll(view: &mut View, offset: usize, direction: Direction) {
229270 use Direction::*;
230271 let text = view.doc.text().slice(..);
helix-term/src/keymap.rs+1−1
@@ -138,7 +138,7 @@ pub fn default() -> Keymaps {
138138 key!('l') => commands::move_char_right,
139139
140140 // key!('t') => commands::till_next_char,
141− // key!('f') => commands::find_next_char,
141+ key!('f') => commands::find_next_char,
142142 // key!('T') => commands::till_prev_char,
143143 // key!('f') => commands::find_prev_char,
144144 // and matching set for select mode (extend)
helix-term/src/ui/editor.rs+42−31
@@ -20,6 +20,7 @@ use tui::{
2020
2121pub struct EditorView {
2222 keymap: Keymaps,
23+ on_next_key: Option<Box<dyn FnOnce(&mut commands::Context, KeyEvent)>>,
2324}
2425
2526const OFFSET: u16 = 7; // 1 diagnostic + 5 linenr + 1 gutter
@@ -28,6 +29,7 @@ impl EditorView {
2829 pub fn new() -> Self {
2930 Self {
3031 keymap: keymap::default(),
32+ on_next_key: None,
3133 }
3234 }
3335 pub fn render_view(
@@ -366,46 +368,55 @@ impl Component for EditorView {
366368 editor: &mut cx.editor,
367369 count: 1,
368370 callback: None,
371+ on_next_key_callback: None,
369372 };
370373
371− match mode {
372− Mode::Insert => {
373− if let Some(command) = self.keymap[&Mode::Insert].get(&event) {
374− command(&mut cxt);
375− } else if let KeyEvent {
376− code: KeyCode::Char(c),
377− ..
378− } = event
379− {
380− commands::insert::insert_char(&mut cxt, c);
381− }
382− }
383− mode => {
384− match event {
385− KeyEvent {
386− code: KeyCode::Char(i @ '0'..='9'),
387− modifiers: KeyModifiers::NONE,
388− } => {
389− let i = i.to_digit(10).unwrap() as usize;
390− cxt.editor.count = Some(cxt.editor.count.map_or(i, |c| c * 10 + i));
374+ if let Some(on_next_key) = self.on_next_key.take() {
375+ // if there's a command waiting input, do that first
376+ on_next_key(&mut cxt, event);
377+ } else {
378+ match mode {
379+ Mode::Insert => {
380+ if let Some(command) = self.keymap[&Mode::Insert].get(&event) {
381+ command(&mut cxt);
382+ } else if let KeyEvent {
383+ code: KeyCode::Char(c),
384+ ..
385+ } = event
386+ {
387+ commands::insert::insert_char(&mut cxt, c);
391388 }
392− _ => {
393− // set the count
394− cxt.count = cxt.editor.count.take().unwrap_or(1);
395− // TODO: edge case: 0j -> reset to 1
396− // if this fails, count was Some(0)
397− // debug_assert!(cxt.count != 0);
398−
399− if let Some(command) = self.keymap[&mode].get(&event) {
400− command(&mut cxt);
401−
402− // TODO: simplistic ensure cursor in view for now
389+ }
390+ mode => {
391+ match event {
392+ KeyEvent {
393+ code: KeyCode::Char(i @ '0'..='9'),
394+ modifiers: KeyModifiers::NONE,
395+ } => {
396+ let i = i.to_digit(10).unwrap() as usize;
397+ cxt.editor.count =
398+ Some(cxt.editor.count.map_or(i, |c| c * 10 + i));
399+ }
400+ _ => {
401+ // set the count
402+ cxt.count = cxt.editor.count.take().unwrap_or(1);
403+ // TODO: edge case: 0j -> reset to 1
404+ // if this fails, count was Some(0)
405+ // debug_assert!(cxt.count != 0);
406+
407+ if let Some(command) = self.keymap[&mode].get(&event) {
408+ command(&mut cxt);
409+
410+ // TODO: simplistic ensure cursor in view for now
411+ }
403412 }
404413 }
405414 }
406415 }
407416 }
408417
418+ self.on_next_key = cxt.on_next_key_callback.take();
419+
409420 // appease borrowck
410421 let callback = cxt.callback.take();
411422 drop(cxt);