Skip to content

Commit 8ad3a15

Browse files
editor: Add active match highlight for buffer and project search (#44098)
Closes #28617 <img width="400" alt="image" src="https://github.com/user-attachments/assets/b1c2880c-5744-4bed-a687-5c5e7aa7fef5" /> Release Notes: - Improved visibility of the currently active match when browsing results in buffer or project search. --------- Co-authored-by: DarkMatter-999 <darkmatter999official@gmail.com>
1 parent 87976e9 commit 8ad3a15

File tree

23 files changed

+338
-145
lines changed

23 files changed

+338
-145
lines changed

assets/themes/ayu/ayu.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
"tab.inactive_background": "#1f2127ff",
4646
"tab.active_background": "#0d1016ff",
4747
"search.match_background": "#5ac2fe66",
48+
"search.active_match_background": "#ea570166",
4849
"panel.background": "#1f2127ff",
4950
"panel.focused_border": "#5ac1feff",
5051
"pane.focused_border": null,
@@ -436,6 +437,7 @@
436437
"tab.inactive_background": "#ececedff",
437438
"tab.active_background": "#fcfcfcff",
438439
"search.match_background": "#3b9ee566",
440+
"search.active_match_background": "#f88b3666",
439441
"panel.background": "#ececedff",
440442
"panel.focused_border": "#3b9ee5ff",
441443
"pane.focused_border": null,
@@ -827,6 +829,7 @@
827829
"tab.inactive_background": "#353944ff",
828830
"tab.active_background": "#242835ff",
829831
"search.match_background": "#73cffe66",
832+
"search.active_match_background": "#fd722b66",
830833
"panel.background": "#353944ff",
831834
"panel.focused_border": null,
832835
"pane.focused_border": null,

assets/themes/gruvbox/gruvbox.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
"tab.inactive_background": "#3a3735ff",
4747
"tab.active_background": "#282828ff",
4848
"search.match_background": "#83a59866",
49+
"search.active_match_background": "#c09f3f66",
4950
"panel.background": "#3a3735ff",
5051
"panel.focused_border": "#83a598ff",
5152
"pane.focused_border": null,
@@ -452,6 +453,7 @@
452453
"tab.inactive_background": "#393634ff",
453454
"tab.active_background": "#1d2021ff",
454455
"search.match_background": "#83a59866",
456+
"search.active_match_background": "#c9653666",
455457
"panel.background": "#393634ff",
456458
"panel.focused_border": "#83a598ff",
457459
"pane.focused_border": null,
@@ -858,6 +860,7 @@
858860
"tab.inactive_background": "#3b3735ff",
859861
"tab.active_background": "#32302fff",
860862
"search.match_background": "#83a59866",
863+
"search.active_match_background": "#aea85166",
861864
"panel.background": "#3b3735ff",
862865
"panel.focused_border": null,
863866
"pane.focused_border": null,
@@ -1264,6 +1267,7 @@
12641267
"tab.inactive_background": "#ecddb4ff",
12651268
"tab.active_background": "#fbf1c7ff",
12661269
"search.match_background": "#0b667866",
1270+
"search.active_match_background": "#ba2d1166",
12671271
"panel.background": "#ecddb4ff",
12681272
"panel.focused_border": null,
12691273
"pane.focused_border": null,
@@ -1670,6 +1674,7 @@
16701674
"tab.inactive_background": "#ecddb5ff",
16711675
"tab.active_background": "#f9f5d7ff",
16721676
"search.match_background": "#0b667866",
1677+
"search.active_match_background": "#dc351466",
16731678
"panel.background": "#ecddb5ff",
16741679
"panel.focused_border": null,
16751680
"pane.focused_border": null,
@@ -2076,6 +2081,7 @@
20762081
"tab.inactive_background": "#ecdcb3ff",
20772082
"tab.active_background": "#f2e5bcff",
20782083
"search.match_background": "#0b667866",
2084+
"search.active_match_background": "#d7331466",
20792085
"panel.background": "#ecdcb3ff",
20802086
"panel.focused_border": null,
20812087
"pane.focused_border": null,

assets/themes/one/one.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
"tab.inactive_background": "#2f343eff",
4646
"tab.active_background": "#282c33ff",
4747
"search.match_background": "#74ade866",
48+
"search.active_match_background": "#e8af7466",
4849
"panel.background": "#2f343eff",
4950
"panel.focused_border": null,
5051
"pane.focused_border": null,
@@ -448,6 +449,7 @@
448449
"tab.inactive_background": "#ebebecff",
449450
"tab.active_background": "#fafafaff",
450451
"search.match_background": "#5c79e266",
452+
"search.active_match_background": "#d0a92366",
451453
"panel.background": "#ebebecff",
452454
"panel.focused_border": null,
453455
"pane.focused_border": null,

crates/agent_ui/src/text_thread_editor.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2622,11 +2622,13 @@ impl SearchableItem for TextThreadEditor {
26222622
fn update_matches(
26232623
&mut self,
26242624
matches: &[Self::Match],
2625+
active_match_index: Option<usize>,
26252626
window: &mut Window,
26262627
cx: &mut Context<Self>,
26272628
) {
2628-
self.editor
2629-
.update(cx, |editor, cx| editor.update_matches(matches, window, cx));
2629+
self.editor.update(cx, |editor, cx| {
2630+
editor.update_matches(matches, active_match_index, window, cx)
2631+
});
26302632
}
26312633

26322634
fn query_suggestion(&mut self, window: &mut Window, cx: &mut Context<Self>) -> String {

crates/debugger_tools/src/dap_log.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,11 +1017,13 @@ impl SearchableItem for DapLogView {
10171017
fn update_matches(
10181018
&mut self,
10191019
matches: &[Self::Match],
1020+
active_match_index: Option<usize>,
10201021
window: &mut Window,
10211022
cx: &mut Context<Self>,
10221023
) {
1023-
self.editor
1024-
.update(cx, |e, cx| e.update_matches(matches, window, cx))
1024+
self.editor.update(cx, |e, cx| {
1025+
e.update_matches(matches, active_match_index, window, cx)
1026+
})
10251027
}
10261028

10271029
fn query_suggestion(&mut self, window: &mut Window, cx: &mut Context<Self>) -> String {

crates/debugger_ui/src/session/running/console.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,10 +252,11 @@ impl Console {
252252
let start_offset = range.start;
253253
let range = buffer.anchor_after(MultiBufferOffset(range.start))
254254
..buffer.anchor_before(MultiBufferOffset(range.end));
255+
let color_fn = color_fetcher(color);
255256
console.highlight_background_key::<ConsoleAnsiHighlight>(
256257
start_offset,
257258
&[range],
258-
color_fetcher(color),
259+
move |_, theme| color_fn(theme),
259260
cx,
260261
);
261262
}

crates/editor/src/editor.rs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -726,7 +726,10 @@ impl EditorActionId {
726726
// type GetFieldEditorTheme = dyn Fn(&theme::Theme) -> theme::FieldEditor;
727727
// type OverrideTextStyle = dyn Fn(&EditorStyle) -> Option<HighlightStyle>;
728728

729-
type BackgroundHighlight = (fn(&Theme) -> Hsla, Arc<[Range<Anchor>]>);
729+
type BackgroundHighlight = (
730+
Arc<dyn Fn(&usize, &Theme) -> Hsla + Send + Sync>,
731+
Arc<[Range<Anchor>]>,
732+
);
730733
type GutterHighlight = (fn(&App) -> Hsla, Vec<Range<Anchor>>);
731734

732735
#[derive(Default)]
@@ -6610,7 +6613,7 @@ impl Editor {
66106613
editor.update(cx, |editor, cx| {
66116614
editor.highlight_background::<Self>(
66126615
&ranges_to_highlight,
6613-
|theme| theme.colors().editor_highlighted_line_background,
6616+
|_, theme| theme.colors().editor_highlighted_line_background,
66146617
cx,
66156618
);
66166619
});
@@ -7012,12 +7015,12 @@ impl Editor {
70127015

70137016
this.highlight_background::<DocumentHighlightRead>(
70147017
&read_ranges,
7015-
|theme| theme.colors().editor_document_highlight_read_background,
7018+
|_, theme| theme.colors().editor_document_highlight_read_background,
70167019
cx,
70177020
);
70187021
this.highlight_background::<DocumentHighlightWrite>(
70197022
&write_ranges,
7020-
|theme| theme.colors().editor_document_highlight_write_background,
7023+
|_, theme| theme.colors().editor_document_highlight_write_background,
70217024
cx,
70227025
);
70237026
cx.notify();
@@ -7125,7 +7128,7 @@ impl Editor {
71257128
if !match_ranges.is_empty() {
71267129
editor.highlight_background::<SelectedTextHighlight>(
71277130
&match_ranges,
7128-
|theme| theme.colors().editor_document_highlight_bracket_background,
7131+
|_, theme| theme.colors().editor_document_highlight_bracket_background,
71297132
cx,
71307133
)
71317134
}
@@ -17519,7 +17522,7 @@ impl Editor {
1751917522
}
1752017523
editor.highlight_background::<Self>(
1752117524
&ranges,
17522-
|theme| theme.colors().editor_highlighted_line_background,
17525+
|_, theme| theme.colors().editor_highlighted_line_background,
1752317526
cx,
1752417527
);
1752517528
}
@@ -20989,7 +20992,7 @@ impl Editor {
2098920992
pub fn set_search_within_ranges(&mut self, ranges: &[Range<Anchor>], cx: &mut Context<Self>) {
2099020993
self.highlight_background::<SearchWithinRange>(
2099120994
ranges,
20992-
|colors| colors.colors().editor_document_highlight_read_background,
20995+
|_, colors| colors.colors().editor_document_highlight_read_background,
2099320996
cx,
2099420997
)
2099520998
}
@@ -21005,12 +21008,12 @@ impl Editor {
2100521008
pub fn highlight_background<T: 'static>(
2100621009
&mut self,
2100721010
ranges: &[Range<Anchor>],
21008-
color_fetcher: fn(&Theme) -> Hsla,
21011+
color_fetcher: impl Fn(&usize, &Theme) -> Hsla + Send + Sync + 'static,
2100921012
cx: &mut Context<Self>,
2101021013
) {
2101121014
self.background_highlights.insert(
2101221015
HighlightKey::Type(TypeId::of::<T>()),
21013-
(color_fetcher, Arc::from(ranges)),
21016+
(Arc::new(color_fetcher), Arc::from(ranges)),
2101421017
);
2101521018
self.scrollbar_marker_state.dirty = true;
2101621019
cx.notify();
@@ -21020,12 +21023,12 @@ impl Editor {
2102021023
&mut self,
2102121024
key: usize,
2102221025
ranges: &[Range<Anchor>],
21023-
color_fetcher: fn(&Theme) -> Hsla,
21026+
color_fetcher: impl Fn(&usize, &Theme) -> Hsla + Send + Sync + 'static,
2102421027
cx: &mut Context<Self>,
2102521028
) {
2102621029
self.background_highlights.insert(
2102721030
HighlightKey::TypePlus(TypeId::of::<T>(), key),
21028-
(color_fetcher, Arc::from(ranges)),
21031+
(Arc::new(color_fetcher), Arc::from(ranges)),
2102921032
);
2103021033
self.scrollbar_marker_state.dirty = true;
2103121034
cx.notify();
@@ -21250,7 +21253,6 @@ impl Editor {
2125021253
) -> Vec<(Range<DisplayPoint>, Hsla)> {
2125121254
let mut results = Vec::new();
2125221255
for (color_fetcher, ranges) in self.background_highlights.values() {
21253-
let color = color_fetcher(theme);
2125421256
let start_ix = match ranges.binary_search_by(|probe| {
2125521257
let cmp = probe
2125621258
.end
@@ -21263,7 +21265,7 @@ impl Editor {
2126321265
}) {
2126421266
Ok(i) | Err(i) => i,
2126521267
};
21266-
for range in &ranges[start_ix..] {
21268+
for (index, range) in ranges[start_ix..].iter().enumerate() {
2126721269
if range
2126821270
.start
2126921271
.cmp(&search_range.end, &display_snapshot.buffer_snapshot())
@@ -21272,6 +21274,7 @@ impl Editor {
2127221274
break;
2127321275
}
2127421276

21277+
let color = color_fetcher(&(start_ix + index), theme);
2127521278
let start = range.start.to_display_point(display_snapshot);
2127621279
let end = range.end.to_display_point(display_snapshot);
2127721280
results.push((start..end, color))

crates/editor/src/editor_tests.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16978,7 +16978,7 @@ fn test_highlighted_ranges(cx: &mut TestAppContext) {
1697816978
anchor_range(Point::new(6, 3)..Point::new(6, 5)),
1697916979
anchor_range(Point::new(8, 4)..Point::new(8, 6)),
1698016980
],
16981-
|_| Hsla::red(),
16981+
|_, _| Hsla::red(),
1698216982
cx,
1698316983
);
1698416984
editor.highlight_background::<Type2>(
@@ -16988,7 +16988,7 @@ fn test_highlighted_ranges(cx: &mut TestAppContext) {
1698816988
anchor_range(Point::new(7, 4)..Point::new(7, 7)),
1698916989
anchor_range(Point::new(9, 5)..Point::new(9, 8)),
1699016990
],
16991-
|_| Hsla::green(),
16991+
|_, _| Hsla::green(),
1699216992
cx,
1699316993
);
1699416994

@@ -23973,7 +23973,7 @@ async fn test_rename_with_duplicate_edits(cx: &mut TestAppContext) {
2397323973
let highlight_range = highlight_range.to_anchors(&editor.buffer().read(cx).snapshot(cx));
2397423974
editor.highlight_background::<DocumentHighlightRead>(
2397523975
&[highlight_range],
23976-
|theme| theme.colors().editor_document_highlight_read_background,
23976+
|_, theme| theme.colors().editor_document_highlight_read_background,
2397723977
cx,
2397823978
);
2397923979
});
@@ -24051,7 +24051,7 @@ async fn test_rename_without_prepare(cx: &mut TestAppContext) {
2405124051
let highlight_range = highlight_range.to_anchors(&editor.buffer().read(cx).snapshot(cx));
2405224052
editor.highlight_background::<DocumentHighlightRead>(
2405324053
&[highlight_range],
24054-
|theme| theme.colors().editor_document_highlight_read_background,
24054+
|_, theme| theme.colors().editor_document_highlight_read_background,
2405524055
cx,
2405624056
);
2405724057
});
@@ -27299,7 +27299,7 @@ let result = variable * 2;",
2729927299

2730027300
editor.highlight_background::<DocumentHighlightRead>(
2730127301
&anchor_ranges,
27302-
|theme| theme.colors().editor_document_highlight_read_background,
27302+
|_, theme| theme.colors().editor_document_highlight_read_background,
2730327303
cx,
2730427304
);
2730527305
});

crates/editor/src/hover_popover.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ fn show_hover(
518518
// Highlight the selected symbol using a background highlight
519519
editor.highlight_background::<HoverState>(
520520
&hover_highlights,
521-
|theme| theme.colors().element_hover, // todo update theme
521+
|_, theme| theme.colors().element_hover, // todo update theme
522522
cx,
523523
);
524524
}

crates/editor/src/items.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1487,6 +1487,7 @@ impl SearchableItem for Editor {
14871487
fn update_matches(
14881488
&mut self,
14891489
matches: &[Range<Anchor>],
1490+
active_match_index: Option<usize>,
14901491
_: &mut Window,
14911492
cx: &mut Context<Self>,
14921493
) {
@@ -1497,7 +1498,13 @@ impl SearchableItem for Editor {
14971498
let updated = existing_range != Some(matches);
14981499
self.highlight_background::<BufferSearchHighlights>(
14991500
matches,
1500-
|theme| theme.colors().search_match_background,
1501+
move |index, theme| {
1502+
if active_match_index == Some(*index) {
1503+
theme.colors().search_active_match_background
1504+
} else {
1505+
theme.colors().search_match_background
1506+
}
1507+
},
15011508
cx,
15021509
);
15031510
if updated {

0 commit comments

Comments
 (0)