mirror of
https://github.com/helix-editor/helix.git
synced 2025-01-19 13:37:06 +04:00
444cd0b068
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`
65 lines
1.0 KiB
Rust
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)
|
|
}
|