Skip to content

Commit

Permalink
Add rainbow indentation guides
Browse files Browse the repository at this point in the history
  • Loading branch information
SoraTenshi authored and omentic committed May 1, 2024
1 parent 9a3f23b commit 27cc594
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 10 deletions.
2 changes: 2 additions & 0 deletions book/src/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ Options for rendering vertical indent guides.
| `render` | Whether to render indent guides | `false` |
| `character` | Literal character to use for rendering the indent guide | `` |
| `skip-levels` | Number of indent levels to skip | `0` |
| `rainbow-option` | Enum to set rainbow indentations. Options: `normal`, `dim` and `none`. | `none` |

Example:

Expand All @@ -299,6 +300,7 @@ Example:
render = true
character = "" # Some characters that work well: "▏", "┆", "┊", "⸽"
skip-levels = 1
rainbow-option = "normal"
```

### `[editor.gutters]` Section
Expand Down
42 changes: 32 additions & 10 deletions helix-term/src/ui/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ use helix_core::syntax::Highlight;
use helix_core::syntax::HighlightEvent;
use helix_core::text_annotations::TextAnnotations;
use helix_core::{visual_offset_from_block, Position, RopeSlice};
use helix_view::editor::{WhitespaceConfig, WhitespaceRenderValue};
use helix_view::editor::{RainbowIndentOptions, WhitespaceConfig, WhitespaceRenderValue};
use helix_view::graphics::Rect;
use helix_view::theme::Style;
use helix_view::theme::{Modifier, Style};
use helix_view::view::ViewPosition;
use helix_view::Document;
use helix_view::Theme;
Expand Down Expand Up @@ -339,6 +339,8 @@ pub struct TextRenderer<'a> {
pub whitespace_style: Style,
pub indent_guide_char: String,
pub indent_guide_style: Style,
pub indent_guide_rainbow: RainbowIndentOptions,
pub theme: &'a Theme,
pub newline: String,
pub nbsp: String,
pub nnbsp: String,
Expand All @@ -361,7 +363,7 @@ impl<'a> TextRenderer<'a> {
pub fn new(
surface: &'a mut Surface,
doc: &Document,
theme: &Theme,
theme: &'a Theme,
col_offset: usize,
viewport: Rect,
) -> TextRenderer<'a> {
Expand Down Expand Up @@ -403,12 +405,19 @@ impl<'a> TextRenderer<'a> {
};

let text_style = theme.get("ui.text");
let basic_style = text_style.patch(
theme
.try_get("ui.virtual.indent-guide")
.unwrap_or_else(|| theme.get("ui.virtual.whitespace")),
);

let indent_width = doc.indent_style.indent_width(tab_width) as u16;

TextRenderer {
surface,
indent_guide_char: editor_config.indent_guides.character.into(),
indent_guide_rainbow: editor_config.indent_guides.rainbow_option.clone(),
theme,
newline,
nbsp,
nnbsp,
Expand All @@ -420,11 +429,7 @@ impl<'a> TextRenderer<'a> {
starting_indent: col_offset / indent_width as usize
+ (col_offset % indent_width as usize != 0) as usize
+ editor_config.indent_guides.skip_levels as usize,
indent_guide_style: text_style.patch(
theme
.try_get("ui.virtual.indent-guide")
.unwrap_or_else(|| theme.get("ui.virtual.whitespace")),
),
indent_guide_style: basic_style,
text_style,
draw_indent_guides: editor_config.indent_guides.render,
viewport,
Expand Down Expand Up @@ -522,8 +527,25 @@ impl<'a> TextRenderer<'a> {
as u16;
let y = self.viewport.y + row;
debug_assert!(self.surface.in_bounds(x, y));
self.surface
.set_string(x, y, &self.indent_guide_char, self.indent_guide_style);
match self.indent_guide_rainbow {
RainbowIndentOptions::None => {
self.surface
.set_string(x, y, &self.indent_guide_char, self.indent_guide_style)
}
RainbowIndentOptions::Dim => {
let new_style = self
.indent_guide_style
.patch(self.theme.get_rainbow(i))
.add_modifier(Modifier::DIM);
self.surface
.set_string(x, y, &self.indent_guide_char, new_style);
}
RainbowIndentOptions::Normal => {
let new_style = self.indent_guide_style.patch(self.theme.get_rainbow(i));
self.surface
.set_string(x, y, &self.indent_guide_char, new_style);
}
};
}
}
}
10 changes: 10 additions & 0 deletions helix-view/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -817,12 +817,21 @@ impl Default for WhitespaceCharacters {
}
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum RainbowIndentOptions {
None,
Dim,
Normal,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default, rename_all = "kebab-case")]
pub struct IndentGuidesConfig {
pub render: bool,
pub character: char,
pub skip_levels: u8,
pub rainbow_option: RainbowIndentOptions,
}

impl Default for IndentGuidesConfig {
Expand All @@ -831,6 +840,7 @@ impl Default for IndentGuidesConfig {
skip_levels: 0,
render: false,
character: '│',
rainbow_option: RainbowIndentOptions::None,
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions helix-view/src/theme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,10 @@ impl Theme {
pub fn rainbow_length(&self) -> usize {
self.rainbow_length
}

pub fn get_rainbow(&self, index: usize) -> Style {
self.highlights[index % self.rainbow_length]
}
}

fn default_rainbow() -> Vec<Style> {
Expand Down

0 comments on commit 27cc594

Please sign in to comment.