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

feat: Use custom character to draw editor rulers #11798

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions book/src/editor.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
| `true-color` | Set to `true` to override automatic detection of terminal truecolor support in the event of a false negative | `false` |
| `undercurl` | Set to `true` to override automatic detection of terminal undercurl support in the event of a false negative | `false` |
| `rulers` | List of column positions at which to display the rulers. Can be overridden by language specific `rulers` in `languages.toml` file | `[]` |
| `ruler-style` | `bg` displays the ruler as a background color, while `char` displays the configured `ruler-char` | `bg` |
| `ruler-char` | Specifies the character used to display the rulers when `ruler-style` is `char` | `│` |
| `bufferline` | Renders a line at the top of the editor displaying open buffers. Can be `always`, `never` or `multiple` (only shown if more than one buffer is in use) | `never` |
| `color-modes` | Whether to color the mode indicator with different colors depending on the mode itself | `false` |
| `text-width` | Maximum line length. Used for the `:reflow` command and soft-wrapping if `soft-wrap.wrap-at-text-width` is set | `80` |
Expand Down
1 change: 1 addition & 0 deletions book/src/themes.md
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@ These scopes are used for theming the editor interface:
| `ui.text.inactive` | Same as `ui.text` but when the text is inactive (e.g. suggestions) |
| `ui.text.info` | The key: command text in `ui.popup.info` boxes |
| `ui.virtual.ruler` | Ruler columns (see the [`editor.rulers` config][editor-section]) |
| `ui.virtual.ruler.char` | Ruler columns, ([only if `editor.ruler-char` is set][editor-section] |
| `ui.virtual.whitespace` | Visible whitespace characters |
| `ui.virtual.indent-guide` | Vertical indent width guides |
| `ui.virtual.inlay-hint` | Default style for inlay hints of all kinds |
Expand Down
29 changes: 25 additions & 4 deletions helix-term/src/ui/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use helix_core::{
use helix_view::{
annotations::diagnostics::DiagnosticFilter,
document::{Mode, SavePoint, SCRATCH_BUFFER_NAME},
editor::{CompleteAction, CursorShapeConfig},
editor::{CompleteAction, CursorShapeConfig, RulerStyle},
graphics::{Color, CursorKind, Modifier, Rect, Style},
input::{KeyEvent, MouseButton, MouseEvent, MouseEventKind},
keyboard::{KeyCode, KeyModifiers},
Expand Down Expand Up @@ -201,6 +201,9 @@ impl EditorView {
inline_diagnostic_config,
config.end_of_line_diagnostics,
));

Self::render_rulers(editor, doc, view, inner, surface, theme);

render_document(
surface,
inner,
Expand All @@ -212,7 +215,6 @@ impl EditorView {
theme,
decorations,
);
Self::render_rulers(editor, doc, view, inner, surface, theme);

// if we're not at the edge of the screen, draw a right border
if viewport.right() != view.area.right() {
Expand Down Expand Up @@ -252,8 +254,16 @@ impl EditorView {
theme: &Theme,
) {
let editor_rulers = &editor.config().rulers;
let ruler_style = &editor.config().ruler_style;
let ruler_char = editor.config().ruler_char.to_string();

let theme_key = match ruler_style {
RulerStyle::Bg => "ui.virtual.ruler",
RulerStyle::Char => "ui.virtual.ruler.char",
};

let ruler_theme = theme
.try_get("ui.virtual.ruler")
.try_get(theme_key)
.unwrap_or_else(|| Style::default().bg(Color::Red));

let rulers = doc
Expand All @@ -270,7 +280,18 @@ impl EditorView {
.filter_map(|ruler| ruler.checked_sub(1 + view_offset.horizontal_offset as u16))
.filter(|ruler| ruler < &viewport.width)
.map(|ruler| viewport.clip_left(ruler).with_width(1))
.for_each(|area| surface.set_style(area, ruler_theme))
.for_each(|area| {
match ruler_style {
RulerStyle::Bg => {
surface.set_style(area, ruler_theme);
}
RulerStyle::Char => {
for y in area.y..area.height {
surface.set_string(area.x, y, &ruler_char, ruler_theme);
}
}
}
})
}

fn viewport_byte_range(
Expand Down
17 changes: 17 additions & 0 deletions helix-view/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,10 @@ pub struct Config {
pub terminal: Option<TerminalConfig>,
/// Column numbers at which to draw the rulers. Defaults to `[]`, meaning no rulers.
pub rulers: Vec<u16>,
/// Set to `bg` to display rulers with background color (default) or `char` to use character from `ruler-char`.
pub ruler_style: RulerStyle,
/// Character used to draw the rulers when specified
pub ruler_char: char,
#[serde(default)]
pub whitespace: WhitespaceConfig,
/// Persistently display open buffers along the top
Expand Down Expand Up @@ -636,6 +640,17 @@ impl Default for CursorShapeConfig {
}
}

/// ruler render style
#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum RulerStyle {
/// Render rulers with background color
#[default]
Bg,
/// Render rulers with character from `ruler-char`
Char,
}

/// bufferline render modes
#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
Expand Down Expand Up @@ -963,6 +978,8 @@ impl Default for Config {
lsp: LspConfig::default(),
terminal: get_terminal_provider(),
rulers: Vec::new(),
ruler_style: RulerStyle::default(),
ruler_char: '│',
whitespace: WhitespaceConfig::default(),
bufferline: BufferLine::default(),
indent_guides: IndentGuidesConfig::default(),
Expand Down