From d028319080d1360eea04fb59f21c68736e6de6cf Mon Sep 17 00:00:00 2001 From: dgkf <18220321+dgkf@users.noreply.github.com> Date: Wed, 17 Aug 2022 19:36:21 -0700 Subject: [PATCH 01/13] dynamically resize line number gutter width --- helix-term/src/ui/editor.rs | 13 ++++---- helix-view/src/gutter.rs | 66 ++++++++++++++++++++++++++++++++----- helix-view/src/view.rs | 50 +++++++++------------------- 3 files changed, 81 insertions(+), 48 deletions(-) diff --git a/helix-term/src/ui/editor.rs b/helix-term/src/ui/editor.rs index 6b3163748fa2..2b68f1edb6e4 100644 --- a/helix-term/src/ui/editor.rs +++ b/helix-term/src/ui/editor.rs @@ -638,22 +638,23 @@ impl EditorView { // avoid lots of small allocations by reusing a text buffer for each line let mut text = String::with_capacity(8); - for (constructor, width) in view.gutters() { - let gutter = constructor(editor, doc, view, theme, is_focused, *width); - text.reserve(*width); // ensure there's enough space for the gutter + for gutter_type in view.gutters() { + let gutter = gutter_type.row_styler(editor, doc, view, theme, is_focused); + let width = gutter_type.width(view); + text.reserve(width); // ensure there's enough space for the gutter for (i, line) in (view.offset.row..(last_line + 1)).enumerate() { let selected = cursors.contains(&line); let x = viewport.x + offset; let y = viewport.y + i as u16; if let Some(style) = gutter(line, selected, &mut text) { - surface.set_stringn(x, y, &text, *width, gutter_style.patch(style)); + surface.set_stringn(x, y, &text, width, gutter_style.patch(style)); } else { surface.set_style( Rect { x, y, - width: *width as u16, + width: width as u16, height: 1, }, gutter_style, @@ -662,7 +663,7 @@ impl EditorView { text.clear(); } - offset += *width as u16; + offset += width as u16; } } diff --git a/helix-view/src/gutter.rs b/helix-view/src/gutter.rs index 8f7c306266a9..9a3a09eb1160 100644 --- a/helix-view/src/gutter.rs +++ b/helix-view/src/gutter.rs @@ -1,21 +1,53 @@ use std::fmt::Write; +use std::iter::successors; use crate::{ + editor::GutterType, graphics::{Color, Modifier, Style}, Document, Editor, Theme, View, }; +fn count_digits(n: usize) -> usize { + successors(Some(n), |&n| (n >= 10).then(|| n / 10)).count() +} + pub type GutterFn<'doc> = Box Option