on master help sign in
Add rulers option (#2060)

* Add color_column option * Rename to ruler Co-authored-by: DeviousStoat <devious@stoat.com>

commit5d5b6bab9ba5211c8c146fb38276f968892e7882 authorThomas <74479846+DeviousStoat@users.noreply.github.com> dateWed 20 Apr 2022, 03:44 (5 years ago) parent0242607
4 files changed+33
book/src/configuration.md+1 helix-core/src/syntax.rs+2 helix-term/src/ui/editor.rs+27 helix-view/src/editor.rs+3
book/src/configuration.md+1
@@ -43,6 +43,7 @@ hidden = false
4343| `completion-trigger-len` | The min-length of word under cursor to trigger autocompletion | `2` |
4444| `auto-info` | Whether to display infoboxes | `true` |
4545| `true-color` | Set to `true` to override automatic detection of terminal truecolor support in the event of a false negative. | `false` |
46+| `rulers` | List of column positions at which to display the rulers. Can be overidden by language specific `rulers` in `languages.toml` file. | `[]` |
4647
4748### `[editor.lsp]` Section
4849
helix-core/src/syntax.rs+2
@@ -104,6 +104,8 @@ pub struct LanguageConfiguration {
104104 /// global setting.
105105 #[serde(default, skip_serializing, deserialize_with = "deserialize_auto_pairs")]
106106 pub auto_pairs: Option<AutoPairs>,
107+
108+ pub rulers: Option<Vec<u16>>, // if set, override editor's rulers
107109}
108110
109111#[derive(Debug, Serialize, Deserialize)]
helix-term/src/ui/editor.rs+27
@@ -126,6 +126,7 @@ impl EditorView {
126126
127127 Self::render_text_highlights(doc, view.offset, inner, surface, theme, highlights);
128128 Self::render_gutter(editor, doc, view, view.area, surface, theme, is_focused);
129+ Self::render_rulers(editor, doc, view, inner, surface, theme);
129130
130131 if is_focused {
131132 Self::render_focused_view_elements(view, doc, inner, theme, surface);
@@ -152,6 +153,32 @@ impl EditorView {
152153 self.render_statusline(doc, view, statusline_area, surface, theme, is_focused);
153154 }
154155
156+ pub fn render_rulers(
157+ editor: &Editor,
158+ doc: &Document,
159+ view: &View,
160+ viewport: Rect,
161+ surface: &mut Surface,
162+ theme: &Theme,
163+ ) {
164+ let editor_rulers = &editor.config().rulers;
165+ let ruler_theme = theme.get("ui.virtual.ruler");
166+
167+ let rulers = doc
168+ .language_config()
169+ .and_then(|config| config.rulers.as_ref())
170+ .unwrap_or(editor_rulers);
171+
172+ rulers
173+ .iter()
174+ // View might be horizontally scrolled, convert from absolute distance
175+ // from the 1st column to relative distance from left of viewport
176+ .filter_map(|ruler| ruler.checked_sub(1 + view.offset.col as u16))
177+ .filter(|ruler| ruler < &viewport.width)
178+ .map(|ruler| viewport.clip_left(ruler).with_width(1))
179+ .for_each(|area| surface.set_style(area, ruler_theme))
180+ }
181+
155182 /// Get syntax highlights for a document in a view represented by the first line
156183 /// and column (`offset`) and the last line. This is done instead of using a view
157184 /// directly to enable rendering syntax highlighted docs anywhere (eg. picker preview)
helix-view/src/editor.rs+3
@@ -146,6 +146,8 @@ pub struct Config {
146146 #[serde(default)]
147147 pub search: SearchConfig,
148148 pub lsp: LspConfig,
149+ /// Column numbers at which to draw the rulers. Default to `[]`, meaning no rulers.
150+ pub rulers: Vec<u16>,
149151}
150152
151153#[derive(Debug, Default, Clone, PartialEq, Serialize, Deserialize)]
@@ -285,6 +287,7 @@ impl Default for Config {
285287 true_color: false,
286288 search: SearchConfig::default(),
287289 lsp: LspConfig::default(),
290+ rulers: Vec::new(),
288291 }
289292 }
290293}