on master help sign in
feat(helix-view): dynamic line numbers (#1522)

* feat(helix-view): dynamic line numbers * docs: describe editor.line-number in more detail * Make dynamic numbers the default behavior of `relative`

commit333c2949c2b036f8aafa77be2286eb3c135ea269 authorAndrew Neth <the0x539@gmail.com> dateTue 25 Jan 2022, 09:18 (5 years ago) parent48a0c80
3 files changed+17−13
book/src/configuration.md+1−1 helix-view/src/editor.rs+3−1 helix-view/src/gutter.rs+13−11
book/src/configuration.md+1−1
@@ -34,7 +34,7 @@ hidden = false
3434| `middle-click-paste` | Middle click paste support. | `true` |
3535| `scroll-lines` | Number of lines to scroll per scroll wheel step. | `3` |
3636| `shell` | Shell to use when running external commands. | Unix: `["sh", "-c"]`<br/>Windows: `["cmd", "/C"]` |
37−| `line-number` | Line number display (`absolute`, `relative`) | `absolute` |
37+| `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` |
3838| `smart-case` | Enable smart case regex searching (case insensitive unless pattern contains upper case characters) | `true` |
3939| `auto-pairs` | Enable automatic insertion of pairs to parenthese, brackets, etc. | `true` |
4040| `auto-completion` | Enable automatic pop up of auto-completion. | `true` |
helix-view/src/editor.rs+3−1
@@ -170,7 +170,9 @@ impl Default for CursorShapeConfig {
170170pub enum LineNumber {
171171 /// Show absolute line number
172172 Absolute,
173− /// Show relative line number to the primary cursor
173+
174+ /// If focused and in normal/select mode, show relative line number to the primary cursor.
175+ /// If unfocused or in insert mode, show absolute line number.
174176 Relative,
175177}
176178
helix-view/src/gutter.rs+13−11
@@ -58,29 +58,31 @@ pub fn line_number<'doc>(
5858 .char_to_line(doc.selection(view.id).primary().cursor(text));
5959
6060 let config = config.line_number;
61+ let mode = doc.mode;
6162
6263 Box::new(move |line: usize, selected: bool, out: &mut String| {
6364 if line == last_line && !draw_last {
6465 write!(out, "{:>1$}", '~', width).unwrap();
6566 Some(linenr)
6667 } else {
67− use crate::editor::LineNumber;
68− let line = match config {
69− LineNumber::Absolute => line + 1,
70− LineNumber::Relative => {
71− if current_line == line {
72− line + 1
73− } else {
74− abs_diff(current_line, line)
75− }
76− }
68+ use crate::{document::Mode, editor::LineNumber};
69+
70+ let relative = config == LineNumber::Relative
71+ && mode != Mode::Insert
72+ && is_focused
73+ && current_line != line;
74+
75+ let display_num = if relative {
76+ abs_diff(current_line, line)
77+ } else {
78+ line + 1
7779 };
7880 let style = if selected && is_focused {
7981 linenr_select
8082 } else {
8183 linenr
8284 };
83− write!(out, "{:>1$}", line, width).unwrap();
85+ write!(out, "{:>1$}", display_num, width).unwrap();
8486 Some(style)
8587 }
8688 })