This commit is contained in:
Lakshyajeet Singh Goyal 2025-08-27 00:39:16 +08:00 committed by GitHub
commit 6499b54f1c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 60 additions and 6 deletions

View file

@ -1470,6 +1470,7 @@ impl Editor {
}
pub(crate) enum BufferSearchHighlights {}
const ACTIVE_SEARCH_MATCH_KEY: usize = 0;
impl SearchableItem for Editor {
type Match = Range<Anchor>;
@ -1488,6 +1489,13 @@ impl SearchableItem for Editor {
{
cx.emit(SearchEvent::MatchesInvalidated);
}
self.highlight_background_key::<BufferSearchHighlights>(
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::<BufferSearchHighlights>(
ACTIVE_SEARCH_MATCH_KEY,
std::slice::from_ref(range),
|theme| theme.colors().element_selection_background,
cx,
);
}
None => {
self.highlight_background_key::<BufferSearchHighlights>(
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::<BufferSearchHighlights>(
ACTIVE_SEARCH_MATCH_KEY,
std::slice::from_ref(&matches[index]),
|theme| theme.colors().element_selection_background,
cx,
);
}
fn select_matches(

View file

@ -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),
],
);
});

View file

@ -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

View file

@ -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