Skip to content

Commit

Permalink
Refactor to avoid duplicating find_nth_next
Browse files Browse the repository at this point in the history
  • Loading branch information
antoyo committed Mar 10, 2022
1 parent 82ead06 commit 1fafef2
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 30 deletions.
29 changes: 1 addition & 28 deletions helix-core/src/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,7 @@ impl<F: Fn(&char) -> bool> CharMatcher for F {
}
}

pub fn find_nth_next(text: RopeSlice, ch: char, mut pos: usize, n: usize) -> Option<usize> {
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<M: CharMatcher>(
pub fn find_nth_next<M: CharMatcher>(
text: RopeSlice,
char_matcher: M,
mut pos: usize,
Expand All @@ -58,10 +35,6 @@ pub fn find_nth_next_until_newline<M: CharMatcher>(

pos += 1;

if c == '\n' || c == '\r' {
return None;
}

if char_matcher.char_match(c) {
break;
}
Expand Down
7 changes: 5 additions & 2 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4340,10 +4340,13 @@ fn find_next_char_until_newline<M: CharMatcher>(
text: RopeSlice,
char_matcher: M,
pos: usize,
n: usize,
_count: usize,
_inclusive: bool,
) -> Option<usize> {
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`.
Expand Down

0 comments on commit 1fafef2

Please sign in to comment.