From 21e894f8d89f3de6770085360a77d7e125d6737b Mon Sep 17 00:00:00 2001 From: TobTobXX Date: Tue, 19 Apr 2022 11:17:00 +0200 Subject: [PATCH 1/2] Implement cursorline --- book/src/configuration.md | 1 + helix-term/src/ui/editor.rs | 41 +++++++++++++++++++++++++++++++++++++ helix-view/src/editor.rs | 3 +++ 3 files changed, 45 insertions(+) diff --git a/book/src/configuration.md b/book/src/configuration.md index 4d7e440a09fe..b21cfca0dc99 100644 --- a/book/src/configuration.md +++ b/book/src/configuration.md @@ -37,6 +37,7 @@ hidden = false | `scroll-lines` | Number of lines to scroll per scroll wheel step. | `3` | | `shell` | Shell to use when running external commands. | Unix: `["sh", "-c"]`
Windows: `["cmd", "/C"]` | | `line-number` | Line number display: `absolute` simply shows each line's number, while `relative` shows the distance from the current line. When unfocused or in insert mode, `relative` will still show absolute line numbers. | `absolute` | +| `cursorline` | Highlight all lines with a cursor. | `false` | | `gutters` | Gutters to display: Available are `diagnostics` and `line-numbers`, note that `diagnostics` also includes other features like breakpoints | `["diagnostics", "line-numbers"]` | | `auto-completion` | Enable automatic pop up of auto-completion. | `true` | | `auto-format` | Enable automatic formatting on save. | `true` | diff --git a/helix-term/src/ui/editor.rs b/helix-term/src/ui/editor.rs index f074d9f1c97f..6a0db97f06dd 100644 --- a/helix-term/src/ui/editor.rs +++ b/helix-term/src/ui/editor.rs @@ -113,6 +113,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> = if is_focused { @@ -651,6 +655,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.contains(&line) { + surface.set_style(area, secondary_style); + } + } + } + pub fn render_statusline( &self, doc: &Document, diff --git a/helix-view/src/editor.rs b/helix-view/src/editor.rs index ac19def10fed..11c7ba758dc8 100644 --- a/helix-view/src/editor.rs +++ b/helix-view/src/editor.rs @@ -121,6 +121,8 @@ pub struct Config { pub shell: Vec, /// Line number mode. pub line_number: LineNumber, + /// Highlight the lines cursors are currently on. Defaults to false. + pub cursorline: bool, /// Gutters. Default ["diagnostics", "line-numbers"] pub gutters: Vec, /// Middle click paste support. Defaults to true. @@ -376,6 +378,7 @@ impl Default for Config { vec!["sh".to_owned(), "-c".to_owned()] }, line_number: LineNumber::Absolute, + cursorline: false, gutters: vec![GutterType::Diagnostics, GutterType::LineNumbers], middle_click_paste: true, auto_pairs: AutoPairConfig::default(), From df376ba3e21b38d6aee801a8273cd0570d09324c Mon Sep 17 00:00:00 2001 From: TobTobXX Date: Wed, 22 Jun 2022 12:21:54 +0200 Subject: [PATCH 2/2] Binary search possible lines --- helix-term/src/ui/editor.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/helix-term/src/ui/editor.rs b/helix-term/src/ui/editor.rs index 6a0db97f06dd..e704fadb323b 100644 --- a/helix-term/src/ui/editor.rs +++ b/helix-term/src/ui/editor.rs @@ -686,7 +686,7 @@ impl EditorView { ); if primary_line == line { surface.set_style(area, primary_style); - } else if secondary_lines.contains(&line) { + } else if secondary_lines.binary_search(&line).is_ok() { surface.set_style(area, secondary_style); } }