Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
search: Fixed highlighting item beyond cursor (#54514)
Dino2d86e3b30b73
diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rsindex b01d1592abb..302708c7864 100644--- a/crates/editor/src/editor.rs+++ b/crates/editor/src/editor.rs@@ -3860,9 +3860,6 @@ impl Editor {cx.emit(EditorEvent::SelectionsChanged { local });let selections = &self.selections.disjoint_anchors_arc();- if selections.len() == 1 {- cx.emit(SearchEvent::ActiveMatchChanged)- }if local && let Some(buffer_snapshot) = buffer.as_singleton() {let inmemory_selections = selections.iter()diff --git a/crates/editor/src/items.rs b/crates/editor/src/items.rsindex 1752aefc5e6..125f09c9661 100644--- a/crates/editor/src/items.rs+++ b/crates/editor/src/items.rs@@ -1799,6 +1799,10 @@ impl SearchableItem for Editor {});}}++ /// Takes the current cursor position and finds the next match in the+ /// provided `direction`, the provide `count` number of times, wrapping+ /// around if necessary.fn match_index_for_direction(&mut self,matches: &[Range<Anchor>],@@ -1809,45 +1813,48 @@ impl SearchableItem for Editor {_: &mut Window,cx: &mut Context<Self>,) -> usize {- let buffer = self.buffer().read(cx).snapshot(cx);- let current_index_position = if self.selections.disjoint_anchors_arc().len() == 1 {+ if count == 0 {+ return current_index;+ }++ let cursor = if self.selections.disjoint_anchors_arc().len() == 1 {self.selections.newest_anchor().head()} else {matches[current_index].start};- let mut count = count % matches.len();- if count == 0 {- return current_index;- }- match direction {- Direction::Next => {- if matches[current_index]- .start- .cmp(¤t_index_position, &buffer)- .is_gt()- {- count -= 1- }-- (current_index + count) % matches.len()- }- Direction::Prev => {- if matches[current_index]- .end- .cmp(¤t_index_position, &buffer)- .is_lt()- {- count -= 1;- }+ let buffer = self.buffer().read(cx).snapshot(cx);+ let new_idx = match direction {+ Direction::Next => matches+ .iter()+ .position(|m| m.start.cmp(&cursor, &buffer).is_gt())+ .unwrap_or(0),+ Direction::Prev => matches+ .iter()+ .rposition(|m| m.end.cmp(&cursor, &buffer).is_lt())+ .unwrap_or(matches.len() - 1),+ } as isize;++ // We'll use `count - 1` because the first jump to the next or previous+ // match already happens in the scenario above, when we find the next or+ // previous match starting from the cursor position.+ let count = count.saturating_sub(1);+ let count = match direction {+ Direction::Prev => -(count as isize),+ Direction::Next => count as isize,+ };- if current_index >= count {- current_index - count- } else {- matches.len() - (count - current_index)- }- }- }+ let new_idx = (new_idx + count) % matches.len() as isize;+ let new_idx = if new_idx.is_negative() {+ // We need a `matches.len() - 1` here in case `next_idx` has now been+ // set to `0`, otherwise we'd end up returning `matches.len()`, which+ // would be out of bounds.+ new_idx + (matches.len() - 1) as isize+ } else {+ new_idx+ };+ assert!(new_idx < matches.len() as isize);+ new_idx as usize}fn find_matches(diff --git a/crates/markdown_preview/src/markdown_preview_view.rs b/crates/markdown_preview/src/markdown_preview_view.rsindex 2b7379b0b40..27ece268d66 100644--- a/crates/markdown_preview/src/markdown_preview_view.rs+++ b/crates/markdown_preview/src/markdown_preview_view.rs@@ -999,7 +999,6 @@ impl SearchableItem for MarkdownPreviewView {markdown.set_active_search_highlight(Some(index), cx);markdown.request_autoscroll_to_source_index(start, cx);});- cx.emit(SearchEvent::ActiveMatchChanged);}}diff --git a/crates/search/src/buffer_search.rs b/crates/search/src/buffer_search.rsindex 9dcef1b60e1..8170d303eb8 100644--- a/crates/search/src/buffer_search.rs+++ b/crates/search/src/buffer_search.rs@@ -1399,6 +1399,7 @@ impl BufferSearchBar {}let new_match_index = searchable_item.match_index_for_direction(matches, index, direction, count, *token, window, cx);+ self.active_match_index = Some(new_match_index);searchable_item.update_matches(matches, Some(new_match_index), *token, window, cx);searchable_item.activate_match(new_match_index, matches, *token, window, cx);@@ -2257,8 +2258,8 @@ mod tests {assert_eq!(search_bar.active_match_index, Some(0));});- // Park the cursor in between matches and ensure that going to the previous match selects- // the closest match to the left.+ // Park the cursor in between matches and ensure that going to the previous match+ // selects the closest match to the left of the cursor.editor.update_in(cx, |editor, window, cx| {editor.change_selections(SelectionEffects::no_scroll(), window, cx, |s| {s.select_display_ranges([@@ -2267,7 +2268,6 @@ mod tests {});});search_bar.update_in(cx, |search_bar, window, cx| {- assert_eq!(search_bar.active_match_index, Some(1));search_bar.select_prev_match(&SelectPreviousMatch, window, cx);assert_eq!(editor.update(cx, |editor, cx| editor@@ -2280,8 +2280,8 @@ mod tests {assert_eq!(search_bar.active_match_index, Some(0));});- // Park the cursor in between matches and ensure that going to the next match selects the- // closest match to the right.+ // Park the cursor in between matches and ensure that going to the next match+ // selects the closest match to the right of the cursor.editor.update_in(cx, |editor, window, cx| {editor.change_selections(SelectionEffects::no_scroll(), window, cx, |s| {s.select_display_ranges([@@ -2290,7 +2290,6 @@ mod tests {});});search_bar.update_in(cx, |search_bar, window, cx| {- assert_eq!(search_bar.active_match_index, Some(1));search_bar.select_next_match(&SelectNextMatch, window, cx);assert_eq!(editor.update(cx, |editor, cx| editor@@ -2303,8 +2302,8 @@ mod tests {assert_eq!(search_bar.active_match_index, Some(1));});- // Park the cursor after the last match and ensure that going to the previous match selects- // the last match.+ // Park the cursor after the last match and ensure that going to the previous match+ // selects the last match.editor.update_in(cx, |editor, window, cx| {editor.change_selections(SelectionEffects::no_scroll(), window, cx, |s| {s.select_display_ranges([@@ -2313,7 +2312,6 @@ mod tests {});});search_bar.update_in(cx, |search_bar, window, cx| {- assert_eq!(search_bar.active_match_index, Some(2));search_bar.select_prev_match(&SelectPreviousMatch, window, cx);assert_eq!(editor.update(cx, |editor, cx| editor@@ -2326,8 +2324,8 @@ mod tests {assert_eq!(search_bar.active_match_index, Some(2));});- // Park the cursor after the last match and ensure that going to the next match selects the- // first match.+ // Park the cursor after the last match and ensure that going to the next match+ // wraps around and selects the first match.editor.update_in(cx, |editor, window, cx| {editor.change_selections(SelectionEffects::no_scroll(), window, cx, |s| {s.select_display_ranges([@@ -2336,7 +2334,6 @@ mod tests {});});search_bar.update_in(cx, |search_bar, window, cx| {- assert_eq!(search_bar.active_match_index, Some(2));search_bar.select_next_match(&SelectNextMatch, window, cx);assert_eq!(editor.update(cx, |editor, cx| editor@@ -2350,7 +2347,7 @@ mod tests {});// Park the cursor before the first match and ensure that going to the previous match- // selects the last match.+ // wraps around and selects the last match.editor.update_in(cx, |editor, window, cx| {editor.change_selections(SelectionEffects::no_scroll(), window, cx, |s| {s.select_display_ranges([