Store accessors for editor highlight colors not colors themselves

This commit is contained in:
Keith Simmons
2022-04-13 15:30:57 -07:00
parent 031472dc5a
commit 292d075f81
4 changed files with 36 additions and 29 deletions
+27 -25
View File
@@ -56,7 +56,7 @@ use std::{
}; };
pub use sum_tree::Bias; pub use sum_tree::Bias;
use text::rope::TextDimension; use text::rope::TextDimension;
use theme::DiagnosticStyle; use theme::{DiagnosticStyle, Theme};
use util::{post_inc, ResultExt, TryFutureExt}; use util::{post_inc, ResultExt, TryFutureExt};
use workspace::{ItemNavHistory, Workspace}; use workspace::{ItemNavHistory, Workspace};
@@ -401,7 +401,7 @@ pub struct Editor {
vertical_scroll_margin: f32, vertical_scroll_margin: f32,
placeholder_text: Option<Arc<str>>, placeholder_text: Option<Arc<str>>,
highlighted_rows: Option<Range<u32>>, highlighted_rows: Option<Range<u32>>,
background_highlights: BTreeMap<TypeId, (Color, Vec<Range<Anchor>>)>, background_highlights: BTreeMap<TypeId, (fn(&Theme) -> Color, Vec<Range<Anchor>>)>,
nav_history: Option<ItemNavHistory>, nav_history: Option<ItemNavHistory>,
context_menu: Option<ContextMenu>, context_menu: Option<ContextMenu>,
completion_tasks: Vec<(CompletionId, Task<Option<()>>)>, completion_tasks: Vec<(CompletionId, Task<Option<()>>)>,
@@ -2548,8 +2548,11 @@ impl Editor {
cx.add_view(|cx| Editor::for_multibuffer(excerpt_buffer, Some(project), cx)); cx.add_view(|cx| Editor::for_multibuffer(excerpt_buffer, Some(project), cx));
workspace.add_item(Box::new(editor.clone()), cx); workspace.add_item(Box::new(editor.clone()), cx);
editor.update(cx, |editor, cx| { editor.update(cx, |editor, cx| {
let color = editor.style(cx).highlighted_line_background; editor.highlight_background::<Self>(
editor.highlight_background::<Self>(ranges_to_highlight, color, cx); ranges_to_highlight,
|theme| theme.editor.highlighted_line_background,
cx,
);
}); });
}); });
@@ -2616,9 +2619,6 @@ impl Editor {
} }
let buffer_id = cursor_position.buffer_id; 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); let buffer = this.buffer.read(cx);
if !buffer if !buffer
.text_anchor_for_position(cursor_position, cx) .text_anchor_for_position(cursor_position, cx)
@@ -2665,12 +2665,12 @@ impl Editor {
this.highlight_background::<DocumentHighlightRead>( this.highlight_background::<DocumentHighlightRead>(
read_ranges, read_ranges,
read_background, |theme| theme.editor.document_highlight_read_background,
cx, cx,
); );
this.highlight_background::<DocumentHighlightWrite>( this.highlight_background::<DocumentHighlightWrite>(
write_ranges, write_ranges,
write_background, |theme| theme.editor.document_highlight_write_background,
cx, cx,
); );
cx.notify(); cx.notify();
@@ -4582,8 +4582,11 @@ impl Editor {
let editor = let editor =
cx.add_view(|cx| Editor::for_multibuffer(excerpt_buffer, Some(project), cx)); cx.add_view(|cx| Editor::for_multibuffer(excerpt_buffer, Some(project), cx));
editor.update(cx, |editor, cx| { editor.update(cx, |editor, cx| {
let color = editor.style(cx).highlighted_line_background; editor.highlight_background::<Self>(
editor.highlight_background::<Self>(ranges_to_highlight, color, cx); ranges_to_highlight,
|theme| theme.editor.highlighted_line_background,
cx,
);
}); });
workspace.add_item(Box::new(editor), cx); workspace.add_item(Box::new(editor), cx);
}); });
@@ -5651,18 +5654,18 @@ impl Editor {
pub fn highlight_background<T: 'static>( pub fn highlight_background<T: 'static>(
&mut self, &mut self,
ranges: Vec<Range<Anchor>>, ranges: Vec<Range<Anchor>>,
color: Color, color_fetcher: fn(&Theme) -> Color,
cx: &mut ViewContext<Self>, cx: &mut ViewContext<Self>,
) { ) {
self.background_highlights self.background_highlights
.insert(TypeId::of::<T>(), (color, ranges)); .insert(TypeId::of::<T>(), (color_fetcher, ranges));
cx.notify(); cx.notify();
} }
pub fn clear_background_highlights<T: 'static>( pub fn clear_background_highlights<T: 'static>(
&mut self, &mut self,
cx: &mut ViewContext<Self>, cx: &mut ViewContext<Self>,
) -> Option<(Color, Vec<Range<Anchor>>)> { ) -> Option<(fn(&Theme) -> Color, Vec<Range<Anchor>>)> {
cx.notify(); cx.notify();
self.background_highlights.remove(&TypeId::of::<T>()) self.background_highlights.remove(&TypeId::of::<T>())
} }
@@ -5676,23 +5679,20 @@ impl Editor {
let buffer = &snapshot.buffer_snapshot; let buffer = &snapshot.buffer_snapshot;
let start = buffer.anchor_before(0); let start = buffer.anchor_before(0);
let end = buffer.anchor_after(buffer.len()); let end = buffer.anchor_after(buffer.len());
self.background_highlights_in_range(start..end, &snapshot) let theme = cx.global::<Settings>().theme.as_ref();
} self.background_highlights_in_range(start..end, &snapshot, theme)
pub fn background_highlights_for_type<T: 'static>(&self) -> Option<(Color, &[Range<Anchor>])> {
self.background_highlights
.get(&TypeId::of::<T>())
.map(|(color, ranges)| (*color, ranges.as_slice()))
} }
pub fn background_highlights_in_range( pub fn background_highlights_in_range(
&self, &self,
search_range: Range<Anchor>, search_range: Range<Anchor>,
display_snapshot: &DisplaySnapshot, display_snapshot: &DisplaySnapshot,
theme: &Theme,
) -> Vec<(Range<DisplayPoint>, Color)> { ) -> Vec<(Range<DisplayPoint>, Color)> {
let mut results = Vec::new(); let mut results = Vec::new();
let buffer = &display_snapshot.buffer_snapshot; 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 start_ix = match ranges.binary_search_by(|probe| {
let cmp = probe.end.cmp(&search_range.start, &buffer); let cmp = probe.end.cmp(&search_range.start, &buffer);
if cmp.is_gt() { if cmp.is_gt() {
@@ -5715,7 +5715,7 @@ impl Editor {
.end .end
.to_point(buffer) .to_point(buffer)
.to_display_point(display_snapshot); .to_display_point(display_snapshot);
results.push((start..end, *color)) results.push((start..end, color))
} }
} }
results results
@@ -9796,7 +9796,7 @@ mod tests {
anchor_range(Point::new(6, 3)..Point::new(6, 5)), anchor_range(Point::new(6, 3)..Point::new(6, 5)),
anchor_range(Point::new(8, 4)..Point::new(8, 6)), anchor_range(Point::new(8, 4)..Point::new(8, 6)),
], ],
Color::red(), |_| Color::red(),
cx, cx,
); );
editor.highlight_background::<Type2>( editor.highlight_background::<Type2>(
@@ -9806,7 +9806,7 @@ mod tests {
anchor_range(Point::new(7, 4)..Point::new(7, 7)), anchor_range(Point::new(7, 4)..Point::new(7, 7)),
anchor_range(Point::new(9, 5)..Point::new(9, 8)), anchor_range(Point::new(9, 5)..Point::new(9, 8)),
], ],
Color::green(), |_| Color::green(),
cx, cx,
); );
@@ -9814,6 +9814,7 @@ mod tests {
let mut highlighted_ranges = editor.background_highlights_in_range( let mut highlighted_ranges = editor.background_highlights_in_range(
anchor_range(Point::new(3, 4)..Point::new(7, 4)), anchor_range(Point::new(3, 4)..Point::new(7, 4)),
&snapshot, &snapshot,
cx.global::<Settings>().theme.as_ref(),
); );
// Enforce a consistent ordering based on color without relying on the ordering of the // Enforce a consistent ordering based on color without relying on the ordering of the
// highlight's `TypeId` which is non-deterministic. // highlight's `TypeId` which is non-deterministic.
@@ -9843,6 +9844,7 @@ mod tests {
editor.background_highlights_in_range( editor.background_highlights_in_range(
anchor_range(Point::new(5, 6)..Point::new(6, 4)), anchor_range(Point::new(5, 6)..Point::new(6, 4)),
&snapshot, &snapshot,
cx.global::<Settings>().theme.as_ref(),
), ),
&[( &[(
DisplayPoint::new(6, 3)..DisplayPoint::new(6, 5), DisplayPoint::new(6, 3)..DisplayPoint::new(6, 5),
+3
View File
@@ -22,6 +22,7 @@ use gpui::{
}; };
use json::json; use json::json;
use language::{Bias, DiagnosticSeverity}; use language::{Bias, DiagnosticSeverity};
use settings::Settings;
use smallvec::SmallVec; use smallvec::SmallVec;
use std::{ use std::{
cmp::{self, Ordering}, cmp::{self, Ordering},
@@ -917,9 +918,11 @@ impl Element for EditorElement {
let display_map = view.display_map.update(cx, |map, cx| map.snapshot(cx)); let display_map = view.display_map.update(cx, |map, cx| map.snapshot(cx));
highlighted_rows = view.highlighted_rows(); highlighted_rows = view.highlighted_rows();
let theme = cx.global::<Settings>().theme.as_ref();
highlighted_ranges = view.background_highlights_in_range( highlighted_ranges = view.background_highlights_in_range(
start_anchor.clone()..end_anchor.clone(), start_anchor.clone()..end_anchor.clone(),
&display_map, &display_map,
theme,
); );
let mut remote_selections = HashMap::default(); let mut remote_selections = HashMap::default();
+1 -2
View File
@@ -544,10 +544,9 @@ impl BufferSearchBar {
} }
} }
let theme = &cx.global::<Settings>().theme.search;
editor.highlight_background::<Self>( editor.highlight_background::<Self>(
ranges, ranges,
theme.match_background, |theme| theme.search.match_background,
cx, cx,
); );
}); });
+5 -2
View File
@@ -473,8 +473,11 @@ impl ProjectSearchView {
if reset_selections { if reset_selections {
editor.select_ranges(match_ranges.first().cloned(), Some(Autoscroll::Fit), cx); editor.select_ranges(match_ranges.first().cloned(), Some(Autoscroll::Fit), cx);
} }
let theme = &cx.global::<Settings>().theme.search; editor.highlight_background::<Self>(
editor.highlight_background::<Self>(match_ranges, theme.match_background, cx); match_ranges,
|theme| theme.search.match_background,
cx,
);
}); });
if self.query_editor.is_focused(cx) { if self.query_editor.is_focused(cx) {
self.focus_results_editor(cx); self.focus_results_editor(cx);