From 68c52a24352f746f48653af0970eaf0f2d447c1d Mon Sep 17 00:00:00 2001 From: Gabriel Dinner-David Date: Thu, 19 Dec 2024 23:04:27 -0500 Subject: [PATCH] hack the text_decorations to make this work --- helix-term/src/ui/document.rs | 5 ++-- helix-term/src/ui/editor.rs | 25 +++++++++++++++++++ helix-term/src/ui/text_decorations.rs | 13 +++++++--- .../src/ui/text_decorations/diagnostics.rs | 1 + 4 files changed, 39 insertions(+), 5 deletions(-) diff --git a/helix-term/src/ui/document.rs b/helix-term/src/ui/document.rs index d1a74e7e..932bcc9d 100644 --- a/helix-term/src/ui/document.rs +++ b/helix-term/src/ui/document.rs @@ -216,7 +216,8 @@ pub fn render_text( .unwrap_or((Style::default(), usize::MAX)); } - let grapheme_style = if let GraphemeSource::VirtualText { highlight } = grapheme.source { + let mut grapheme_style = if let GraphemeSource::VirtualText { highlight } = grapheme.source + { let mut style = renderer.text_style; if let Some(highlight) = highlight { style = style.patch(theme.highlight(highlight.0)); @@ -231,7 +232,7 @@ pub fn render_text( overlay_style: overlay_style_span.0, } }; - decorations.decorate_grapheme(renderer, &grapheme); + decorations.decorate_grapheme(renderer, &grapheme, &mut grapheme_style.syntax_style); let virt = grapheme.is_virtual(); let grapheme_width = renderer.draw_grapheme( diff --git a/helix-term/src/ui/editor.rs b/helix-term/src/ui/editor.rs index c29d0499..7a1345e9 100644 --- a/helix-term/src/ui/editor.rs +++ b/helix-term/src/ui/editor.rs @@ -15,6 +15,7 @@ use crate::{ use helix_core::{ diagnostic::NumberOrString, + doc_formatter::FormattedGrapheme, graphemes::{next_grapheme_boundary, prev_grapheme_boundary}, movement::Direction, syntax::{self, HighlightEvent}, @@ -205,6 +206,30 @@ impl EditorView { inline_diagnostic_config, config.end_of_line_diagnostics, )); + if let Some(swatches) = doc.color_swatches(view.id) { + for swatch in swatches.color_swatches.clone() { + struct SwatchAnnotate { + swatch_idx: usize, + } + impl Decoration for SwatchAnnotate { + fn decorate_grapheme( + &mut self, + _renderer: &mut TextRenderer, + _grapheme: &FormattedGrapheme, + style: &mut Style, + ) -> usize { + style.fg = Some(Color::Rgb(4, 12, 28)); + usize::MAX + } + fn reset_pos(&mut self, _pos: usize) -> usize { + self.swatch_idx + } + } + decorations.add_decoration(SwatchAnnotate { + swatch_idx: swatch.char_idx, + }); + } + } render_document( surface, inner, diff --git a/helix-term/src/ui/text_decorations.rs b/helix-term/src/ui/text_decorations.rs index 931ea431..4f4e9c13 100644 --- a/helix-term/src/ui/text_decorations.rs +++ b/helix-term/src/ui/text_decorations.rs @@ -2,7 +2,7 @@ use std::cmp::Ordering; use helix_core::doc_formatter::FormattedGrapheme; use helix_core::Position; -use helix_view::editor::CursorCache; +use helix_view::{editor::CursorCache, theme::Style}; use crate::ui::document::{LinePos, TextRenderer}; @@ -81,6 +81,7 @@ pub trait Decoration { &mut self, _renderer: &mut TextRenderer, _grapheme: &FormattedGrapheme, + _style: &mut Style, ) -> usize { usize::MAX } @@ -108,7 +109,12 @@ impl<'a> DecorationManager<'a> { } } - pub fn decorate_grapheme(&mut self, renderer: &mut TextRenderer, grapheme: &FormattedGrapheme) { + pub fn decorate_grapheme( + &mut self, + renderer: &mut TextRenderer, + grapheme: &FormattedGrapheme, + style: &mut Style, + ) { for (decoration, hook_char_idx) in &mut self.decorations { loop { match (*hook_char_idx).cmp(&grapheme.char_idx) { @@ -117,7 +123,7 @@ impl<'a> DecorationManager<'a> { *hook_char_idx = decoration.skip_concealed_anchor(grapheme.char_idx) } Ordering::Equal => { - *hook_char_idx = decoration.decorate_grapheme(renderer, grapheme) + *hook_char_idx = decoration.decorate_grapheme(renderer, grapheme, style) } Ordering::Greater => break, } @@ -163,6 +169,7 @@ impl Decoration for Cursor<'_> { &mut self, renderer: &mut TextRenderer, grapheme: &FormattedGrapheme, + _style: &mut Style, ) -> usize { if renderer.column_in_bounds(grapheme.visual_pos.col, grapheme.width()) && renderer.offset.row < grapheme.visual_pos.row diff --git a/helix-term/src/ui/text_decorations/diagnostics.rs b/helix-term/src/ui/text_decorations/diagnostics.rs index fb82bcf5..8b5c0a5e 100644 --- a/helix-term/src/ui/text_decorations/diagnostics.rs +++ b/helix-term/src/ui/text_decorations/diagnostics.rs @@ -307,6 +307,7 @@ impl Decoration for InlineDiagnostics<'_> { &mut self, renderer: &mut TextRenderer, grapheme: &FormattedGrapheme, + _style: &mut Style, ) -> usize { self.state .proccess_anchor(grapheme, renderer.viewport.width, renderer.offset.col)