diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs index 357e0237c74bd7d4187a95ac769742ab2f3ccebe..4822d4557076a6614b706b8ec4d27e76e14b3d00 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>)>, @@ -2553,8 +2553,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, + ); }); }); @@ -2621,9 +2624,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) @@ -2670,12 +2670,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(); @@ -4591,8 +4591,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); }); @@ -5660,18 +5663,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::()) } @@ -5685,23 +5688,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() { @@ -5724,7 +5724,7 @@ impl Editor { .end .to_point(buffer) .to_display_point(display_snapshot); - results.push((start..end, *color)) + results.push((start..end, color)) } } results @@ -9821,7 +9821,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::( @@ -9831,7 +9831,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, ); @@ -9839,6 +9839,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. @@ -9868,6 +9869,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 017c137dbc4a23a5608709f27096a07b298e9599..9893c108c0fbbec0e273262b8144519ccf91bc2e 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);