diff --git a/crates/editor/src/items.rs b/crates/editor/src/items.rs index b7110190fd..30ff51fe53 100644 --- a/crates/editor/src/items.rs +++ b/crates/editor/src/items.rs @@ -1470,6 +1470,7 @@ impl Editor { } pub(crate) enum BufferSearchHighlights {} +const ACTIVE_SEARCH_MATCH_KEY: usize = 0; impl SearchableItem for Editor { type Match = Range; @@ -1488,6 +1489,13 @@ impl SearchableItem for Editor { { cx.emit(SearchEvent::MatchesInvalidated); } + + self.highlight_background_key::( + ACTIVE_SEARCH_MATCH_KEY, + &[], + |theme| theme.colors().element_selection_background, + cx, + ); } fn update_matches( @@ -1506,6 +1514,33 @@ impl SearchableItem for Editor { |theme| theme.colors().search_match_background, cx, ); + + let active_ix = active_match_index( + Direction::Next, + matches, + &self.selections.newest_anchor().head(), + &self.buffer().read(cx).snapshot(cx), + ); + match active_ix { + Some(ix) => { + let range = &matches[ix]; + self.highlight_background_key::( + ACTIVE_SEARCH_MATCH_KEY, + std::slice::from_ref(range), + |theme| theme.colors().element_selection_background, + cx, + ); + } + None => { + self.highlight_background_key::( + ACTIVE_SEARCH_MATCH_KEY, + &[], + |theme| theme.colors().element_selection_background, + cx, + ); + } + } + if updated { cx.emit(SearchEvent::MatchesInvalidated); } @@ -1603,7 +1638,13 @@ impl SearchableItem for Editor { let range = self.range_for_match(&matches[index]); self.change_selections(Default::default(), window, cx, |s| { s.select_ranges([range]); - }) + }); + self.highlight_background_key::( + ACTIVE_SEARCH_MATCH_KEY, + std::slice::from_ref(&matches[index]), + |theme| theme.colors().element_selection_background, + cx, + ); } fn select_matches( diff --git a/crates/search/src/buffer_search.rs b/crates/search/src/buffer_search.rs index a38dc8c35b..11bbbb7151 100644 --- a/crates/search/src/buffer_search.rs +++ b/crates/search/src/buffer_search.rs @@ -1551,6 +1551,7 @@ mod tests { &[ DisplayPoint::new(DisplayRow(2), 17)..DisplayPoint::new(DisplayRow(2), 19), DisplayPoint::new(DisplayRow(2), 43)..DisplayPoint::new(DisplayRow(2), 45), + DisplayPoint::new(DisplayRow(2), 17)..DisplayPoint::new(DisplayRow(2), 19), ] ); }); @@ -1564,7 +1565,10 @@ mod tests { editor.update_in(cx, |editor, window, cx| { assert_eq!( display_points_of(editor.all_text_background_highlights(window, cx)), - &[DisplayPoint::new(DisplayRow(2), 43)..DisplayPoint::new(DisplayRow(2), 45),] + &[ + DisplayPoint::new(DisplayRow(2), 43)..DisplayPoint::new(DisplayRow(2), 45), + DisplayPoint::new(DisplayRow(2), 43)..DisplayPoint::new(DisplayRow(2), 45), + ] ); }); @@ -1587,6 +1591,7 @@ mod tests { DisplayPoint::new(DisplayRow(3), 11)..DisplayPoint::new(DisplayRow(3), 13), DisplayPoint::new(DisplayRow(3), 56)..DisplayPoint::new(DisplayRow(3), 58), DisplayPoint::new(DisplayRow(3), 60)..DisplayPoint::new(DisplayRow(3), 62), + DisplayPoint::new(DisplayRow(0), 24)..DisplayPoint::new(DisplayRow(0), 26), ] ); }); @@ -1604,6 +1609,7 @@ mod tests { DisplayPoint::new(DisplayRow(0), 41)..DisplayPoint::new(DisplayRow(0), 43), DisplayPoint::new(DisplayRow(3), 11)..DisplayPoint::new(DisplayRow(3), 13), DisplayPoint::new(DisplayRow(3), 56)..DisplayPoint::new(DisplayRow(3), 58), + DisplayPoint::new(DisplayRow(0), 41)..DisplayPoint::new(DisplayRow(0), 43), ] ); }); @@ -1823,7 +1829,10 @@ mod tests { editor.update_in(cx, |editor, window, cx| { assert_eq!( display_points_of(editor.all_text_background_highlights(window, cx)), - &[DisplayPoint::new(DisplayRow(2), 43)..DisplayPoint::new(DisplayRow(2), 45),] + &[ + DisplayPoint::new(DisplayRow(2), 43)..DisplayPoint::new(DisplayRow(2), 45), + DisplayPoint::new(DisplayRow(2), 43)..DisplayPoint::new(DisplayRow(2), 45) + ] ); }); @@ -1848,7 +1857,10 @@ mod tests { editor.update_in(cx, |editor, window, cx| { assert_eq!( display_points_of(editor.all_text_background_highlights(window, cx)), - &[DisplayPoint::new(DisplayRow(0), 35)..DisplayPoint::new(DisplayRow(0), 40),] + &[ + DisplayPoint::new(DisplayRow(0), 35)..DisplayPoint::new(DisplayRow(0), 40), + DisplayPoint::new(DisplayRow(0), 35)..DisplayPoint::new(DisplayRow(0), 40), + ] ); }); @@ -2695,6 +2707,7 @@ mod tests { &[ DisplayPoint::new(DisplayRow(0), 10)..DisplayPoint::new(DisplayRow(0), 20), DisplayPoint::new(DisplayRow(1), 9)..DisplayPoint::new(DisplayRow(1), 19), + DisplayPoint::new(DisplayRow(0), 10)..DisplayPoint::new(DisplayRow(0), 20), ], ); }); diff --git a/crates/vim/src/normal/search.rs b/crates/vim/src/normal/search.rs index dba003ec5f..0e9d71b561 100644 --- a/crates/vim/src/normal/search.rs +++ b/crates/vim/src/normal/search.rs @@ -752,7 +752,7 @@ mod test { cx.update_editor(|editor, window, cx| { let highlights = editor.all_text_background_highlights(window, cx); - assert_eq!(3, highlights.len()); + assert_eq!(4, highlights.len()); assert_eq!( DisplayPoint::new(DisplayRow(2), 0)..DisplayPoint::new(DisplayRow(2), 2), highlights[0].0 diff --git a/crates/vim/src/test.rs b/crates/vim/src/test.rs index ce04b621cb..fbc9802119 100644 --- a/crates/vim/src/test.rs +++ b/crates/vim/src/test.rs @@ -273,7 +273,7 @@ async fn test_selection_on_search(cx: &mut gpui::TestAppContext) { cx.update_editor(|editor, window, cx| { let highlights = editor.all_text_background_highlights(window, cx); - assert_eq!(3, highlights.len()); + assert_eq!(4, highlights.len()); assert_eq!( DisplayPoint::new(DisplayRow(2), 0)..DisplayPoint::new(DisplayRow(2), 2), highlights[0].0