Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add indent guides #1796

Merged
merged 3 commits into from
Jun 21, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Make indent guides configurable
  • Loading branch information
sudormrfbin committed May 31, 2022
commit 73ffb0c4326a49ad79a0372c6d24c7679e977f62
17 changes: 17 additions & 0 deletions book/src/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "╎"
```
20 changes: 12 additions & 8 deletions helix-term/src/ui/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand All @@ -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,
);
}
Expand Down Expand Up @@ -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);
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion helix-term/src/ui/picker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ impl<T: 'static> Component for FilePicker<T> {
surface,
&cx.editor.theme,
highlights,
&cx.editor.config().whitespace,
&cx.editor.config(),
);

// highlight the line
Expand Down
19 changes: 19 additions & 0 deletions helix-view/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ pub struct Config {
pub rulers: Vec<u16>,
#[serde(default)]
pub whitespace: WhitespaceConfig,
/// Vertical indent width guides.
pub indent_guides: IndentGuidesConfig,
}

#[derive(Debug, Default, Clone, PartialEq, Serialize, Deserialize)]
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -387,6 +405,7 @@ impl Default for Config {
lsp: LspConfig::default(),
rulers: Vec::new(),
whitespace: WhitespaceConfig::default(),
indent_guides: IndentGuidesConfig::default(),
}
}
}
Expand Down