Unnamed repository; edit this file 'description' to name the repository.
streamline text decoration API
This commit brings the text decoration API inline with the LineAnnotation API (so they are consistent) resulting in a single streamlined API instead of multiple ADHOK callbacks.
Pascal Kuthe 2024-07-15
parent 9a93240 · commit 2c0506a
-rw-r--r--helix-term/src/application.rs2
-rw-r--r--helix-term/src/ui/document.rs325
-rw-r--r--helix-term/src/ui/editor.rs74
-rw-r--r--helix-term/src/ui/mod.rs1
-rw-r--r--helix-term/src/ui/picker.rs14
-rw-r--r--helix-term/src/ui/text_decorations.rs175
-rw-r--r--helix-view/src/editor.rs39
7 files changed, 379 insertions, 251 deletions
diff --git a/helix-term/src/application.rs b/helix-term/src/application.rs
index 9695703b..3d10862d 100644
--- a/helix-term/src/application.rs
+++ b/helix-term/src/application.rs
@@ -290,7 +290,7 @@ impl Application {
self.compositor.render(area, surface, &mut cx);
let (pos, kind) = self.compositor.cursor(area, &self.editor);
// reset cursor cache
- self.editor.cursor_cache.set(None);
+ self.editor.cursor_cache.reset();
let pos = pos.map(|pos| (pos.col as u16, pos.row as u16));
self.terminal.draw(pos, kind).unwrap();
diff --git a/helix-term/src/ui/document.rs b/helix-term/src/ui/document.rs
index 34282b26..2da4d4b3 100644
--- a/helix-term/src/ui/document.rs
+++ b/helix-term/src/ui/document.rs
@@ -12,26 +12,10 @@ use helix_view::editor::{WhitespaceConfig, WhitespaceRenderValue};
use helix_view::graphics::Rect;
use helix_view::theme::Style;
use helix_view::view::ViewPosition;
-use helix_view::Document;
-use helix_view::Theme;
+use helix_view::{Document, Theme};
use tui::buffer::Buffer as Surface;
-pub trait LineDecoration {
- fn render_background(&mut self, _renderer: &mut TextRenderer, _pos: LinePos) {}
- fn render_foreground(
- &mut self,
- _renderer: &mut TextRenderer,
- _pos: LinePos,
- _end_char_idx: usize,
- ) {
- }
-}
-
-impl<F: FnMut(&mut TextRenderer, LinePos)> LineDecoration for F {
- fn render_background(&mut self, renderer: &mut TextRenderer, pos: LinePos) {
- self(renderer, pos)
- }
-}
+use crate::ui::text_decorations::DecorationManager;
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
enum StyleIterKind {
@@ -95,15 +79,8 @@ pub struct LinePos {
pub doc_line: usize,
/// Vertical offset from the top of the inner view area
pub visual_line: u16,
- /// The first char index of this visual line.
- /// Note that if the visual line is entirely filled by
- /// a very long inline virtual text then this index will point
- /// at the next (non-virtual) char after this visual line
- pub start_char_idx: usize,
}
-pub type TranslatedPosition<'a> = (usize, Box<dyn FnMut(&mut TextRenderer, Position) + 'a>);
-
#[allow(clippy::too_many_arguments)]
pub fn render_document(
surface: &mut Surface,
@@ -114,84 +91,46 @@ pub fn render_document(
syntax_highlight_iter: impl Iterator<Item = HighlightEvent>,
overlay_highlight_iter: impl Iterator<Item = HighlightEvent>,
theme: &Theme,
- line_decoration: &mut [Box<dyn LineDecoration + '_>],
- translated_positions: &mut [TranslatedPosition],
+ decorations: DecorationManager,
) {
- let mut renderer = TextRenderer::new(surface, doc, theme, offset.horizontal_offset, viewport);
+ let mut renderer = TextRenderer::new(
+ surface,
+ doc,
+ theme,
+ Position::new(offset.vertical_offset, offset.horizontal_offset),
+ viewport,
+ );
render_text(
&mut renderer,
doc.text().slice(..),
- offset,
+ offset.anchor,
&doc.text_format(viewport.width, Some(theme)),
doc_annotations,
syntax_highlight_iter,
overlay_highlight_iter,
theme,
- line_decoration,
- translated_positions,
+ decorations,
)
}
-fn translate_positions(
- char_pos: usize,
- first_visible_char_idx: usize,
- translated_positions: &mut [TranslatedPosition],
- text_fmt: &TextFormat,
- renderer: &mut TextRenderer,
- pos: Position,
-) {
- // check if any positions translated on the fly (like cursor) has been reached
- for (char_idx, callback) in &mut *translated_positions {
- if *char_idx < char_pos && *char_idx >= first_visible_char_idx {
- // by replacing the char_index with usize::MAX large number we ensure
- // that the same position is only translated once
- // text will never reach usize::MAX as rust memory allocations are limited
- // to isize::MAX
- *char_idx = usize::MAX;
-
- if text_fmt.soft_wrap {
- callback(renderer, pos)
- } else if pos.col >= renderer.col_offset
- && pos.col - renderer.col_offset < renderer.viewport.width as usize
- {
- callback(
- renderer,
- Position {
- row: pos.row,
- col: pos.col - renderer.col_offset,
- },
- )
- }
- }
- }
-}
-
#[allow(clippy::too_many_arguments)]
pub fn render_text<'t>(
renderer: &mut TextRenderer,
- text: RopeSlice<'t>,
- offset: ViewPosition,
+ text: RopeSlice<'_>,
+ anchor: usize,
text_fmt: &TextFormat,
text_annotations: &TextAnnotations,
syntax_highlight_iter: impl Iterator<Item = HighlightEvent>,
overlay_highlight_iter: impl Iterator<Item = HighlightEvent>,
theme: &Theme,
- line_decorations: &mut [Box<dyn LineDecoration + '_>],
- translated_positions: &mut [TranslatedPosition],
+ mut decorations: DecorationManager,
) {
- let mut row_off = visual_offset_from_block(
- text,
- offset.anchor,
- offset.anchor,
- text_fmt,
- text_annotations,
- )
- .0
- .row;
- row_off += offset.vertical_offset;
+ let row_off = visual_offset_from_block(text, anchor, anchor, text_fmt, text_annotations)
+ .0
+ .row;
let mut formatter =
- DocumentFormatter::new_at_prev_checkpoint(text, text_fmt, text_annotations, offset.anchor);
+ DocumentFormatter::new_at_prev_checkpoint(text, text_fmt, text_annotations, anchor);
let mut syntax_styles = StyleIter {
text_style: renderer.text_style,
active_highlights: Vec::with_capacity(64),
@@ -213,8 +152,8 @@ pub fn render_text<'t>(
first_visual_line: false,
doc_line: usize::MAX,
visual_line: u16::MAX,
- start_char_idx: usize::MAX,
};
+ let mut last_line_end = 0;
let mut is_in_indent_area = true;
let mut last_line_indent_level = 0;
let mut syntax_style_span = syntax_styles
@@ -223,58 +162,22 @@ pub fn render_text<'t>(
let mut overlay_style_span = overlay_styles
.next()
.unwrap_or_else(|| (Style::default(), usize::MAX));
- let mut first_visible_char_idx = formatter.next_char_pos();
+ let mut reached_view_top = false;
loop {
- // formattter.line_pos returns to line index of the next grapheme
- // so it must be called before formatter.next
let Some(mut grapheme) = formatter.next() else {
- let mut last_pos = formatter.next_visual_pos();
- if last_pos.row >= row_off {
- last_pos.col -= 1;
- last_pos.row -= row_off;
- // check if any positions translated on the fly (like cursor) are at the EOF
- translate_positions(
- text.len_chars() + 1,
- first_visible_char_idx,
- translated_positions,
- text_fmt,
- renderer,
- last_pos,
- );
- }
break;
};
// skip any graphemes on visual lines before the block start
- // if pos.row < row_off {
- // if char_pos >= syntax_style_span.1 {
- // syntax_style_span = if let Some(syntax_style_span) = syntax_styles.next() {
- // syntax_style_span
- // } else {
- // break;
- // }
- // }
- // if char_pos >= overlay_style_span.1 {
- // overlay_style_span = if let Some(overlay_style_span) = overlay_styles.next() {
- // overlay_style_span
if grapheme.visual_pos.row < row_off {
- if grapheme.char_idx >= style_span.1 {
- style_span = if let Some(style_span) = styles.next() {
- style_span
- } else {
- break;
- };
- overlay_span = if let Some(overlay_span) = overlays.next() {
- overlay_span
- } else {
- break;
- };
- }
- first_visible_char_idx = formatter.next_char_pos();
continue;
}
grapheme.visual_pos.row -= row_off;
+ if !reached_view_top {
+ decorations.prepare_for_rendering(grapheme.char_idx);
+ reached_view_top = true;
+ }
// if the end of the viewport is reached stop rendering
if grapheme.visual_pos.row as u16 >= renderer.viewport.height + renderer.offset.row as u16 {
@@ -283,87 +186,67 @@ pub fn render_text<'t>(
// apply decorations before rendering a new line
if grapheme.visual_pos.row as u16 != last_line_pos.visual_line {
- if grapheme.visual_pos.row > 0 {
+ // we initiate doc_line with usize::MAX because no file
+ // can reach that size (memory allocations are limited to isize::MAX)
+ // initially there is no "previous" line (so doc_line is set to usize::MAX)
+ // in that case we don't need to draw indent guides/virtual text
+ if last_line_pos.doc_line != usize::MAX {
// draw indent guides for the last line
- renderer
- .draw_indent_guides(last_line_indent_level, last_line_pos.visual_line as u16);
+ renderer.draw_indent_guides(last_line_indent_level, last_line_pos.visual_line);
is_in_indent_area = true;
- for line_decoration in &mut *line_decorations {
- line_decoration.render_foreground(renderer, last_line_pos, grapheme.char_idx);
- }
+ decorations.render_virtual_lines(renderer, last_line_pos, last_line_end)
}
last_line_pos = LinePos {
first_visual_line: grapheme.line_idx != last_line_pos.doc_line,
doc_line: grapheme.line_idx,
visual_line: grapheme.visual_pos.row as u16,
- start_char_idx: grapheme.char_idx,
};
- for line_decoration in &mut *line_decorations {
- line_decoration.render_background(renderer, last_line_pos);
- }
+ decorations.decorate_line(renderer, last_line_pos);
}
// acquire the correct grapheme style
- while grapheme.char_idx >= syntax_style_span.1 {
+ while grapheme.char_idx >= syntax_style_span.1 {
syntax_style_span = syntax_styles
.next()
.unwrap_or((Style::default(), usize::MAX));
}
- while grapheme.char_idx >= overlay_style_span.1 {
+ while grapheme.char_idx >= overlay_style_span.1 {
overlay_style_span = overlay_styles
.next()
.unwrap_or((Style::default(), usize::MAX));
}
- // check if any positions translated on the fly (like cursor) has been reached
- translate_positions(
- formatter.next_char_pos(),
- first_visible_char_idx,
- translated_positions,
- text_fmt,
- renderer,
- grapheme.visual_pos,
- );
-
- let (syntax_style, overlay_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))
- }
- (style, Style::default())
- } else {
- (syntax_style_span.0, overlay_style_span.0)
- };
-
- let is_virtual = grapheme.is_virtual();
- renderer.draw_grapheme(
-<<<<<<< HEAD
- grapheme.grapheme,
+ let 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));
+ }
GraphemeStyle {
- syntax_style,
- overlay_style,
- },
- is_virtual,
-||||||| parent of 5e32edd8 (track char_idx in DocFormatter)
- grapheme.grapheme,
- grapheme_style,
- virt,
-=======
+ syntax_style: style,
+ overlay_style: Style::default(),
+ }
+ } else {
+ GraphemeStyle {
+ syntax_style: syntax_style_span.0,
+ overlay_style: overlay_style_span.0,
+ }
+ };
+ decorations.decorate_grapheme(renderer, &grapheme);
+
+ let virt = grapheme.is_virtual();
+ let grapheme_width = renderer.draw_grapheme(
grapheme.raw,
grapheme_style,
virt,
->>>>>>> 5e32edd8 (track char_idx in DocFormatter)
&mut last_line_indent_level,
&mut is_in_indent_area,
grapheme.visual_pos,
);
+ last_line_end = grapheme.visual_pos.col + grapheme_width;
}
renderer.draw_indent_guides(last_line_indent_level, last_line_pos.visual_line);
- for line_decoration in &mut *line_decorations {
- line_decoration.render_foreground(renderer, last_line_pos, formatter.next_char_pos());
- }
+ decorations.render_virtual_lines(renderer, last_line_pos, last_line_end)
}
#[derive(Debug)]
@@ -382,8 +265,8 @@ pub struct TextRenderer<'a> {
pub indent_width: u16,
pub starting_indent: usize,
pub draw_indent_guides: bool,
- pub col_offset: usize,
pub viewport: Rect,
+ pub offset: Position,
}
pub struct GraphemeStyle {
@@ -396,7 +279,7 @@ impl<'a> TextRenderer<'a> {
surface: &'a mut Surface,
doc: &Document,
theme: &Theme,
- col_offset: usize,
+ offset: Position,
viewport: Rect,
) -> TextRenderer<'a> {
let editor_config = doc.config.load();
@@ -451,8 +334,8 @@ impl<'a> TextRenderer<'a> {
virtual_tab,
whitespace_style: theme.get("ui.virtual.whitespace"),
indent_width,
- starting_indent: col_offset / indent_width as usize
- + (col_offset % indent_width as usize != 0) as usize
+ starting_indent: offset.col / indent_width as usize
+ + (offset.col % indent_width as usize != 0) as usize
+ editor_config.indent_guides.skip_levels as usize,
indent_guide_style: text_style.patch(
theme
@@ -462,7 +345,7 @@ impl<'a> TextRenderer<'a> {
text_style,
draw_indent_guides: editor_config.indent_guides.render,
viewport,
- col_offset,
+ offset,
}
}
@@ -474,9 +357,13 @@ impl<'a> TextRenderer<'a> {
is_virtual: bool,
last_indent_level: &mut usize,
is_in_indent_area: &mut bool,
- position: Position,
- ) {
- let cut_off_start = self.col_offset.saturating_sub(position.col);
+ mut position: Position,
+ ) -> usize {
+ if position.row < self.offset.row {
+ return 0;
+ }
+ position.row -= self.offset.row;
+ let cut_off_start = self.offset.col.saturating_sub(position.col);
let is_whitespace = grapheme.is_whitespace();
// TODO is it correct to apply the whitespace style to all unicode white spaces?
@@ -508,12 +395,11 @@ impl<'a> TextRenderer<'a> {
Grapheme::Newline => &self.newline,
};
- let in_bounds = self.col_offset <= position.col
- && position.col < self.viewport.width as usize + self.col_offset;
+ let in_bounds = self.column_in_bounds(position.col + width - 1);
if in_bounds {
self.surface.set_string(
- self.viewport.x + (position.col - self.col_offset) as u16,
+ self.viewport.x + (position.col - self.offset.col) as u16,
self.viewport.y + position.row as u16,
grapheme,
style,
@@ -533,26 +419,33 @@ impl<'a> TextRenderer<'a> {
*last_indent_level = position.col;
*is_in_indent_area = false;
}
+
+ width
+ }
+
+ pub fn column_in_bounds(&self, colum: usize) -> bool {
+ self.offset.col <= colum && colum < self.viewport.width as usize + self.offset.col
}
/// Overlay indentation guides ontop of a rendered line
/// The indentation level is computed in `draw_lines`.
/// Therefore this function must always be called afterwards.
- pub fn draw_indent_guides(&mut self, indent_level: usize, row: u16) {
- if !self.draw_indent_guides {
+ pub fn draw_indent_guides(&mut self, indent_level: usize, mut row: u16) {
+ if !self.draw_indent_guides || self.offset.row > row as usize {
return;
}
+ row -= self.offset.row as u16;
// Don't draw indent guides outside of view
let end_indent = min(
indent_level,
// Add indent_width - 1 to round up, since the first visible
// indent might be a bit after offset.col
- self.col_offset + self.viewport.width as usize + (self.indent_width as usize - 1),
+ self.offset.col + self.viewport.width as usize + (self.indent_width as usize - 1),
) / self.indent_width as usize;
for i in self.starting_indent..end_indent {
- let x = (self.viewport.x as usize + (i * self.indent_width as usize) - self.col_offset)
+ let x = (self.viewport.x as usize + (i * self.indent_width as usize) - self.offset.col)
as u16;
let y = self.viewport.y + row;
debug_assert!(self.surface.in_bounds(x, y));
@@ -560,4 +453,62 @@ impl<'a> TextRenderer<'a> {
.set_string(x, y, &self.indent_guide_char, self.indent_guide_style);
}
}
+
+ pub fn set_string(&mut self, x: u16, y: u16, string: impl AsRef<str>, style: Style) {
+ if (y as usize) < self.offset.row {
+ return;
+ }
+ self.surface
+ .set_string(x, y + self.viewport.y, string, style)
+ }
+
+ pub fn set_stringn(
+ &mut self,
+ x: u16,
+ y: u16,
+ string: impl AsRef<str>,
+ width: usize,
+ style: Style,
+ ) {
+ if (y as usize) < self.offset.row {
+ return;
+ }
+ self.surface
+ .set_stringn(x, y + self.viewport.y, string, width, style);
+ }
+
+ /// Sets the style of an area **within the text viewport* this accounts
+ /// both for the renderers vertical offset and its viewport
+ pub fn set_style(&mut self, mut area: Rect, style: Style) {
+ area = area.clip_top(self.offset.row as u16);
+ area.y += self.viewport.y;
+ self.surface.set_style(area, style);
+ }
+
+ /// Sets the style of an area **within the text viewport* this accounts
+ /// both for the renderers vertical offset and its viewport
+ #[allow(clippy::too_many_arguments)]
+ pub fn set_string_truncated(
+ &mut self,
+ x: u16,
+ y: u16,
+ string: &str,
+ width: usize,
+ style: impl Fn(usize) -> Style, // Map a grapheme's string offset to a style
+ ellipsis: bool,
+ truncate_start: bool,
+ ) -> (u16, u16) {
+ if (y as usize) < self.offset.row {
+ return (x, y);
+ }
+ self.surface.set_string_truncated(
+ x,
+ y + self.viewport.y,
+ string,
+ width,
+ style,
+ ellipsis,
+ truncate_start,
+ )
+ }
}
diff --git a/helix-term/src/ui/editor.rs b/helix-term/src/ui/editor.rs
index a071bfaa..49386b83 100644
--- a/helix-term/src/ui/editor.rs
+++ b/helix-term/src/ui/editor.rs
@@ -5,8 +5,10 @@ use crate::{
key,
keymap::{KeymapResult, Keymaps},
ui::{
- document::{render_document, LinePos, TextRenderer, TranslatedPosition},
- Completion, ProgressSpinners,
+ document::{render_document, LinePos, TextRenderer},
+ statusline,
+ text_decorations::{self, Decoration, DecorationManager, InlineDiagnostics},
+ Completion, CompletionItem, ProgressSpinners,
},
};
@@ -31,9 +33,6 @@ use std::{mem::take, num::NonZeroUsize, path::PathBuf, rc::Rc, sync::Arc};
use tui::{buffer::Buffer as Surface, text::Span};
-use super::document::LineDecoration;
-use super::{completion::CompletionItem, statusline};
-
pub struct EditorView {
pub keymaps: Keymaps,
on_next_key: Option<OnKeyCallback>,
@@ -94,11 +93,10 @@ impl EditorView {
let config = editor.config();
let text_annotations = view.text_annotations(doc, Some(theme));
- let mut line_decorations: Vec<Box<dyn LineDecoration>> = Vec::new();
- let mut translated_positions: Vec<TranslatedPosition> = Vec::new();
+ let mut decorations = DecorationManager::default();
if is_focused && config.cursorline {
- line_decorations.push(Self::cursorline_decorator(doc, view, theme))
+ decorations.add_decoration(Self::cursorline(doc, view, theme));
}
if is_focused && config.cursorcolumn {
@@ -113,13 +111,10 @@ impl EditorView {
if pos.doc_line != dap_line {
return;
}
- renderer.surface.set_style(
- Rect::new(inner.x, inner.y + pos.visual_line, inner.width, 1),
- style,
- );
+ renderer.set_style(Rect::new(inner.x, pos.visual_line, inner.width, 1), style);
};
- line_decorations.push(Box::new(line_decoration));
+ decorations.add_decoration(line_decoration);
}
let syntax_highlights =
@@ -176,22 +171,20 @@ impl EditorView {
view.area,
theme,
is_focused & self.terminal_focused,
- &mut line_decorations,
+ &mut decorations,
);
}
+ let primary_cursor = doc
+ .selection(view.id)
+ .primary()
+ .cursor(doc.text().slice(..));
if is_focused {
- let cursor = doc
- .selection(view.id)
- .primary()
- .cursor(doc.text().slice(..));
- // set the cursor_cache to out of view in case the position is not found
- editor.cursor_cache.set(Some(None));
- let update_cursor_cache =
- |_: &mut TextRenderer, pos| editor.cursor_cache.set(Some(Some(pos)));
- translated_positions.push((cursor, Box::new(update_cursor_cache)));
+ decorations.add_decoration(text_decorations::Cursor {
+ cache: &editor.cursor_cache,
+ primary_cursor,
+ });
}
-
render_document(
surface,
inner,
@@ -201,8 +194,7 @@ impl EditorView {
syntax_highlights,
overlay_highlights,
theme,
- &mut line_decorations,
- &mut translated_positions,
+ decorations,
);
Self::render_rulers(editor, doc, view, inner, surface, theme);
@@ -637,7 +629,7 @@ impl EditorView {
viewport: Rect,
theme: &Theme,
is_focused: bool,
- line_decorations: &mut Vec<Box<(dyn LineDecoration + 'd)>>,
+ decoration_manager: &mut DecorationManager<'d>,
) {
let text = doc.text().slice(..);
let cursors: Rc<[_]> = doc
@@ -663,7 +655,7 @@ impl EditorView {
// TODO handle softwrap in gutters
let selected = cursors.contains(&pos.doc_line);
let x = viewport.x + offset;
- let y = viewport.y + pos.visual_line;
+ let y = pos.visual_line;
let gutter_style = match (selected, pos.first_visual_line) {
(false, true) => gutter_style,
@@ -675,11 +667,9 @@ impl EditorView {
if let Some(style) =
gutter(pos.doc_line, selected, pos.first_visual_line, &mut text)
{
- renderer
- .surface
- .set_stringn(x, y, &text, width, gutter_style.patch(style));
+ renderer.set_stringn(x, y, &text, width, gutter_style.patch(style));
} else {
- renderer.surface.set_style(
+ renderer.set_style(
Rect {
x,
y,
@@ -691,7 +681,7 @@ impl EditorView {
}
text.clear();
};
- line_decorations.push(Box::new(gutter_decoration));
+ decoration_manager.add_decoration(gutter_decoration);
offset += width as u16;
}
@@ -761,11 +751,7 @@ impl EditorView {
}
/// Apply the highlighting on the lines where a cursor is active
- pub fn cursorline_decorator(
- doc: &Document,
- view: &View,
- theme: &Theme,
- ) -> Box<dyn LineDecoration> {
+ pub fn cursorline(doc: &Document, view: &View, theme: &Theme) -> impl Decoration {
let text = doc.text().slice(..);
// TODO only highlight the visual line that contains the cursor instead of the full visual line
let primary_line = doc.selection(view.id).primary().cursor_line(text);
@@ -786,16 +772,14 @@ impl EditorView {
let secondary_style = theme.get("ui.cursorline.secondary");
let viewport = view.area;
- let line_decoration = move |renderer: &mut TextRenderer, pos: LinePos| {
- let area = Rect::new(viewport.x, viewport.y + pos.visual_line, viewport.width, 1);
+ move |renderer: &mut TextRenderer, pos: LinePos| {
+ let area = Rect::new(viewport.x, pos.visual_line, viewport.width, 1);
if primary_line == pos.doc_line {
- renderer.surface.set_style(area, primary_style);
+ renderer.set_style(area, primary_style);
} else if secondary_lines.binary_search(&pos.doc_line).is_ok() {
- renderer.surface.set_style(area, secondary_style);
+ renderer.set_style(area, secondary_style);
}
- };
-
- Box::new(line_decoration)
+ }
}
/// Apply the highlighting on the columns where a cursor is active
diff --git a/helix-term/src/ui/mod.rs b/helix-term/src/ui/mod.rs
index fae64062..10f4104a 100644
--- a/helix-term/src/ui/mod.rs
+++ b/helix-term/src/ui/mod.rs
@@ -12,6 +12,7 @@ mod prompt;
mod spinner;
mod statusline;
mod text;
+mod text_decorations;
use crate::compositor::Compositor;
use crate::filter_picker_entry;
diff --git a/helix-term/src/ui/picker.rs b/helix-term/src/ui/picker.rs
index 07901239..70be421a 100644
--- a/helix-term/src/ui/picker.rs
+++ b/helix-term/src/ui/picker.rs
@@ -7,8 +7,9 @@ use crate::{
ctrl, key, shift,
ui::{
self,
- document::{render_document, LineDecoration, LinePos, TextRenderer},
+ document::{render_document, LinePos, TextRenderer},
picker::query::PickerQuery,
+ text_decorations::DecorationManager,
EditorView,
},
};
@@ -895,7 +896,7 @@ impl<T: 'static + Send + Sync, D: 'static + Send + Sync> Picker<T, D> {
}
overlay_highlights = Box::new(helix_core::syntax::merge(overlay_highlights, spans));
}
- let mut decorations: Vec<Box<dyn LineDecoration>> = Vec::new();
+ let mut decorations = DecorationManager::default();
if let Some((start, end)) = range {
let style = cx
@@ -907,14 +908,14 @@ impl<T: 'static + Send + Sync, D: 'static + Send + Sync> Picker<T, D> {
if (start..=end).contains(&pos.doc_line) {
let area = Rect::new(
renderer.viewport.x,
- renderer.viewport.y + pos.visual_line,
+ pos.visual_line,
renderer.viewport.width,
1,
);
- renderer.surface.set_style(area, style)
+ renderer.set_style(area, style)
}
};
- decorations.push(Box::new(draw_highlight))
+ decorations.add_decoration(draw_highlight);
}
render_document(
@@ -927,8 +928,7 @@ impl<T: 'static + Send + Sync, D: 'static + Send + Sync> Picker<T, D> {
syntax_highlights,
overlay_highlights,
&cx.editor.theme,
- &mut decorations,
- &mut [],
+ decorations,
);
}
}
diff --git a/helix-term/src/ui/text_decorations.rs b/helix-term/src/ui/text_decorations.rs
new file mode 100644
index 00000000..630af581
--- /dev/null
+++ b/helix-term/src/ui/text_decorations.rs
@@ -0,0 +1,175 @@
+use std::cmp::Ordering;
+
+use helix_core::doc_formatter::FormattedGrapheme;
+use helix_core::Position;
+use helix_view::editor::CursorCache;
+
+use crate::ui::document::{LinePos, TextRenderer};
+
+pub use diagnostics::InlineDiagnostics;
+
+mod diagnostics;
+
+/// Decorations are the primary mechanism for extending the text rendering.
+///
+/// Any on-screen element which is anchored to the rendered text in some form
+/// should be implemented using this trait. Translating char positions to
+/// on-screen positions can be expensive and should not be done manually in the
+/// ui loop. Instead such translations are automatically performed on the fly
+/// while the text is being rendered. The results are provided to this trait by
+/// the rendering infrastructure.
+///
+/// To reserve space for virtual text lines (which is then filled by this trait) emit appropriate
+/// [`LineAnnotation`](helix_core::text_annotations::LineAnnotation)s in [`helix_view::View::text_annotations`]
+pub trait Decoration {
+ /// Called **before** a **visual** line is rendered. A visual line does not
+ /// necessarily correspond to a single line in a document as soft wrapping can
+ /// spread a single document line across multiple visual lines.
+ ///
+ /// This function is called before text is rendered as any decorations should
+ /// never overlap the document text. That means that setting the forground color
+ /// here is (essentially) useless as the text color is overwritten by the
+ /// rendered text. This _of course_ doesn't apply when rendering inside virtual lines
+ /// below the line reserved by `LineAnnotation`s as no text will be rendered here.
+ fn decorate_line(&mut self, _renderer: &mut TextRenderer, _pos: LinePos) {}
+
+ /// Called **after** a **visual** line is rendered. A visual line does not
+ /// necessarily correspond to a single line in a document as soft wrapping can
+ /// spread a single document line across multiple visual lines.
+ ///
+ /// This function is called after text is rendered so that decorations can collect
+ /// horizontal positions on the line (see [`Decoration::decorate_grapheme`]) first and
+ /// use those positions` while rendering
+ /// virtual text.
+ /// That means that setting the forground color
+ /// here is (essentially) useless as the text color is overwritten by the
+ /// rendered text. This -ofcourse- doesn't apply when rendering inside virtual lines
+ /// below the line reserved by `LineAnnotation`s. e as no text will be rendered here.
+ /// **Note**: To avoid overlapping decorations in the virtual lines, each decoration
+ /// must return the number of virtual text lines it has taken up. Each `Decoration` recieves
+ /// an offset `virt_off` based on these return values where it can render virtual text:
+ ///
+ /// That means that a `render_line` implementation that returns `X` can render virtual text
+ /// in the following area:
+ /// ``` no-compile
+ /// let start = inner.y + pos.virtual_line + virt_off;
+ /// start .. start + X
+ /// ````
+ fn render_virt_lines(
+ &mut self,
+ _renderer: &mut TextRenderer,
+ _pos: LinePos,
+ _virt_off: Position,
+ ) -> Position {
+ Position::new(0, 0)
+ }
+
+ 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)
+ }
+
+ /// This function is called **before** the grapheme at `char_idx` is rendered.
+ ///
+ /// # Returns
+ ///
+ /// The char idx of the next grapheme that this function should be called for
+ fn decorate_grapheme(
+ &mut self,
+ _renderer: &mut TextRenderer,
+ _grapheme: &FormattedGrapheme,
+ ) -> usize {
+ usize::MAX
+ }
+}
+
+impl<F: FnMut(&mut TextRenderer, LinePos)> Decoration for F {
+ fn decorate_line(&mut self, renderer: &mut TextRenderer, pos: LinePos) {
+ self(renderer, pos);
+ }
+}
+
+#[derive(Default)]
+pub struct DecorationManager<'a> {
+ decorations: Vec<(Box<dyn Decoration + 'a>, usize)>,
+}
+
+impl<'a> DecorationManager<'a> {
+ pub fn add_decoration(&mut self, decoration: impl Decoration + 'a) {
+ self.decorations.push((Box::new(decoration), 0));
+ }
+
+ pub fn prepare_for_rendering(&mut self, first_visible_char: usize) {
+ for (decoration, next_position) in &mut self.decorations {
+ *next_position = decoration.reset_pos(first_visible_char)
+ }
+ }
+
+ pub fn decorate_grapheme(&mut self, renderer: &mut TextRenderer, grapheme: &FormattedGrapheme) {
+ for (decoration, hook_char_idx) in &mut self.decorations {
+ loop {
+ match (*hook_char_idx).cmp(&grapheme.char_idx) {
+ // this grapheme has been concealed or we are at the first grapheme
+ Ordering::Less => {
+ *hook_char_idx = decoration.skip_concealed_anchor(grapheme.char_idx)
+ }
+ Ordering::Equal => {
+ *hook_char_idx = decoration.decorate_grapheme(renderer, grapheme)
+ }
+ Ordering::Greater => break,
+ }
+ }
+ }
+ }
+
+ pub fn decorate_line(&mut self, renderer: &mut TextRenderer, pos: LinePos) {
+ for (decoration, _) in &mut self.decorations {
+ decoration.decorate_line(renderer, pos);
+ }
+ }
+
+ pub fn render_virtual_lines(
+ &mut self,
+ renderer: &mut TextRenderer,
+ pos: LinePos,
+ line_width: usize,
+ ) {
+ let mut virt_off = Position::new(1, line_width); // start at 1 so the line is never overwritten
+ for (decoration, _) in &mut self.decorations {
+ virt_off += decoration.render_virt_lines(renderer, pos, virt_off);
+ }
+ }
+}
+
+/// Cursor rendering is done externally so all the cursor decoration
+/// does is save the position of primary cursor
+pub struct Cursor<'a> {
+ pub cache: &'a CursorCache,
+ pub primary_cursor: usize,
+}
+impl Decoration for Cursor<'_> {
+ fn reset_pos(&mut self, pos: usize) -> usize {
+ if pos <= self.primary_cursor {
+ self.primary_cursor
+ } else {
+ usize::MAX
+ }
+ }
+
+ fn decorate_grapheme(
+ &mut self,
+ renderer: &mut TextRenderer,
+ grapheme: &FormattedGrapheme,
+ ) -> usize {
+ if renderer.column_in_bounds(grapheme.visual_pos.col)
+ && renderer.offset.row < grapheme.visual_pos.row
+ {
+ let position = grapheme.visual_pos - renderer.offset;
+ self.cache.set(Some(position));
+ }
+ usize::MAX
+ }
+}
diff --git a/helix-view/src/editor.rs b/helix-view/src/editor.rs
index 29059095..fb8438be 100644
--- a/helix-view/src/editor.rs
+++ b/helix-view/src/editor.rs
@@ -1070,10 +1070,10 @@ pub struct Editor {
/// This cache is only a performance optimization to
/// avoid calculating the cursor position multiple
/// times during rendering and should not be set by other functions.
- pub cursor_cache: Cell<Option<Option<Position>>>,
pub handlers: Handlers,
pub mouse_down_range: Option<Range>,
+ pub cursor_cache: CursorCache,
}
pub type Motion = Box<dyn Fn(&mut Editor)>;
@@ -1188,9 +1188,9 @@ impl Editor {
exit_code: 0,
config_events: unbounded_channel(),
needs_redraw: false,
- cursor_cache: Cell::new(None),
handlers,
mouse_down_range: None,
+ cursor_cache: CursorCache::default(),
}
}
@@ -1985,15 +1985,7 @@ impl Editor {
pub fn cursor(&self) -> (Option<Position>, CursorKind) {
let config = self.config();
let (view, doc) = current_ref!(self);
- let cursor = doc
- .selection(view.id)
- .primary()
- .cursor(doc.text().slice(..));
- let pos = self
- .cursor_cache
- .get()
- .unwrap_or_else(|| view.screen_coords_at_pos(doc, doc.text().slice(..), cursor));
- if let Some(mut pos) = pos {
+ if let Some(mut pos) = self.cursor_cache.get(view, doc) {
let inner = view.inner_area(doc);
pos.col += inner.x as usize;
pos.row += inner.y as usize;
@@ -2188,3 +2180,28 @@ fn try_restore_indent(doc: &mut Document, view: &mut View) {
doc.apply(&transaction, view.id);
}
}
+
+#[derive(Default)]
+pub struct CursorCache(Cell<Option<Option<Position>>>);
+
+impl CursorCache {
+ pub fn get(&self, view: &View, doc: &Document) -> Option<Position> {
+ if let Some(pos) = self.0.get() {
+ return pos;
+ }
+
+ let text = doc.text().slice(..);
+ let cursor = doc.selection(view.id).primary().cursor(text);
+ let res = view.screen_coords_at_pos(doc, text, cursor);
+ self.set(res);
+ res
+ }
+
+ pub fn set(&self, cursor_pos: Option<Position>) {
+ self.0.set(Some(cursor_pos))
+ }
+
+ pub fn reset(&self) {
+ self.0.set(None)
+ }
+}