From 292d075f81d79c685dc13ce8c920342f2ba581cf Mon Sep 17 00:00:00 2001 From: Keith Simmons Date: Wed, 13 Apr 2022 15:30:57 -0700 Subject: [PATCH] Store accessors for editor highlight colors not colors themselves --- crates/editor/src/editor.rs | 52 +++++++++++++++-------------- crates/editor/src/element.rs | 3 ++ crates/search/src/buffer_search.rs | 3 +- crates/search/src/project_search.rs | 7 ++-- 4 files changed, 36 insertions(+), 29 deletions(-) diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs index bc398754a78edcf1f889e4dff1bd5328ba697f88..ea6fd38d1a451eaf085e940c355f6d5be80ca682 100644 --- a/crates/editor/src/editor.rs +++ b/crates/editor/src/editor.rs @@ -56,7 +56,7 @@ use std::{ }; pub use sum_tree::Bias; use text::rope::TextDimension; -use theme::DiagnosticStyle; +use theme::{DiagnosticStyle, Theme}; use util::{post_inc, ResultExt, TryFutureExt}; use workspace::{ItemNavHistory, Workspace}; @@ -401,7 +401,7 @@ pub struct Editor { vertical_scroll_margin: f32, placeholder_text: Option>, highlighted_rows: Option>, - background_highlights: BTreeMap>)>, + background_highlights: BTreeMap Color, Vec>)>, nav_history: Option, context_menu: Option, completion_tasks: Vec<(CompletionId, Task>)>, @@ -2548,8 +2548,11 @@ impl Editor { cx.add_view(|cx| Editor::for_multibuffer(excerpt_buffer, Some(project), cx)); workspace.add_item(Box::new(editor.clone()), cx); editor.update(cx, |editor, cx| { - let color = editor.style(cx).highlighted_line_background; - editor.highlight_background::(ranges_to_highlight, color, cx); + editor.highlight_background::( + ranges_to_highlight, + |theme| theme.editor.highlighted_line_background, + cx, + ); }); }); @@ -2616,9 +2619,6 @@ impl Editor { } let buffer_id = cursor_position.buffer_id; - let style = this.style(cx); - let read_background = style.document_highlight_read_background; - let write_background = style.document_highlight_write_background; let buffer = this.buffer.read(cx); if !buffer .text_anchor_for_position(cursor_position, cx) @@ -2665,12 +2665,12 @@ impl Editor { this.highlight_background::( read_ranges, - read_background, + |theme| theme.editor.document_highlight_read_background, cx, ); this.highlight_background::( write_ranges, - write_background, + |theme| theme.editor.document_highlight_write_background, cx, ); cx.notify(); @@ -4582,8 +4582,11 @@ impl Editor { let editor = cx.add_view(|cx| Editor::for_multibuffer(excerpt_buffer, Some(project), cx)); editor.update(cx, |editor, cx| { - let color = editor.style(cx).highlighted_line_background; - editor.highlight_background::(ranges_to_highlight, color, cx); + editor.highlight_background::( + ranges_to_highlight, + |theme| theme.editor.highlighted_line_background, + cx, + ); }); workspace.add_item(Box::new(editor), cx); }); @@ -5651,18 +5654,18 @@ impl Editor { pub fn highlight_background( &mut self, ranges: Vec>, - color: Color, + color_fetcher: fn(&Theme) -> Color, cx: &mut ViewContext, ) { self.background_highlights - .insert(TypeId::of::(), (color, ranges)); + .insert(TypeId::of::(), (color_fetcher, ranges)); cx.notify(); } pub fn clear_background_highlights( &mut self, cx: &mut ViewContext, - ) -> Option<(Color, Vec>)> { + ) -> Option<(fn(&Theme) -> Color, Vec>)> { cx.notify(); self.background_highlights.remove(&TypeId::of::()) } @@ -5676,23 +5679,20 @@ impl Editor { let buffer = &snapshot.buffer_snapshot; let start = buffer.anchor_before(0); let end = buffer.anchor_after(buffer.len()); - self.background_highlights_in_range(start..end, &snapshot) - } - - pub fn background_highlights_for_type(&self) -> Option<(Color, &[Range])> { - self.background_highlights - .get(&TypeId::of::()) - .map(|(color, ranges)| (*color, ranges.as_slice())) + let theme = cx.global::().theme.as_ref(); + self.background_highlights_in_range(start..end, &snapshot, theme) } pub fn background_highlights_in_range( &self, search_range: Range, display_snapshot: &DisplaySnapshot, + theme: &Theme, ) -> Vec<(Range, Color)> { let mut results = Vec::new(); let buffer = &display_snapshot.buffer_snapshot; - for (color, ranges) in self.background_highlights.values() { + for (color_fetcher, ranges) in self.background_highlights.values() { + let color = color_fetcher(theme); let start_ix = match ranges.binary_search_by(|probe| { let cmp = probe.end.cmp(&search_range.start, &buffer); if cmp.is_gt() { @@ -5715,7 +5715,7 @@ impl Editor { .end .to_point(buffer) .to_display_point(display_snapshot); - results.push((start..end, *color)) + results.push((start..end, color)) } } results @@ -9796,7 +9796,7 @@ mod tests { anchor_range(Point::new(6, 3)..Point::new(6, 5)), anchor_range(Point::new(8, 4)..Point::new(8, 6)), ], - Color::red(), + |_| Color::red(), cx, ); editor.highlight_background::( @@ -9806,7 +9806,7 @@ mod tests { anchor_range(Point::new(7, 4)..Point::new(7, 7)), anchor_range(Point::new(9, 5)..Point::new(9, 8)), ], - Color::green(), + |_| Color::green(), cx, ); @@ -9814,6 +9814,7 @@ mod tests { let mut highlighted_ranges = editor.background_highlights_in_range( anchor_range(Point::new(3, 4)..Point::new(7, 4)), &snapshot, + cx.global::().theme.as_ref(), ); // Enforce a consistent ordering based on color without relying on the ordering of the // highlight's `TypeId` which is non-deterministic. @@ -9843,6 +9844,7 @@ mod tests { editor.background_highlights_in_range( anchor_range(Point::new(5, 6)..Point::new(6, 4)), &snapshot, + cx.global::().theme.as_ref(), ), &[( DisplayPoint::new(6, 3)..DisplayPoint::new(6, 5), diff --git a/crates/editor/src/element.rs b/crates/editor/src/element.rs index 640ba80ac226c22ecf1bc85e614be48ffb813040..cd53e629645daaca4561af48a410823c8aa1dcdd 100644 --- a/crates/editor/src/element.rs +++ b/crates/editor/src/element.rs @@ -22,6 +22,7 @@ use gpui::{ }; use json::json; use language::{Bias, DiagnosticSeverity}; +use settings::Settings; use smallvec::SmallVec; use std::{ cmp::{self, Ordering}, @@ -917,9 +918,11 @@ impl Element for EditorElement { let display_map = view.display_map.update(cx, |map, cx| map.snapshot(cx)); highlighted_rows = view.highlighted_rows(); + let theme = cx.global::().theme.as_ref(); highlighted_ranges = view.background_highlights_in_range( start_anchor.clone()..end_anchor.clone(), &display_map, + theme, ); let mut remote_selections = HashMap::default(); diff --git a/crates/search/src/buffer_search.rs b/crates/search/src/buffer_search.rs index c81daf6f5b78b62f38d2e79cfd0e3a722d8c06ad..e0f0974b0aed996fc0bcb51d0c9cef4052a8f08c 100644 --- a/crates/search/src/buffer_search.rs +++ b/crates/search/src/buffer_search.rs @@ -544,10 +544,9 @@ impl BufferSearchBar { } } - let theme = &cx.global::().theme.search; editor.highlight_background::( ranges, - theme.match_background, + |theme| theme.search.match_background, cx, ); }); diff --git a/crates/search/src/project_search.rs b/crates/search/src/project_search.rs index 7ed4e802b300ed57d9d08ba11352c030c5c52793..fe3e2e95cf8441536f3ac21cd07ec1ebca7cb31b 100644 --- a/crates/search/src/project_search.rs +++ b/crates/search/src/project_search.rs @@ -473,8 +473,11 @@ impl ProjectSearchView { if reset_selections { editor.select_ranges(match_ranges.first().cloned(), Some(Autoscroll::Fit), cx); } - let theme = &cx.global::().theme.search; - editor.highlight_background::(match_ranges, theme.match_background, cx); + editor.highlight_background::( + match_ranges, + |theme| theme.search.match_background, + cx, + ); }); if self.query_editor.is_focused(cx) { self.focus_results_editor(cx);