Highlight find matches

Co-Authored-By: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
Max Brunsfeld 2022-01-27 15:19:52 -08:00
parent 34ed734749
commit 3abd7bc8dd
3 changed files with 21 additions and 11 deletions

View file

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

View file

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

View file

@ -318,3 +318,6 @@ status_bar_item = { extends = "$text.2", margin.right = 10 }
tab_icon_width = 13 tab_icon_width = 13
tab_icon_spacing = 4 tab_icon_spacing = 4
tab_summary_spacing = 10 tab_summary_spacing = 10
[find]
match_background = "$state.highlighted_line"