From 597465b0f54718d3acfc91311ba9526679b69572 Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Sun, 10 Mar 2024 00:38:45 +0200 Subject: [PATCH] Slightly simplify editor highlights code (#9123) Prepare for git diff hunk highlights by grouping all inlay highlight properties into one struct, and removing the dead background highlight code. Release Notes: - N/A --- crates/editor/src/display_map.rs | 23 ++++++++++------ crates/editor/src/display_map/inlay_map.rs | 12 ++++---- crates/editor/src/editor.rs | 32 ++++------------------ crates/editor/src/hover_popover.rs | 7 +---- 4 files changed, 25 insertions(+), 49 deletions(-) diff --git a/crates/editor/src/display_map.rs b/crates/editor/src/display_map.rs index c0213bc331..2e3e4c0c12 100644 --- a/crates/editor/src/display_map.rs +++ b/crates/editor/src/display_map.rs @@ -339,8 +339,13 @@ impl DisplayMap { pub(crate) struct Highlights<'a> { pub text_highlights: Option<&'a TextHighlights>, pub inlay_highlights: Option<&'a InlayHighlights>, - pub inlay_highlight_style: Option, - pub suggestion_highlight_style: Option, + pub styles: HighlightStyles, +} + +#[derive(Default, Debug, Clone, Copy)] +pub struct HighlightStyles { + pub inlay_hint: Option, + pub suggestion: Option, } pub struct HighlightedChunk<'a> { @@ -516,8 +521,7 @@ impl DisplaySnapshot { &self, display_rows: Range, language_aware: bool, - inlay_highlight_style: Option, - suggestion_highlight_style: Option, + highlight_styles: HighlightStyles, ) -> DisplayChunks<'_> { self.block_snapshot.chunks( display_rows, @@ -525,8 +529,7 @@ impl DisplaySnapshot { Highlights { text_highlights: Some(&self.text_highlights), inlay_highlights: Some(&self.inlay_highlights), - inlay_highlight_style, - suggestion_highlight_style, + styles: highlight_styles, }, ) } @@ -540,8 +543,10 @@ impl DisplaySnapshot { self.chunks( display_rows, language_aware, - Some(editor_style.inlays_style), - Some(editor_style.suggestions_style), + HighlightStyles { + inlay_hint: Some(editor_style.inlay_hints_style), + suggestion: Some(editor_style.suggestions_style), + }, ) .map(|chunk| { let mut highlight_style = chunk @@ -1846,7 +1851,7 @@ pub mod tests { ) -> Vec<(String, Option, Option)> { let snapshot = map.update(cx, |map, cx| map.snapshot(cx)); let mut chunks: Vec<(String, Option, Option)> = Vec::new(); - for chunk in snapshot.chunks(rows, true, None, None) { + for chunk in snapshot.chunks(rows, true, HighlightStyles::default()) { let syntax_color = chunk .syntax_highlight_id .and_then(|id| id.style(theme)?.color); diff --git a/crates/editor/src/display_map/inlay_map.rs b/crates/editor/src/display_map/inlay_map.rs index 19c6e8970d..a64200709e 100644 --- a/crates/editor/src/display_map/inlay_map.rs +++ b/crates/editor/src/display_map/inlay_map.rs @@ -1,4 +1,4 @@ -use crate::InlayId; +use crate::{HighlightStyles, InlayId}; use collections::{BTreeMap, BTreeSet}; use gpui::HighlightStyle; use language::{Chunk, Edit, Point, TextSummary}; @@ -215,8 +215,7 @@ pub struct InlayChunks<'a> { inlay_chunk: Option<&'a str>, output_offset: InlayOffset, max_output_offset: InlayOffset, - inlay_highlight_style: Option, - suggestion_highlight_style: Option, + highlight_styles: HighlightStyles, highlight_endpoints: Peekable>, active_highlights: BTreeMap, HighlightStyle>, highlights: Highlights<'a>, @@ -307,8 +306,8 @@ impl<'a> Iterator for InlayChunks<'a> { } let mut highlight_style = match inlay.id { - InlayId::Suggestion(_) => self.suggestion_highlight_style, - InlayId::Hint(_) => self.inlay_highlight_style, + InlayId::Suggestion(_) => self.highlight_styles.suggestion, + InlayId::Hint(_) => self.highlight_styles.inlay_hint, }; let next_inlay_highlight_endpoint; let offset_in_inlay = self.output_offset - self.transforms.start().0; @@ -1052,8 +1051,7 @@ impl InlaySnapshot { buffer_chunk: None, output_offset: range.start, max_output_offset: range.end, - inlay_highlight_style: highlights.inlay_highlight_style, - suggestion_highlight_style: highlights.suggestion_highlight_style, + highlight_styles: highlights.styles, highlight_endpoints: highlight_endpoints.into_iter().peekable(), active_highlights: Default::default(), highlights, diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs index a003b948d8..f489ef9e2a 100644 --- a/crates/editor/src/editor.rs +++ b/crates/editor/src/editor.rs @@ -111,7 +111,6 @@ use std::{ time::{Duration, Instant}, }; pub use sum_tree::Bias; -use sum_tree::TreeMap; use text::{BufferId, OffsetUtf16, Rope}; use theme::{ observe_buffer_font_size_adjustment, ActiveTheme, PlayerColor, StatusColors, SyntaxTheme, @@ -323,7 +322,7 @@ pub struct EditorStyle { pub scrollbar_width: Pixels, pub syntax: Arc, pub status: StatusColors, - pub inlays_style: HighlightStyle, + pub inlay_hints_style: HighlightStyle, pub suggestions_style: HighlightStyle, } @@ -339,7 +338,7 @@ impl Default for EditorStyle { // We should look into removing the status colors from the editor // style and retrieve them directly from the theme. status: StatusColors::dark(), - inlays_style: HighlightStyle::default(), + inlay_hints_style: HighlightStyle::default(), suggestions_style: HighlightStyle::default(), } } @@ -351,7 +350,6 @@ type CompletionId = usize; // type OverrideTextStyle = dyn Fn(&EditorStyle) -> Option; type BackgroundHighlight = (fn(&ThemeColors) -> Hsla, Vec>); -type InlayBackgroundHighlight = (fn(&ThemeColors) -> Hsla, Vec); /// Zed's primary text input `View`, allowing users to edit a [`MultiBuffer`] /// @@ -390,7 +388,6 @@ pub struct Editor { placeholder_text: Option>, highlighted_rows: Option>, background_highlights: BTreeMap, - inlay_background_highlights: TreeMap, InlayBackgroundHighlight>, nav_history: Option, context_menu: RwLock>, mouse_context_menu: Option, @@ -1528,7 +1525,6 @@ impl Editor { placeholder_text: None, highlighted_rows: None, background_highlights: Default::default(), - inlay_background_highlights: Default::default(), nav_history: None, context_menu: RwLock::new(None), mouse_context_menu: None, @@ -8148,7 +8144,7 @@ impl Editor { scrollbar_width: cx.editor_style.scrollbar_width, syntax: cx.editor_style.syntax.clone(), status: cx.editor_style.status.clone(), - inlays_style: HighlightStyle { + inlay_hints_style: HighlightStyle { color: Some(cx.theme().status().hint), font_weight: Some(FontWeight::BOLD), ..HighlightStyle::default() @@ -8955,29 +8951,11 @@ impl Editor { cx.notify(); } - pub(crate) fn highlight_inlay_background( - &mut self, - ranges: Vec, - color_fetcher: fn(&ThemeColors) -> Hsla, - cx: &mut ViewContext, - ) { - // TODO: no actual highlights happen for inlays currently, find a way to do that - self.inlay_background_highlights - .insert(Some(TypeId::of::()), (color_fetcher, ranges)); - cx.notify(); - } - pub fn clear_background_highlights( &mut self, - cx: &mut ViewContext, + _cx: &mut ViewContext, ) -> Option { let text_highlights = self.background_highlights.remove(&TypeId::of::()); - let inlay_highlights = self - .inlay_background_highlights - .remove(&Some(TypeId::of::())); - if text_highlights.is_some() || inlay_highlights.is_some() { - cx.notify(); - } text_highlights } @@ -10089,7 +10067,7 @@ impl Render for Editor { scrollbar_width: px(12.), syntax: cx.theme().syntax().clone(), status: cx.theme().status().clone(), - inlays_style: HighlightStyle { + inlay_hints_style: HighlightStyle { color: Some(cx.theme().status().hint), ..HighlightStyle::default() }, diff --git a/crates/editor/src/hover_popover.rs b/crates/editor/src/hover_popover.rs index ed90fd7130..fd0e145c37 100644 --- a/crates/editor/src/hover_popover.rs +++ b/crates/editor/src/hover_popover.rs @@ -114,12 +114,7 @@ pub fn hover_at_inlay(editor: &mut Editor, inlay_hover: InlayHover, cx: &mut Vie }; this.update(&mut cx, |this, cx| { - // Highlight the selected symbol using a background highlight - this.highlight_inlay_background::( - vec![inlay_hover.range], - |theme| theme.element_hover, // todo("use a proper background here") - cx, - ); + // TODO: no background highlights happen for inlays currently this.hover_state.info_popover = Some(hover_popover); cx.notify(); })?;