Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'helix-term/src/ui/document.rs')
-rw-r--r--helix-term/src/ui/document.rs325
1 files changed, 138 insertions, 187 deletions
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,
+ )
+ }
}