Make buffer search aware of search direction (#24974)

This solves a couple of issues with Vim search by making the search
buffer and `SearchableItem` aware of the direction of the search. If
`SearchOptions::BACKWARDS` is set, all operations will be reversed. By
making `SearchableItem` aware of the direction, the correct active match
can be selected when searching backward.

Fixes #22506. This PR does not fix the last problem in that issue, but
that one is also tracked in #8049.

Release Notes:

- Fixes incorrect behavior of backward search in Vim mode
This commit is contained in:
Nico Lehmann 2025-03-04 18:27:37 -08:00 committed by GitHub
parent ed13e05855
commit 229e853874
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 80 additions and 25 deletions

View file

@ -154,6 +154,9 @@ impl Vim {
if action.regex {
options |= SearchOptions::REGEX;
}
if action.backwards {
options |= SearchOptions::BACKWARDS;
}
search_bar.set_search_options(options, cx);
let prior_mode = if self.temp_mode {
Mode::Insert
@ -198,7 +201,7 @@ impl Vim {
.last()
.map_or(true, |range| range.start != new_head);
if is_different_head && self.search.direction == Direction::Next {
if is_different_head {
count = count.saturating_sub(1)
}
self.search.count = 1;
@ -743,6 +746,12 @@ mod test {
cx.simulate_keystrokes("*");
cx.assert_state("one two ˇone", Mode::Normal);
// check that a backward search after last match works correctly
cx.set_state("aa\naa\nbbˇ", Mode::Normal);
cx.simulate_keystrokes("? a a");
cx.simulate_keystrokes("enter");
cx.assert_state("aa\nˇaa\nbb", Mode::Normal);
// check that searching with unable search wrap
cx.update_global(|store: &mut SettingsStore, cx| {
store.update_user_settings::<EditorSettings>(cx, |s| s.search_wrap = Some(false));