refactor: extract ColorSwatch into a separate struct

This commit is contained in:
Nikita Revenco 2024-12-20 14:46:10 +00:00
parent 0c50ce1432
commit 3f8731f003
4 changed files with 49 additions and 35 deletions

View file

@ -1254,21 +1254,22 @@ pub fn compute_inlay_hints_for_all_views(editor: &mut Editor, jobs: &mut crate::
}
}
fn compute_lines(view: &View, doc: &Document) -> (usize, usize) {
// Compute ~3 times the current view height of color swatches, that way some scrolling
// will not show half the view with hints and half without while still being faster
// than computing all the hints for the full file (which could be dozens of time
// longer than the view is).
fn lsp_annotations_area(view: &View, doc: &Document) -> (usize, usize) {
// Compute ~3 times the current view heigh, that way some scrolling
// will not show half the view with annoations half without while still being faster
// than computing all the hints for the full file
let doc_text = doc.text();
let len_lines = doc_text.len_lines();
let view_height = view.inner_height();
let first_visible_line =
doc_text.char_to_line(doc.view_offset(view.id).anchor.min(doc_text.len_chars()));
let first_line = first_visible_line.saturating_sub(view_height);
let last_line = first_visible_line
.saturating_add(view_height.saturating_mul(2))
.min(len_lines);
(first_line, last_line)
}
@ -1283,7 +1284,7 @@ fn compute_inlay_hints_for_view(
.language_servers_with_feature(LanguageServerFeature::InlayHints)
.next()?;
let (first_line, last_line) = compute_lines(view, doc);
let (first_line, last_line) = lsp_annotations_area(view, doc);
let new_doc_inlay_hints_id = DocumentInlayHintsId {
first_line,
@ -1432,14 +1433,14 @@ fn compute_color_swatches_for_view(
.language_servers_with_feature(LanguageServerFeature::ColorProvider)
.next()?;
let (first_line, last_line) = compute_lines(view, doc);
let (first_line, last_line) = lsp_annotations_area(view, doc);
let new_doc_color_swatches_id = ColorSwatchesId {
first_line,
last_line,
};
// Don't recompute the annotations in case nothing has changed about the view
// Don't recompute the color swatches in case nothing has changed about the view
if !doc.color_swatches_outdated
&& doc
.color_swatches(view_id)
@ -1514,6 +1515,7 @@ fn compute_color_swatches_for_view(
color_swatches,
},
);
doc.color_swatches_outdated = false;
},
);

View file

@ -232,6 +232,7 @@ pub fn render_text(
overlay_style: overlay_style_span.0,
}
};
decorations.decorate_grapheme(renderer, &grapheme, &mut grapheme_style.syntax_style);
let virt = grapheme.is_virtual();

View file

@ -36,6 +36,8 @@ use std::{mem::take, num::NonZeroUsize, path::PathBuf, rc::Rc, sync::Arc};
use tui::{buffer::Buffer as Surface, text::Span};
use super::text_decorations::ColorSwatch;
pub struct EditorView {
pub keymaps: Keymaps,
on_next_key: Option<(OnKeyCallback, OnKeyCallbackKind)>,
@ -208,32 +210,7 @@ impl EditorView {
));
if let Some(swatches) = doc.color_swatches(view.id) {
for (swatch, color) in swatches.color_swatches.iter().zip(swatches.colors.iter()) {
struct SwatchAnnotate {
swatch_idx: usize,
color: Color,
}
impl Decoration for SwatchAnnotate {
fn decorate_grapheme(
&mut self,
_renderer: &mut TextRenderer,
_grapheme: &FormattedGrapheme,
style: &mut Style,
) -> usize {
style.fg = Some(self.color);
usize::MAX
}
fn reset_pos(&mut self, pos: usize) -> usize {
if self.swatch_idx >= pos {
self.swatch_idx
} else {
usize::MAX
}
}
}
decorations.add_decoration(SwatchAnnotate {
color: *color,
swatch_idx: swatch.char_idx,
});
decorations.add_decoration(ColorSwatch::new(*color, swatch.char_idx));
}
}
render_document(

View file

@ -2,7 +2,10 @@ use std::cmp::Ordering;
use helix_core::doc_formatter::FormattedGrapheme;
use helix_core::Position;
use helix_view::{editor::CursorCache, theme::Style};
use helix_view::{
editor::CursorCache,
theme::{Color, Style},
};
use crate::ui::document::{LinePos, TextRenderer};
@ -150,6 +153,37 @@ impl<'a> DecorationManager<'a> {
}
}
pub struct ColorSwatch {
color: Color,
pos: usize,
}
impl ColorSwatch {
pub fn new(color: Color, pos: usize) -> Self {
ColorSwatch { color, pos }
}
}
impl Decoration for ColorSwatch {
fn decorate_grapheme(
&mut self,
_renderer: &mut TextRenderer,
_grapheme: &FormattedGrapheme,
style: &mut Style,
) -> usize {
style.fg = Some(self.color);
usize::MAX
}
fn reset_pos(&mut self, pos: usize) -> usize {
if self.pos >= pos {
self.pos
} else {
usize::MAX
}
}
}
/// Cursor rendering is done externally so all the cursor decoration
/// does is save the position of primary cursor
pub struct Cursor<'a> {