helix-mirror/helix-core/src/search.rs
ahkrr 444cd0b068 fix: make find_prev_char and till_prev_char work
Bevore this PR `commands::find_prev_char` and `commands::till_prev_char` were triggerable through keys 
but `seach::find_nth_next()` was hardcoded in `_find_char`. 
The passed `fn` was nerver used. With this PR the passed `fn` is used.
The change in search.rs resolves an off by one error in the behivor of `find_nth_prev`
2021-06-06 00:01:16 +09:00

65 lines
1.0 KiB
Rust

use crate::RopeSlice;
pub fn find_nth_next(
text: RopeSlice,
ch: char,
mut pos: usize,
n: usize,
inclusive: bool,
) -> Option<usize> {
if pos >= text.len_chars() {
return None;
}
// start searching right after pos
let mut chars = text.chars_at(pos + 1);
for _ in 0..n {
loop {
let c = chars.next()?;
pos += 1;
if c == ch {
break;
}
}
}
if !inclusive {
pos -= 1;
}
Some(pos)
}
pub fn find_nth_prev(
text: RopeSlice,
ch: char,
mut pos: usize,
n: usize,
inclusive: bool,
) -> Option<usize> {
// start searching right before pos
pos = pos.saturating_sub(1);
let mut chars = text.chars_at(pos);
for _ in 0..n {
loop {
let c = chars.prev()?;
pos = pos.saturating_sub(1);
if c == ch {
break;
}
}
}
if !inclusive {
pos += 1;
}
Some(pos)
}