aboutsummaryrefslogtreecommitdiff
path: root/helix-term/src
diff options
context:
space:
mode:
authorTobias Menzi2022-06-27 08:09:34 +0000
committerGitHub2022-06-27 08:09:34 +0000
commit8dc86beabd9334d013291656313cb33a88754c42 (patch)
tree8407d9e6d4a06e1bb2366a3bee44697c194ae4ac /helix-term/src
parenta26943de4e421a453371aa53b3bb11a2baabff56 (diff)
Implement cursorline (#2170)
* Implement cursorline * Binary search possible lines
Diffstat (limited to 'helix-term/src')
-rw-r--r--helix-term/src/ui/editor.rs41
1 files changed, 41 insertions, 0 deletions
diff --git a/helix-term/src/ui/editor.rs b/helix-term/src/ui/editor.rs
index 70f60070..2f44dae9 100644
--- a/helix-term/src/ui/editor.rs
+++ b/helix-term/src/ui/editor.rs
@@ -112,6 +112,10 @@ impl EditorView {
}
}
+ if editor.config().cursorline {
+ Self::highlight_cursorline(doc, view, surface, theme);
+ }
+
let highlights = Self::doc_syntax_highlights(doc, view.offset, inner.height, theme);
let highlights = syntax::merge(highlights, Self::doc_diagnostics_highlights(doc, theme));
let highlights: Box<dyn Iterator<Item = HighlightEvent>> = if is_focused {
@@ -686,6 +690,43 @@ impl EditorView {
);
}
+ /// Apply the highlighting on the lines where a cursor is active
+ pub fn highlight_cursorline(doc: &Document, view: &View, surface: &mut Surface, theme: &Theme) {
+ let text = doc.text().slice(..);
+ let last_line = view.last_line(doc);
+
+ let primary_line = doc.selection(view.id).primary().cursor_line(text);
+
+ // The secondary_lines do contain the primary_line, it doesn't matter
+ // as the else-if clause in the loop later won't test for the
+ // secondary_lines if primary_line == line.
+ // It's used inside a loop so the collect isn't needless:
+ // https://github.com/rust-lang/rust-clippy/issues/6164
+ #[allow(clippy::needless_collect)]
+ let secondary_lines: Vec<_> = doc
+ .selection(view.id)
+ .iter()
+ .map(|range| range.cursor_line(text))
+ .collect();
+
+ let primary_style = theme.get("ui.cursorline.primary");
+ let secondary_style = theme.get("ui.cursorline.secondary");
+
+ for line in view.offset.row..(last_line + 1) {
+ let area = Rect::new(
+ view.area.x,
+ view.area.y + (line - view.offset.row) as u16,
+ view.area.width,
+ 1,
+ );
+ if primary_line == line {
+ surface.set_style(area, primary_style);
+ } else if secondary_lines.binary_search(&line).is_ok() {
+ surface.set_style(area, secondary_style);
+ }
+ }
+ }
+
pub fn render_statusline(
&self,
doc: &Document,