From 08dff863ccb028afd6f60807f4110c3c91d266e6 Mon Sep 17 00:00:00 2001 From: gbaranski Date: Tue, 10 Aug 2021 20:50:23 +0200 Subject: [PATCH 1/9] feat(helix-view): configuring line-number --- helix-view/src/editor.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/helix-view/src/editor.rs b/helix-view/src/editor.rs index ec3cedd605bd..bbaf92cb066d 100644 --- a/helix-view/src/editor.rs +++ b/helix-view/src/editor.rs @@ -29,6 +29,21 @@ pub struct Config { pub scroll_lines: isize, /// Mouse support. Defaults to true. pub mouse: bool, + /// Line number mode. + pub line_number: LineNumber, +} + +#[derive(Debug, Clone, PartialEq, Eq, Deserialize)] +#[serde(rename_all = "kebab-case")] +pub enum LineNumber { + /// No line number will be displayed + None, + + /// Show absolute line number + Absolute, + + /// Show relative line number to the primary cursor + Relative, } impl Default for Config { @@ -37,6 +52,7 @@ impl Default for Config { scrolloff: 5, scroll_lines: 3, mouse: true, + line_number: LineNumber::Absolute, } } } From 64d6cc3390c788f681e44a6e4b031028e80296ec Mon Sep 17 00:00:00 2001 From: gbaranski Date: Tue, 10 Aug 2021 20:50:32 +0200 Subject: [PATCH 2/9] feat(helix-term): relative line numbers --- helix-term/src/ui/editor.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/helix-term/src/ui/editor.rs b/helix-term/src/ui/editor.rs index aa21a3890112..8dff239758ff 100644 --- a/helix-term/src/ui/editor.rs +++ b/helix-term/src/ui/editor.rs @@ -358,7 +358,14 @@ impl EditorView { let line_number_text = if line == last_line && !draw_last { " ~".into() } else { - format!("{:>5}", line + 1) + let line_number = if true { + let current_line = doc.text().char_to_line(selections.primary().anchor); + ((current_line as isize) - (line as isize)).abs() as usize + } else { + line + 1 + }; + + format!("{:>5}", line_number) }; surface.set_stringn( viewport.x + 1 - GUTTER_OFFSET, From 4109e4540a7ddc9b8b729a5cb6570ec8c97b36d9 Mon Sep 17 00:00:00 2001 From: gbaranski Date: Tue, 10 Aug 2021 21:16:32 +0200 Subject: [PATCH 3/9] feat(helix-term): passing editor::Config to render --- helix-term/src/ui/editor.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/helix-term/src/ui/editor.rs b/helix-term/src/ui/editor.rs index 8dff239758ff..f2bdd067688f 100644 --- a/helix-term/src/ui/editor.rs +++ b/helix-term/src/ui/editor.rs @@ -71,6 +71,7 @@ impl EditorView { theme: &Theme, is_focused: bool, loader: &syntax::Loader, + config: &helix_view::editor::Config, ) { let area = Rect::new( view.area.x + GUTTER_OFFSET, @@ -79,7 +80,7 @@ impl EditorView { view.area.height.saturating_sub(1), ); // - 1 for statusline - self.render_buffer(doc, view, area, surface, theme, is_focused, loader); + self.render_buffer(doc, view, area, surface, theme, is_focused, loader, config); // if we're not at the edge of the screen, draw a right border if viewport.right() != view.area.right() { @@ -115,6 +116,7 @@ impl EditorView { theme: &Theme, is_focused: bool, loader: &syntax::Loader, + config: &helix_view::editor::Config, ) { let text = doc.text().slice(..); @@ -932,6 +934,7 @@ impl Component for EditorView { &cx.editor.theme, is_focused, loader, + &cx.editor.config, ); } From 00cdb0b7489bec17b87f529fad64fbf7134391e0 Mon Sep 17 00:00:00 2001 From: gbaranski Date: Tue, 10 Aug 2021 21:16:51 +0200 Subject: [PATCH 4/9] fix(helix-view): remove LineNumber::None --- helix-view/src/editor.rs | 3 --- 1 file changed, 3 deletions(-) diff --git a/helix-view/src/editor.rs b/helix-view/src/editor.rs index bbaf92cb066d..d23f22b6c06f 100644 --- a/helix-view/src/editor.rs +++ b/helix-view/src/editor.rs @@ -36,9 +36,6 @@ pub struct Config { #[derive(Debug, Clone, PartialEq, Eq, Deserialize)] #[serde(rename_all = "kebab-case")] pub enum LineNumber { - /// No line number will be displayed - None, - /// Show absolute line number Absolute, From f502ffb39f8ffa9224edb5ea21ed2eb2784ced06 Mon Sep 17 00:00:00 2001 From: gbaranski Date: Tue, 10 Aug 2021 21:17:10 +0200 Subject: [PATCH 5/9] feat(helix-term): rendering line-number according to configuration --- helix-term/src/ui/editor.rs | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/helix-term/src/ui/editor.rs b/helix-term/src/ui/editor.rs index f2bdd067688f..c99a64cb3a8d 100644 --- a/helix-term/src/ui/editor.rs +++ b/helix-term/src/ui/editor.rs @@ -17,6 +17,7 @@ use helix_core::{ }; use helix_view::{ document::Mode, + editor::LineNumber, graphics::{CursorKind, Modifier, Rect, Style}, info::Info, input::KeyEvent, @@ -360,14 +361,16 @@ impl EditorView { let line_number_text = if line == last_line && !draw_last { " ~".into() } else { - let line_number = if true { - let current_line = doc.text().char_to_line(selections.primary().anchor); - ((current_line as isize) - (line as isize)).abs() as usize - } else { - line + 1 - }; - - format!("{:>5}", line_number) + match config.line_number { + LineNumber::Absolute => format!("{:>5}", line + 1), + LineNumber::Relative => { + // TODO: Put char_to_line out of the loop + let current_line = doc.text().char_to_line(selections.primary().anchor); + let relative_line = + ((current_line as isize) - (line as isize)).abs() as usize; + format!("{:>5}", relative_line) + } + } }; surface.set_stringn( viewport.x + 1 - GUTTER_OFFSET, From 6d1d8d241f4f792bda3c27685f48ed48c3013032 Mon Sep 17 00:00:00 2001 From: gbaranski Date: Wed, 11 Aug 2021 17:17:23 +0200 Subject: [PATCH 6/9] fix(term): put calculating current line above line iteration --- helix-term/src/ui/editor.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/helix-term/src/ui/editor.rs b/helix-term/src/ui/editor.rs index c99a64cb3a8d..07901ca69dd2 100644 --- a/helix-term/src/ui/editor.rs +++ b/helix-term/src/ui/editor.rs @@ -339,6 +339,7 @@ impl EditorView { // document or not. We only draw it if it's not an empty line. let draw_last = text.line_to_byte(last_line) < text.len_bytes(); + let current_line = doc.text().char_to_line(selections.primary().anchor); for (i, line) in (view.first_line..(last_line + 1)).enumerate() { use helix_core::diagnostic::Severity; if let Some(diagnostic) = doc.diagnostics().iter().find(|d| d.line == line) { @@ -364,8 +365,6 @@ impl EditorView { match config.line_number { LineNumber::Absolute => format!("{:>5}", line + 1), LineNumber::Relative => { - // TODO: Put char_to_line out of the loop - let current_line = doc.text().char_to_line(selections.primary().anchor); let relative_line = ((current_line as isize) - (line as isize)).abs() as usize; format!("{:>5}", relative_line) From 4c2ab074108fd7f1e2b16cbd9b52cb1194cd8443 Mon Sep 17 00:00:00 2001 From: gbaranski Date: Wed, 11 Aug 2021 17:25:09 +0200 Subject: [PATCH 7/9] fix: add abs_diff function --- helix-term/src/ui/editor.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/helix-term/src/ui/editor.rs b/helix-term/src/ui/editor.rs index 07901ca69dd2..2f7f55070fd2 100644 --- a/helix-term/src/ui/editor.rs +++ b/helix-term/src/ui/editor.rs @@ -365,8 +365,7 @@ impl EditorView { match config.line_number { LineNumber::Absolute => format!("{:>5}", line + 1), LineNumber::Relative => { - let relative_line = - ((current_line as isize) - (line as isize)).abs() as usize; + let relative_line = abs_diff(current_line, line); format!("{:>5}", relative_line) } } @@ -1010,3 +1009,12 @@ fn canonicalize_key(key: &mut KeyEvent) { key.modifiers.remove(KeyModifiers::SHIFT) } } + +#[inline] +fn abs_diff(a: usize, b: usize) -> usize { + if a > b { + a - b + } else { + b - a + } +} From 150ab7dc1619300afbcb7c77af278e86aa5e872d Mon Sep 17 00:00:00 2001 From: gbaranski Date: Thu, 12 Aug 2021 14:07:02 +0200 Subject: [PATCH 8/9] deps: cargo update --- Cargo.lock | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c5b54272b59c..7118ca4068ea 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -31,9 +31,9 @@ checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" [[package]] name = "bitflags" -version = "1.2.1" +version = "1.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" +checksum = "2da1976d75adbe5fbc88130ecd119529cf1cc6a93ae1546d8696ee66f0d21af1" [[package]] name = "bstr" @@ -313,7 +313,6 @@ dependencies = [ "arc-swap", "etcetera", "helix-syntax", - "log", "once_cell", "quickcheck", "regex", From 4d3f437904656ca80bee136cf4f364d5430407ea Mon Sep 17 00:00:00 2001 From: gbaranski Date: Sat, 14 Aug 2021 00:23:14 +0200 Subject: [PATCH 9/9] fix: pass config argument --- helix-term/src/ui/editor.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/helix-term/src/ui/editor.rs b/helix-term/src/ui/editor.rs index fd6a53cb4d9d..5e1e8f5065a4 100644 --- a/helix-term/src/ui/editor.rs +++ b/helix-term/src/ui/editor.rs @@ -95,7 +95,7 @@ impl EditorView { }; Self::render_text_highlights(doc, offset, area, surface, theme, highlights); - Self::render_gutter(doc, view, area, surface, theme); + Self::render_gutter(doc, view, area, surface, theme, config); if is_focused { Self::render_focused_view_elements(view, doc, area, theme, surface); @@ -135,7 +135,6 @@ impl EditorView { height: u16, theme: &Theme, loader: &syntax::Loader, - config: &helix_view::editor::Config, ) -> Box + 'doc> { let text = doc.text().slice(..); let last_line = std::cmp::min( @@ -462,6 +461,7 @@ impl EditorView { viewport: Rect, surface: &mut Surface, theme: &Theme, + config: &helix_view::editor::Config, ) { let text = doc.text().slice(..); let last_line = view.last_line(doc); @@ -476,7 +476,9 @@ impl EditorView { // document or not. We only draw it if it's not an empty line. let draw_last = text.line_to_byte(last_line) < text.len_bytes(); - let current_line = doc.text().char_to_line(selections.primary().anchor); + let current_line = doc + .text() + .char_to_line(doc.selection(view.id).primary().anchor); for (i, line) in (view.first_line..(last_line + 1)).enumerate() { use helix_core::diagnostic::Severity; if let Some(diagnostic) = doc.diagnostics().iter().find(|d| d.line == line) {