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 configuration for min width of line-numbers gutter #4724

Merged
merged 22 commits into from
Jan 21, 2023
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
22 changes: 20 additions & 2 deletions helix-view/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ pub struct Config {
pub cursorcolumn: bool,
/// Gutters. Default ["diagnostics", "line-numbers"]
pub gutters: Vec<GutterType>,
/// Minimum number of characters to display as part of line number gutter. Defaults to 2.
pub line_numbers_width_min: usize,
/// Maximum number of characters to display as part of line number gutter, or 0 for no maximum. Defaults to 0 (no maximum).
pub line_numbers_width_max: usize,
dgkf marked this conversation as resolved.
Show resolved Hide resolved
/// Middle click paste support. Defaults to true.
pub middle_click_paste: bool,
/// Automatic insertion of pairs to parentheses, brackets,
Expand Down Expand Up @@ -595,6 +599,8 @@ impl Default for Config {
GutterType::Spacer,
GutterType::LineNumbers,
],
line_numbers_width_min: 2,
line_numbers_width_max: 0, // unlimited
dgkf marked this conversation as resolved.
Show resolved Hide resolved
dgkf marked this conversation as resolved.
Show resolved Hide resolved
middle_click_paste: true,
auto_pairs: AutoPairConfig::default(),
auto_completion: true,
Expand Down Expand Up @@ -1039,7 +1045,14 @@ impl Editor {
.try_get(self.tree.focus)
.filter(|v| id == v.doc) // Different Document
.cloned()
.unwrap_or_else(|| View::new(id, self.config().gutters.clone()));
.unwrap_or_else(|| {
View::new(
id,
self.config().gutters.clone(),
self.config().line_numbers_width_min,
self.config().line_numbers_width_max,
)
});
let view_id = self.tree.split(
view,
match action {
Expand Down Expand Up @@ -1182,7 +1195,12 @@ impl Editor {
.map(|(&doc_id, _)| doc_id)
.next()
.unwrap_or_else(|| self.new_document(Document::default()));
let view = View::new(doc_id, self.config().gutters.clone());
let view = View::new(
doc_id,
self.config().gutters.clone(),
self.config().line_numbers_width_min,
self.config().line_numbers_width_max,
);
let view_id = self.tree.insert(view);
let doc = doc_mut!(self, &doc_id);
doc.ensure_view_init(view_id);
Expand Down
59 changes: 51 additions & 8 deletions helix-view/src/gutter.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::cmp::{max, min};
use std::fmt::Write;

use crate::{
Expand Down Expand Up @@ -90,13 +91,22 @@ pub fn line_numbers<'doc>(
theme: &Theme,
is_focused: bool,
) -> GutterFn<'doc> {
const ELLIPSIS: &str = "\u{2026}";

let text = doc.text().slice(..);
let last_line = view.last_line(doc);
let width = GutterType::LineNumbers.width(view, doc);

let last_line = text.len_lines().saturating_sub(1);
let last_line_in_view = view.last_line(doc);

// Whether to draw the line number for the last line of the
// document or not. We only draw it if it's not an empty line.
let draw_last = text.line_to_byte(last_line) < text.len_bytes();
let draw_last = text.line_to_byte(last_line_in_view) < text.len_bytes();
let last_drawn_line = if draw_last { last_line + 1 } else { last_line };

// characters used to display last line, and settings for min/max
let n_last_line = count_digits(last_drawn_line);
let n_min = editor.config().line_numbers_width_min;
let n_max = editor.config().line_numbers_width_max;

let linenr = theme.get("ui.linenr");
let linenr_select = theme.get("ui.linenr.selected");
Expand All @@ -109,7 +119,8 @@ pub fn line_numbers<'doc>(
let mode = editor.mode;

Box::new(move |line: usize, selected: bool, out: &mut String| {
if line == last_line && !draw_last {
if line == last_line_in_view && !draw_last {
let width = clamp_line_numbers_width(n_last_line, n_min, n_max);
write!(out, "{:>1$}", '~', width).unwrap();
Some(linenr)
} else {
Expand All @@ -132,20 +143,52 @@ pub fn line_numbers<'doc>(
linenr
};

write!(out, "{:>1$}", display_num, width).unwrap();
let n_display = count_digits(display_num);
if n_max > 0 && n_display > n_max {
let display_num_trunc = display_num % (10 as usize).pow(n_max as u32);
write!(out, "{}{1:0>2$}", ELLIPSIS, display_num_trunc, n_max - 1).unwrap();
} else {
let width = clamp_line_numbers_width(n_last_line, n_min, n_max);
write!(out, "{:>1$}", display_num, width).unwrap();
}

Some(style)
}
})
}

pub fn line_numbers_width(_view: &View, doc: &Document) -> usize {
/// Clamp a line number width between a lower and upper bound
///
/// However, an upper bound of `0` is treated as having no maximum
#[inline(always)]
fn clamp_line_numbers_width(x: usize, lb: usize, ub: usize) -> usize {
if ub == 0 {
min(max(x, lb), ub)
} else {
max(x, lb)
}
}

/// The width of a "line-numbers" gutter
///
/// The width of the gutter depends on the number of lines in the document,
/// whether there is content on the last line (the `~` line), and the `line-
/// numbers-width-min` & `line-numbers-width-max` settings.
dgkf marked this conversation as resolved.
Show resolved Hide resolved
pub fn line_numbers_width(view: &View, doc: &Document) -> usize {
the-mikedavis marked this conversation as resolved.
Show resolved Hide resolved
let text = doc.text();
let last_line = text.len_lines().saturating_sub(1);
let draw_last = text.line_to_byte(last_line) < text.len_bytes();
let last_drawn = if draw_last { last_line + 1 } else { last_line };
let digits = count_digits(last_drawn);

let n_min = view.gutter_line_numbers_width_min;
let n_max = view.gutter_line_numbers_width_max;

// set a lower bound to 2-chars to minimize ambiguous relative line numbers
std::cmp::max(count_digits(last_drawn), 2)
if n_max > 0 {
min(max(digits, n_min), n_max)
} else {
max(digits, n_min)
}
}

pub fn padding<'doc>(
Expand Down
16 changes: 14 additions & 2 deletions helix-view/src/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ pub struct View {
pub object_selections: Vec<Selection>,
/// GutterTypes used to fetch Gutter (constructor) and width for rendering
gutters: Vec<GutterType>,
/// Relevant gutter config settings
pub gutter_line_numbers_width_min: usize,
pub gutter_line_numbers_width_max: usize,
}

impl fmt::Debug for View {
Expand All @@ -109,7 +112,12 @@ impl fmt::Debug for View {
}

impl View {
pub fn new(doc: DocumentId, gutter_types: Vec<crate::editor::GutterType>) -> Self {
pub fn new(
doc: DocumentId,
gutter_types: Vec<crate::editor::GutterType>,
gutter_line_numbers_width_min: usize,
gutter_line_numbers_width_max: usize,
dgkf marked this conversation as resolved.
Show resolved Hide resolved
) -> Self {
Self {
id: ViewId::default(),
doc,
Expand All @@ -120,6 +128,8 @@ impl View {
last_modified_docs: [None, None],
object_selections: Vec::new(),
gutters: gutter_types,
gutter_line_numbers_width_min,
gutter_line_numbers_width_max,
}
}

Expand Down Expand Up @@ -364,6 +374,8 @@ mod tests {
let mut view = View::new(
DocumentId::default(),
vec![GutterType::Diagnostics, GutterType::LineNumbers],
2,
5,
);
view.area = Rect::new(40, 40, 40, 40);
let rope = Rope::from_str("abc\n\tdef");
Expand Down Expand Up @@ -473,7 +485,7 @@ mod tests {
fn test_text_pos_at_screen_coords_graphemes() {
let mut view = View::new(
DocumentId::default(),
vec![GutterType::Diagnostics, GutterType::LineNumbers],
vec![GutterType::Diagnostics, GutterType::LineNumbers, 2, 4],
);
view.area = Rect::new(40, 40, 40, 40);
let rope = Rope::from_str("Hèl̀l̀ò world!");
Expand Down