| 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 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
pub trait Decoration { |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
fn decorate_line(&mut self, _renderer: &mut TextRenderer, _pos: LinePos) {} |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 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 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 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 |
|
| 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); |
| 140 |
for (decoration, _) in &mut self.decorations { |
| 141 |
virt_off += decoration.render_virt_lines(renderer, pos, virt_off); |
| 142 |
} |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
|
| 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 |
} |