on master help sign in
Jump to the next number on the line before incrementing (#1778)

* Jump to the next number on the line before incrementing Partially fix #1645 * Refactor to avoid duplicating find_nth_next

commit47fe7397574208ecdcccc63770ce1b0374de1126 authorantoyo <antoyo@users.noreply.github.com> dateFri 1 Apr 2022, 09:14 (5 years ago) parent855e438
2 files changed+61−7
helix-core/src/search.rs+24−2 helix-term/src/commands.rs+37−5
helix-core/src/search.rs+24−2
@@ -1,6 +1,28 @@
11use crate::RopeSlice;
22
3−pub fn find_nth_next(text: RopeSlice, ch: char, mut pos: usize, n: usize) -> Option<usize> {
3+// TODO: switch to std::str::Pattern when it is stable.
4+pub trait CharMatcher {
5+ fn char_match(&self, ch: char) -> bool;
6+}
7+
8+impl CharMatcher for char {
9+ fn char_match(&self, ch: char) -> bool {
10+ *self == ch
11+ }
12+}
13+
14+impl<F: Fn(&char) -> bool> CharMatcher for F {
15+ fn char_match(&self, ch: char) -> bool {
16+ (*self)(&ch)
17+ }
18+}
19+
20+pub fn find_nth_next<M: CharMatcher>(
21+ text: RopeSlice,
22+ char_matcher: M,
23+ mut pos: usize,
24+ n: usize,
25+) -> Option<usize> {
426 if pos >= text.len_chars() || n == 0 {
527 return None;
628 }
@@ -13,7 +35,7 @@ pub fn find_nth_next(text: RopeSlice, ch: char, mut pos: usize, n: usize) -> Opt
1335
1436 pos += 1;
1537
16− if c == ch {
38+ if char_matcher.char_match(c) {
1739 break;
1840 }
1941 }
helix-term/src/commands.rs+37−5
@@ -18,7 +18,8 @@ use helix_core::{
1818 movement::{self, Direction},
1919 object, pos_at_coords,
2020 regex::{self, Regex, RegexBuilder},
21− search, selection, shellwords, surround, textobject,
21+ search::{self, CharMatcher},
22+ selection, shellwords, surround, textobject,
2223 tree_sitter::Node,
2324 unicode::width::UnicodeWidthChar,
2425 LineEnding, Position, Range, Rope, RopeGraphemes, RopeSlice, Selection, SmallVec, Tendril,
@@ -1053,15 +1054,15 @@ where
10531054//
10541055
10551056#[inline]
1056−fn find_char_impl<F>(
1057+fn find_char_impl<F, M: CharMatcher + Clone + Copy>(
10571058 editor: &mut Editor,
10581059 search_fn: &F,
10591060 inclusive: bool,
10601061 extend: bool,
1061− ch: char,
1062+ char_matcher: M,
10621063 count: usize,
10631064) where
1064− F: Fn(RopeSlice, char, usize, usize, bool) -> Option<usize> + 'static,
1065+ F: Fn(RopeSlice, M, usize, usize, bool) -> Option<usize> + 'static,
10651066{
10661067 let (view, doc) = current!(editor);
10671068 let text = doc.text().slice(..);
@@ -1076,7 +1077,7 @@ fn find_char_impl<F>(
10761077 range.head
10771078 };
10781079
1079− search_fn(text, ch, search_start_pos, count, inclusive).map_or(range, |pos| {
1080+ search_fn(text, char_matcher, search_start_pos, count, inclusive).map_or(range, |pos| {
10801081 if extend {
10811082 range.put_cursor(text, pos, true)
10821083 } else {
@@ -4327,8 +4328,39 @@ fn decrement(cx: &mut Context) {
43274328 increment_impl(cx, -(cx.count() as i64));
43284329}
43294330
4331+/// This function differs from find_next_char_impl in that it stops searching at the newline, but also
4332+/// starts searching at the current character, instead of the next.
4333+/// It does not want to start at the next character because this function is used for incrementing
4334+/// number and we don't want to move forward if we're already on a digit.
4335+fn find_next_char_until_newline<M: CharMatcher>(
4336+ text: RopeSlice,
4337+ char_matcher: M,
4338+ pos: usize,
4339+ _count: usize,
4340+ _inclusive: bool,
4341+) -> Option<usize> {
4342+ // Since we send the current line to find_nth_next instead of the whole text, we need to adjust
4343+ // the position we send to this function so that it's relative to that line and its returned
4344+ // position since it's expected this function returns a global position.
4345+ let line_index = text.char_to_line(pos);
4346+ let pos_delta = text.line_to_char(line_index);
4347+ let pos = pos - pos_delta;
4348+ search::find_nth_next(text.line(line_index), char_matcher, pos, 1).map(|pos| pos + pos_delta)
4349+}
4350+
43304351/// Decrement object under cursor by `amount`.
43314352fn increment_impl(cx: &mut Context, amount: i64) {
4353+ // TODO: when incrementing or decrementing a number that gets a new digit or lose one, the
4354+ // selection is updated improperly.
4355+ find_char_impl(
4356+ cx.editor,
4357+ &find_next_char_until_newline,
4358+ true,
4359+ true,
4360+ char::is_ascii_digit,
4361+ 1,
4362+ );
4363+
43324364 let (view, doc) = current!(cx.editor);
43334365 let selection = doc.selection(view.id);
43344366 let text = doc.text().slice(..);