From 986cbf6b88f30be95ac0c756b85c76f1a15a6e4e Mon Sep 17 00:00:00 2001 From: Aiko Mastboom Date: Mon, 1 Aug 2022 20:48:25 +0200 Subject: [PATCH] Remove last Spacer but keep its offset when Spacer is last gutter item. --- book/src/configuration.md | 2 +- helix-view/src/editor.rs | 8 +++-- helix-view/src/view.rs | 70 ++++++++++++++++++++++++++++++++++----- 3 files changed, 69 insertions(+), 11 deletions(-) diff --git a/book/src/configuration.md b/book/src/configuration.md index fdabe7687dd3..f49fcf29d503 100644 --- a/book/src/configuration.md +++ b/book/src/configuration.md @@ -41,7 +41,7 @@ You may also specify a file to use for configuration with the `-c` or | `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` and `spacer`, note that `diagnostics` also includes other features like breakpoints, 1-width padding will be inserted if gutters is non-empty | `["diagnostics", "line-numbers"]` | +| `gutters` | Gutters to display: Available are `diagnostics` and `line-numbers` and `spacer`, note that `diagnostics` also includes other features like breakpoints. | `["diagnostics", "line-numbers", "spacer"]` | | `auto-completion` | Enable automatic pop up of auto-completion. | `true` | | `auto-format` | Enable automatic formatting on save. | `true` | | `idle-timeout` | Time in milliseconds since last keypress before idle timers trigger. Used for autocompletion, set to 0 for instant. | `400` | diff --git a/helix-view/src/editor.rs b/helix-view/src/editor.rs index b6947e445a6b..3d59aa0bdfb2 100644 --- a/helix-view/src/editor.rs +++ b/helix-view/src/editor.rs @@ -124,7 +124,7 @@ pub struct Config { pub line_number: LineNumber, /// Highlight the lines cursors are currently on. Defaults to false. pub cursorline: bool, - /// Gutters. Default ["diagnostics", "line-numbers"] + /// Gutters. Default ["diagnostics", "line-numbers", "spacer"] pub gutters: Vec, /// Middle click paste support. Defaults to true. pub middle_click_paste: bool, @@ -557,7 +557,11 @@ impl Default for Config { }, line_number: LineNumber::Absolute, cursorline: false, - gutters: vec![GutterType::Diagnostics, GutterType::LineNumbers], + gutters: vec![ + GutterType::Diagnostics, + GutterType::LineNumbers, + GutterType::Spacer, + ], middle_click_paste: true, auto_pairs: AutoPairConfig::default(), auto_completion: true, diff --git a/helix-view/src/view.rs b/helix-view/src/view.rs index 3df533dfc6d9..a2d7245a19a6 100644 --- a/helix-view/src/view.rs +++ b/helix-view/src/view.rs @@ -101,6 +101,7 @@ impl View { pub fn new(doc: DocumentId, gutter_types: Vec) -> Self { let mut gutters: Vec<(Gutter, usize)> = vec![]; let mut gutter_offset = 0; + let mut last_gutter_is_spacer = false; use crate::editor::GutterType; for gutter_type in &gutter_types { let width = match gutter_type { @@ -117,9 +118,11 @@ impl View { }, width as usize, )); + last_gutter_is_spacer = matches!(gutter_type, GutterType::Spacer); } - if !gutter_types.is_empty() { - gutter_offset += 1; + if last_gutter_is_spacer && !gutter_types.is_empty() { + // pop from gutters, but leave the offset. + gutters.pop(); } Self { id: ViewId::default(), @@ -340,16 +343,20 @@ impl View { mod tests { use super::*; use helix_core::Rope; - const OFFSET: u16 = 7; // 1 diagnostic + 5 linenr + 1 gutter - const OFFSET_WITHOUT_LINE_NUMBERS: u16 = 2; // 1 diagnostic + 1 gutter - // const OFFSET: u16 = GUTTERS.iter().map(|(_, width)| *width as u16).sum(); + const OFFSET: u16 = 7; // 1 diagnostic + 5 linenr + 1 spacer + const OFFSET_WITHOUT_LINE_NUMBERS: u16 = 1; // 1 diagnostic + use crate::editor::GutterType; #[test] fn test_text_pos_at_screen_coords() { let mut view = View::new( DocumentId::default(), - vec![GutterType::Diagnostics, GutterType::LineNumbers], + vec![ + GutterType::Diagnostics, + GutterType::LineNumbers, + GutterType::Spacer, + ], ); view.area = Rect::new(40, 40, 40, 40); let rope = Rope::from_str("abc\n\tdef"); @@ -418,11 +425,54 @@ mod tests { ); } + #[test] + fn test_text_pos_at_screen_coords_with_spacer_gutters() { + let mut view = View::new( + DocumentId::default(), + vec![ + GutterType::Diagnostics, + GutterType::Spacer, + GutterType::LineNumbers, + GutterType::Spacer, + ], + ); + view.area = Rect::new(40, 40, 40, 40); + let rope = Rope::from_str("abc\n\tdef"); + let text = rope.slice(..); + assert_eq!( + view.text_pos_at_screen_coords(&text, 41, 40 + 1 + 1 + 5 + 1, 4), + Some(4) + ); + } + + #[test] + fn test_text_pos_at_screen_coords_without_last_spacer_gutters() { + let mut view = View::new( + DocumentId::default(), + vec![ + GutterType::Diagnostics, + GutterType::Spacer, + GutterType::LineNumbers, + ], + ); + view.area = Rect::new(40, 40, 40, 40); + let rope = Rope::from_str("abc\n\tdef"); + let text = rope.slice(..); + assert_eq!( + view.text_pos_at_screen_coords(&text, 41, 40 + 1 + 1 + 5, 4), + Some(4) + ); + } + #[test] fn test_text_pos_at_screen_coords_cjk() { let mut view = View::new( DocumentId::default(), - vec![GutterType::Diagnostics, GutterType::LineNumbers], + vec![ + GutterType::Diagnostics, + GutterType::LineNumbers, + GutterType::Spacer, + ], ); view.area = Rect::new(40, 40, 40, 40); let rope = Rope::from_str("Hi! こんにちは皆さん"); @@ -462,7 +512,11 @@ mod tests { fn test_text_pos_at_screen_coords_graphemes() { let mut view = View::new( DocumentId::default(), - vec![GutterType::Diagnostics, GutterType::LineNumbers], + vec![ + GutterType::Diagnostics, + GutterType::LineNumbers, + GutterType::Spacer, + ], ); view.area = Rect::new(40, 40, 40, 40); let rope = Rope::from_str("Hèl̀l̀ò world!");