Highlight find matches

Max Brunsfeld and Nathan Sobo created

Co-Authored-By: Nathan Sobo <nathan@zed.dev>

Change summary

crates/find/src/find.rs             | 21 +++++++++++----------
crates/theme/src/theme.rs           |  6 ++++++
crates/zed/assets/themes/_base.toml |  5 ++++-
3 files changed, 21 insertions(+), 11 deletions(-)

Detailed changes

crates/find/src/find.rs 🔗

@@ -110,22 +110,23 @@ impl FindBar {
     ) {
         if let Some(editor) = &self.active_editor {
             let search = self.query_editor.read(cx).text(cx);
-            if search.is_empty() {
-                return;
-            }
-            let search = AhoCorasick::new_auto_configured(&[search]);
+            let theme = &self.settings.borrow().theme.find;
             editor.update(cx, |editor, cx| {
+                if search.is_empty() {
+                    editor.clear_highlighted_ranges::<Self>(cx);
+                    return;
+                }
+
+                let search = AhoCorasick::new_auto_configured(&[search]);
                 let buffer = editor.buffer().read(cx).snapshot(cx);
-                let mut ranges = search
+                let ranges = search
                     .stream_find_iter(buffer.bytes_in_range(0..buffer.len()))
                     .map(|mat| {
                         let mat = mat.unwrap();
-                        mat.start()..mat.end()
+                        buffer.anchor_after(mat.start())..buffer.anchor_before(mat.end())
                     })
-                    .peekable();
-                if ranges.peek().is_some() {
-                    editor.select_ranges(ranges, None, cx);
-                }
+                    .collect();
+                editor.highlight_ranges::<Self>(ranges, theme.match_background, cx);
             });
         }
     }

crates/theme/src/theme.rs 🔗

@@ -24,6 +24,7 @@ pub struct Theme {
     pub project_panel: ProjectPanel,
     pub selector: Selector,
     pub editor: EditorStyle,
+    pub find: Find,
     pub project_diagnostics: ProjectDiagnostics,
 }
 
@@ -87,6 +88,11 @@ pub struct Tab {
     pub icon_conflict: Color,
 }
 
+#[derive(Clone, Deserialize, Default)]
+pub struct Find {
+    pub match_background: Color,
+}
+
 #[derive(Deserialize, Default)]
 pub struct Sidebar {
     #[serde(flatten)]

crates/zed/assets/themes/_base.toml 🔗

@@ -185,7 +185,7 @@ corner_radius = 6
 
 [project_panel]
 extends = "$panel"
-padding.top = 6 # ($workspace.tab.height - $project_panel.entry.height) / 2
+padding.top = 6    # ($workspace.tab.height - $project_panel.entry.height) / 2
 
 [project_panel.entry]
 text = "$text.1"
@@ -318,3 +318,6 @@ status_bar_item = { extends = "$text.2", margin.right = 10 }
 tab_icon_width = 13
 tab_icon_spacing = 4
 tab_summary_spacing = 10
+
+[find]
+match_background = "$state.highlighted_line"