From 1ac4e519323b78b8e86001cb203d09e7567d1eee Mon Sep 17 00:00:00 2001 From: Nik Revenco <154856872+NikitaRevenco@users.noreply.github.com> Date: Tue, 18 Mar 2025 18:45:04 +0000 Subject: [PATCH] feat: figure out how to draw stuff at the end of lines --- helix-term/src/handlers.rs | 2 +- helix-term/src/ui/editor.rs | 9 ++ helix-term/src/ui/text_decorations.rs | 1 + helix-term/src/ui/text_decorations/blame.rs | 92 +++++++++++++++++++ .../src/ui/text_decorations/diagnostics.rs | 31 ++++++- 5 files changed, 132 insertions(+), 3 deletions(-) create mode 100644 helix-term/src/ui/text_decorations/blame.rs diff --git a/helix-term/src/handlers.rs b/helix-term/src/handlers.rs index 6f18d43b..2ea0bf90 100644 --- a/helix-term/src/handlers.rs +++ b/helix-term/src/handlers.rs @@ -37,6 +37,6 @@ pub fn setup(config: Arc>) -> Handlers { auto_save::register_hooks(&handlers); diagnostics::register_hooks(&handlers); snippet::register_hooks(&handlers); - blame::register_hooks(&handlers); + // blame::register_hooks(&handlers); handlers } diff --git a/helix-term/src/ui/editor.rs b/helix-term/src/ui/editor.rs index 6be56574..ff127b2f 100644 --- a/helix-term/src/ui/editor.rs +++ b/helix-term/src/ui/editor.rs @@ -201,6 +201,15 @@ impl EditorView { inline_diagnostic_config, config.end_of_line_diagnostics, )); + log::error!("{}", primary_cursor); + // if config.vcs.blame { + // decorations.add_decoration(text_decorations::blame::EolBlame::new( + // doc, + // theme, + // primary_cursor, + // "hello world".to_string(), + // )); + // } render_document( surface, inner, diff --git a/helix-term/src/ui/text_decorations.rs b/helix-term/src/ui/text_decorations.rs index 931ea431..f9d757ad 100644 --- a/helix-term/src/ui/text_decorations.rs +++ b/helix-term/src/ui/text_decorations.rs @@ -8,6 +8,7 @@ use crate::ui::document::{LinePos, TextRenderer}; pub use diagnostics::InlineDiagnostics; +pub mod blame; mod diagnostics; /// Decorations are the primary mechanism for extending the text rendering. diff --git a/helix-term/src/ui/text_decorations/blame.rs b/helix-term/src/ui/text_decorations/blame.rs new file mode 100644 index 00000000..585e9821 --- /dev/null +++ b/helix-term/src/ui/text_decorations/blame.rs @@ -0,0 +1,92 @@ +#![allow(dead_code, unused_variables, unused_mut)] + +use helix_core::doc_formatter::FormattedGrapheme; +use helix_core::Position; + +use helix_view::theme::Style; +use helix_view::{Document, Theme}; + +use crate::ui::document::{LinePos, TextRenderer}; +use crate::ui::text_decorations::Decoration; + +pub struct EolBlame<'a> { + message: String, + doc: &'a Document, + cursor: usize, + style: Style, +} + +impl<'a> EolBlame<'a> { + pub fn new(doc: &'a Document, theme: &Theme, cursor: usize, message: String) -> Self { + EolBlame { + style: theme.get("ui.virtual.blame"), + message, + doc, + cursor, + } + } +} + +impl Decoration for EolBlame<'_> { + // fn decorate_line(&mut self, renderer: &mut TextRenderer, pos: LinePos) { + // // renderer.draw_dec + // // ration_grapheme(grapheme, style, row, col) + // let col_off = 50; + // } + + fn render_virt_lines( + &mut self, + renderer: &mut TextRenderer, + pos: LinePos, + virt_off: Position, + ) -> Position { + let row = pos.visual_line; + let col = virt_off.col as u16; + // if col != self.cursor as u16 { + // return Position::new(0, 0); + // } + let style = self.style; + let width = renderer.viewport.width; + let start_col = col - renderer.offset.col as u16; + // start drawing the git blame 1 space after the end of the line + let draw_col = col + 1; + + let end_col = renderer + .column_in_bounds(draw_col as usize, 1) + .then(|| { + renderer + .set_string_truncated( + renderer.viewport.x + draw_col, + row, + &self.message, + width.saturating_sub(draw_col) as usize, + |_| self.style, + true, + false, + ) + .0 + }) + .unwrap_or(start_col); + log::error!("cursor: {}, row: {row}, col: {col}, start_col: {start_col}, draw_col: {draw_col}, end_col: {end_col}", self.cursor); + + let col_off = end_col - start_col; + + Position::new(0, col_off as usize) + } + + // fn reset_pos(&mut self, _pos: usize) -> usize { + // usize::MAX + // } + + // fn skip_concealed_anchor(&mut self, conceal_end_char_idx: usize) -> usize { + // self.reset_pos(conceal_end_char_idx) + // } + + // fn decorate_grapheme( + // &mut self, + // _renderer: &mut TextRenderer, + // _grapheme: &FormattedGrapheme, + // ) -> usize { + // usize::MAX + // } +} diff --git a/helix-term/src/ui/text_decorations/diagnostics.rs b/helix-term/src/ui/text_decorations/diagnostics.rs index fb82bcf5..df7ea439 100644 --- a/helix-term/src/ui/text_decorations/diagnostics.rs +++ b/helix-term/src/ui/text_decorations/diagnostics.rs @@ -271,14 +271,41 @@ impl Decoration for InlineDiagnostics<'_> { DiagnosticFilter::Disable => None, }; if let Some((eol_diagnostic, _)) = eol_diagnostic { - let mut renderer = Renderer { + let renderer = Renderer { renderer, first_row: pos.visual_line, row: pos.visual_line, config: &self.state.config, styles: &self.styles, }; - col_off = renderer.draw_eol_diagnostic(eol_diagnostic, pos.visual_line, virt_off.col); + // let ref mut this = renderer; + let row = pos.visual_line; + let col = virt_off.col; + let style = renderer.styles.severity_style(eol_diagnostic.severity()); + let width = renderer.renderer.viewport.width; + let start_col = (col - renderer.renderer.offset.col) as u16; + let mut end_col = start_col; + let mut draw_col = (col + 1) as u16; + + for line in eol_diagnostic.message.lines() { + if !renderer.renderer.column_in_bounds(draw_col as usize, 1) { + break; + } + + (end_col, _) = renderer.renderer.set_string_truncated( + renderer.renderer.viewport.x + draw_col, + row, + line, + width.saturating_sub(draw_col) as usize, + |_| style, + true, + false, + ); + + draw_col = end_col - renderer.renderer.viewport.x + 2; // double space between lines + } + + col_off = end_col - start_col; } self.state.compute_line_diagnostics();