From 1fafef23b34088c496c02940dfc7d1981579a420 Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Thu, 10 Mar 2022 17:22:36 -0500 Subject: [PATCH] Refactor to avoid duplicating find_nth_next --- helix-core/src/search.rs | 29 +---------------------------- helix-term/src/commands.rs | 7 +++++-- 2 files changed, 6 insertions(+), 30 deletions(-) diff --git a/helix-core/src/search.rs b/helix-core/src/search.rs index 2782b32dcd9a2..81cb412939df5 100644 --- a/helix-core/src/search.rs +++ b/helix-core/src/search.rs @@ -17,30 +17,7 @@ impl bool> CharMatcher for F { } } -pub fn find_nth_next(text: RopeSlice, ch: char, mut pos: usize, n: usize) -> Option { - if pos >= text.len_chars() || n == 0 { - return None; - } - - let mut chars = text.chars_at(pos); - - for _ in 0..n { - loop { - let c = chars.next()?; - - pos += 1; - - if c == ch { - break; - } - } - } - - Some(pos - 1) -} - -/// Like find_nth_next, but stops at the first newline character. -pub fn find_nth_next_until_newline( +pub fn find_nth_next( text: RopeSlice, char_matcher: M, mut pos: usize, @@ -58,10 +35,6 @@ pub fn find_nth_next_until_newline( pos += 1; - if c == '\n' || c == '\r' { - return None; - } - if char_matcher.char_match(c) { break; } diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 807e39245b500..608dbb407b0b0 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -4340,10 +4340,13 @@ fn find_next_char_until_newline( text: RopeSlice, char_matcher: M, pos: usize, - n: usize, + _count: usize, _inclusive: bool, ) -> Option { - search::find_nth_next_until_newline(text, char_matcher, pos, n) + let line_index = text.char_to_line(pos); + let pos_delta = text.line_to_char(line_index); + let pos = pos - pos_delta; + search::find_nth_next(text.line(line_index), char_matcher, pos, 1).map(|pos| pos + pos_delta) } /// Decrement object under cursor by `amount`.