From 73ffb0c4326a49ad79a0372c6d24c7679e977f62 Mon Sep 17 00:00:00 2001 From: Gokul Soumya Date: Tue, 31 May 2022 22:43:08 +0530 Subject: [PATCH] Make indent guides configurable --- book/src/configuration.md | 17 +++++++++++++++++ helix-term/src/ui/editor.rs | 20 ++++++++++++-------- helix-term/src/ui/picker.rs | 2 +- helix-view/src/editor.rs | 19 +++++++++++++++++++ 4 files changed, 49 insertions(+), 9 deletions(-) diff --git a/book/src/configuration.md b/book/src/configuration.md index 4d7e440a09fe..3fa9b307ab38 100644 --- a/book/src/configuration.md +++ b/book/src/configuration.md @@ -167,3 +167,20 @@ nbsp = "⍽" tab = "→" newline = "⏎" ``` + +### `[editor.indent-guides]` Section + +Options for rendering vertical indent guides. + +| Key | Description | Default | +| --- | --- | --- | +| `render` | Whether to render indent guides. | `false` | +| `character` | Literal character to use for rendering the indent guide | `│` | + +Example: + +```toml +[editor.indent-guides] +render = true +character = "╎" +``` diff --git a/helix-term/src/ui/editor.rs b/helix-term/src/ui/editor.rs index a7cb0e972f8f..38ec4715e148 100644 --- a/helix-term/src/ui/editor.rs +++ b/helix-term/src/ui/editor.rs @@ -131,7 +131,7 @@ impl EditorView { surface, theme, highlights, - &editor.config().whitespace, + &editor.config(), ); Self::render_gutter(editor, doc, view, view.area, surface, theme, is_focused); Self::render_rulers(editor, doc, view, inner, surface, theme); @@ -371,8 +371,9 @@ impl EditorView { surface: &mut Surface, theme: &Theme, highlights: H, - whitespace: &helix_view::editor::WhitespaceConfig, + config: &helix_view::editor::Config, ) { + let whitespace = &config.whitespace; use helix_view::editor::WhitespaceRenderValue; // It's slightly more efficient to produce a full RopeSlice from the Rope, then slice that a bunch @@ -395,22 +396,25 @@ impl EditorView { } else { " ".to_string() }; + let indent_guide_char = config.indent_guides.character.to_string(); let text_style = theme.get("ui.text"); let whitespace_style = theme.get("ui.virtual.whitespace"); let mut is_in_indent_area = true; let mut last_line_indent_level = 0; - let indent_style = theme - .try_get("ui.virtual.indent-guide") - .unwrap_or_else(|| theme.get("comment")); + let indent_style = theme.get("ui.virtual.indent-guide"); let draw_indent_guides = |indent_level, line, surface: &mut Surface| { + if !config.indent_guides.render { + return; + } + for i in 0..(indent_level / tab_width as u16) { surface.set_string( viewport.x + (i * tab_width as u16) - offset.col as u16, viewport.y + line, - "│", + &indent_guide_char, indent_style, ); } @@ -489,7 +493,7 @@ impl EditorView { let grapheme = Cow::from(grapheme); let is_whitespace; - let (grapheme, width) = if grapheme == "\t" { + let (display_grapheme, width) = if grapheme == "\t" { is_whitespace = true; // make sure we display tab as appropriate amount of spaces let visual_tab_width = tab_width - (visual_x as usize % tab_width); @@ -516,7 +520,7 @@ impl EditorView { surface.set_string( viewport.x + visual_x - offset.col as u16, viewport.y + line, - grapheme, + display_grapheme, if is_whitespace { style.patch(whitespace_style) } else { diff --git a/helix-term/src/ui/picker.rs b/helix-term/src/ui/picker.rs index 9ffe45c1b648..95ec840580a3 100644 --- a/helix-term/src/ui/picker.rs +++ b/helix-term/src/ui/picker.rs @@ -240,7 +240,7 @@ impl Component for FilePicker { surface, &cx.editor.theme, highlights, - &cx.editor.config().whitespace, + &cx.editor.config(), ); // highlight the line diff --git a/helix-view/src/editor.rs b/helix-view/src/editor.rs index c53fcc7f71c0..75c56ec66df2 100644 --- a/helix-view/src/editor.rs +++ b/helix-view/src/editor.rs @@ -152,6 +152,8 @@ pub struct Config { pub rulers: Vec, #[serde(default)] pub whitespace: WhitespaceConfig, + /// Vertical indent width guides. + pub indent_guides: IndentGuidesConfig, } #[derive(Debug, Default, Clone, PartialEq, Serialize, Deserialize)] @@ -360,6 +362,22 @@ impl Default for WhitespaceCharacters { } } +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +#[serde(default)] +pub struct IndentGuidesConfig { + pub render: bool, + pub character: char, +} + +impl Default for IndentGuidesConfig { + fn default() -> Self { + Self { + render: false, + character: '│', + } + } +} + impl Default for Config { fn default() -> Self { Self { @@ -387,6 +405,7 @@ impl Default for Config { lsp: LspConfig::default(), rulers: Vec::new(), whitespace: WhitespaceConfig::default(), + indent_guides: IndentGuidesConfig::default(), } } }