Debian Codegraph @

helix-term/src/ui/text_decorations.rs

raw symbols SCIP
1 use std::cmp::Ordering;
2
3 use helix_core::doc_formatter::FormattedGrapheme;
4 use helix_core::Position;
5 use helix_view::editor::CursorCache;
6
7 use crate::ui::document::{LinePos, TextRenderer};
8
9 pub use diagnostics::InlineDiagnostics;
10
11 mod diagnostics;
12
13 /// Decorations are the primary mechanism for extending the text rendering.
14 ///
15 /// Any on-screen element which is anchored to the rendered text in some form
16 /// should be implemented using this trait. Translating char positions to
17 /// on-screen positions can be expensive and should not be done manually in the
18 /// ui loop. Instead such translations are automatically performed on the fly
19 /// while the text is being rendered. The results are provided to this trait by
20 /// the rendering infrastructure.
21 ///
22 /// To reserve space for virtual text lines (which is then filled by this trait) emit appropriate
23 /// [`LineAnnotation`](helix_core::text_annotations::LineAnnotation)s in [`helix_view::View::text_annotations`]
24 pub trait Decoration {
25 /// Called **before** a **visual** line is rendered. A visual line does not
26 /// necessarily correspond to a single line in a document as soft wrapping can
27 /// spread a single document line across multiple visual lines.
28 ///
29 /// This function is called before text is rendered as any decorations should
30 /// never overlap the document text. That means that setting the forground color
31 /// here is (essentially) useless as the text color is overwritten by the
32 /// rendered text. This _of course_ doesn't apply when rendering inside virtual lines
33 /// below the line reserved by `LineAnnotation`s as no text will be rendered here.
34 fn decorate_line(&mut self, _renderer: &mut TextRenderer, _pos: LinePos) {}
35
36 /// Called **after** a **visual** line is rendered. A visual line does not
37 /// necessarily correspond to a single line in a document as soft wrapping can
38 /// spread a single document line across multiple visual lines.
39 ///
40 /// This function is called after text is rendered so that decorations can collect
41 /// horizontal positions on the line (see [`Decoration::decorate_grapheme`]) first and
42 /// use those positions while rendering virtual text. That means that setting the foreground
43 /// color here is (essentially) useless as the text color is overwritten by the
44 /// rendered text. This _of course_ doesn't apply when rendering inside virtual lines
45 /// below the line reserved by `LineAnnotation`s. e as no text will be rendered here.
46 /// **Note**: To avoid overlapping decorations in the virtual lines, each decoration
47 /// must return the number of virtual text lines it has taken up. Each `Decoration` receives
48 /// an offset `virt_off` based on these return values where it can render virtual text:
49 ///
50 /// That means that a `render_line` implementation that returns `X` can render virtual text
51 /// in the following area:
52 ///
53 /// ```ignore
54 /// let start = inner.y + pos.virtual_line + virt_off;
55 /// start .. start + X
56 /// ````
57 fn render_virt_lines(
58 &mut self,
59 _renderer: &mut TextRenderer,
60 _pos: LinePos,
61 _virt_off: Position,
62 ) -> Position {
63 Position::new(0, 0)
64 }
65
66 fn reset_pos(&mut self, _pos: usize) -> usize {
67 usize::MAX
68 }
69
70 fn skip_concealed_anchor(&mut self, conceal_end_char_idx: usize) -> usize {
71 self.reset_pos(conceal_end_char_idx)
72 }
73
74 /// This function is called **before** the grapheme at `char_idx` is rendered.
75 ///
76 /// # Returns
77 ///
78 /// The char idx of the next grapheme that this function should be called for
79 fn decorate_grapheme(
80 &mut self,
81 _renderer: &mut TextRenderer,
82 _grapheme: &FormattedGrapheme,
83 ) -> usize {
84 usize::MAX
85 }
86 }
87
88 impl<F: FnMut(&mut TextRenderer, LinePos)> Decoration for F {
89 fn decorate_line(&mut self, renderer: &mut TextRenderer, pos: LinePos) {
90 self(renderer, pos);
91 }
92 }
93
94 #[derive(Default)]
95 pub struct DecorationManager<'a> {
96 decorations: Vec<(Box<dyn Decoration + 'a>, usize)>,
97 }
98
99 impl<'a> DecorationManager<'a> {
100 pub fn add_decoration(&mut self, decoration: impl Decoration + 'a) {
101 self.decorations.push((Box::new(decoration), 0));
102 }
103
104 pub fn prepare_for_rendering(&mut self, first_visible_char: usize) {
105 for (decoration, next_position) in &mut self.decorations {
106 *next_position = decoration.reset_pos(first_visible_char)
107 }
108 }
109
110 pub fn decorate_grapheme(&mut self, renderer: &mut TextRenderer, grapheme: &FormattedGrapheme) {
111 for (decoration, hook_char_idx) in &mut self.decorations {
112 loop {
113 match (*hook_char_idx).cmp(&grapheme.char_idx) {
114 // this grapheme has been concealed or we are at the first grapheme
115 Ordering::Less => {
116 *hook_char_idx = decoration.skip_concealed_anchor(grapheme.char_idx)
117 }
118 Ordering::Equal => {
119 *hook_char_idx = decoration.decorate_grapheme(renderer, grapheme)
120 }
121 Ordering::Greater => break,
122 }
123 }
124 }
125 }
126
127 pub fn decorate_line(&mut self, renderer: &mut TextRenderer, pos: LinePos) {
128 for (decoration, _) in &mut self.decorations {
129 decoration.decorate_line(renderer, pos);
130 }
131 }
132
133 pub fn render_virtual_lines(
134 &mut self,
135 renderer: &mut TextRenderer,
136 pos: LinePos,
137 line_width: usize,
138 ) {
139 let mut virt_off = Position::new(1, line_width); // start at 1 so the line is never overwritten
140 for (decoration, _) in &mut self.decorations {
141 virt_off += decoration.render_virt_lines(renderer, pos, virt_off);
142 }
143 }
144 }
145
146 /// Cursor rendering is done externally so all the cursor decoration
147 /// does is save the position of primary cursor
148 pub struct Cursor<'a> {
149 pub cache: &'a CursorCache,
150 pub primary_cursor: usize,
151 }
152 impl Decoration for Cursor<'_> {
153 fn reset_pos(&mut self, pos: usize) -> usize {
154 if pos <= self.primary_cursor {
155 self.primary_cursor
156 } else {
157 usize::MAX
158 }
159 }
160
161 fn decorate_grapheme(
162 &mut self,
163 renderer: &mut TextRenderer,
164 grapheme: &FormattedGrapheme,
165 ) -> usize {
166 if renderer.column_in_bounds(grapheme.visual_pos.col, grapheme.width())
167 && renderer.offset.row < grapheme.visual_pos.row
168 {
169 let position = grapheme.visual_pos - renderer.offset;
170 self.cache.set(Some(position));
171 }
172 usize::MAX
173 }
174 }