From 7d5a5d09841ead6e2fdf8436fcd445d4bc4e52f7 Mon Sep 17 00:00:00 2001
From: Max Mynter <32773644+maxmynter@users.noreply.github.com>
Date: Wed, 11 Jun 2025 12:00:50 +0200
Subject: [PATCH] Make minimum width for line numbers in gutter configurable
(#31959)
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Closes #7334
# Changes
This PR makes the minimum width allocated for line numbers in the side
gutter configurable in units of character width via the
`"line_number_base_width"` attribute in `gutter` settings. Set the
previously hard coded value of `4` as default.
Together with other settings (`"folds"`, `"breakpoints"`,...) this gives
the user control over the gutter width.
If the number of lines exceedes the base width, the number of digits in
the largest line number is chosen instead. This is consistent with
previous behaviour.
Screenshot for reference:
P.S.: This is my first time contributing to zed (yay!🎉). Let me know if
i'm missing something.
Release Notes:
- Make minimum line number width in gutter configurable
---
assets/settings/default.json | 4 +++-
crates/editor/src/editor.rs | 4 ++--
crates/editor/src/editor_settings.rs | 5 +++++
crates/editor/src/element.rs | 11 ++++++-----
4 files changed, 16 insertions(+), 8 deletions(-)
diff --git a/assets/settings/default.json b/assets/settings/default.json
index 939f79e28127f715113a3ae9e0f0b3fa51514087..98f3585f47350bf5db7cb2311b223b70c6601ef7 100644
--- a/assets/settings/default.json
+++ b/assets/settings/default.json
@@ -445,7 +445,9 @@
// Whether to show breakpoints in the gutter.
"breakpoints": true,
// Whether to show fold buttons in the gutter.
- "folds": true
+ "folds": true,
+ // Minimum number of characters to reserve space for in the gutter.
+ "min_line_number_digits": 4
},
"indent_guides": {
// Whether to show indent guides in the editor.
diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs
index 412333f15f116fd7e4dfb98ffedb4210e8cbd6e0..7215a9e0a2575f51439aee25bd72bcbabf8e3399 100644
--- a/crates/editor/src/editor.rs
+++ b/crates/editor/src/editor.rs
@@ -240,7 +240,6 @@ pub(crate) const SCROLL_CENTER_TOP_BOTTOM_DEBOUNCE_TIMEOUT: Duration = Duration:
pub(crate) const EDIT_PREDICTION_KEY_CONTEXT: &str = "edit_prediction";
pub(crate) const EDIT_PREDICTION_CONFLICT_KEY_CONTEXT: &str = "edit_prediction_conflict";
-pub(crate) const MIN_LINE_NUMBER_DIGITS: u32 = 4;
pub(crate) const MINIMAP_FONT_SIZE: AbsoluteLength = AbsoluteLength::Pixels(px(2.));
pub type RenderDiffHunkControlsFn = Arc<
@@ -21558,7 +21557,8 @@ impl EditorSnapshot {
.unwrap_or(gutter_settings.line_numbers);
let line_gutter_width = if show_line_numbers {
// Avoid flicker-like gutter resizes when the line number gains another digit and only resize the gutter on files with N*10^5 lines.
- let min_width_for_number_on_gutter = em_advance * MIN_LINE_NUMBER_DIGITS as f32;
+ let min_width_for_number_on_gutter =
+ em_advance * gutter_settings.min_line_number_digits as f32;
max_line_number_width.max(min_width_for_number_on_gutter)
} else {
0.0.into()
diff --git a/crates/editor/src/editor_settings.rs b/crates/editor/src/editor_settings.rs
index 803587a923c5cc6322bf906d45a62258641c2121..44e220f90d83f65acdf08d2dc22cc1980a522705 100644
--- a/crates/editor/src/editor_settings.rs
+++ b/crates/editor/src/editor_settings.rs
@@ -150,6 +150,7 @@ impl Minimap {
#[derive(Copy, Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq)]
pub struct Gutter {
+ pub min_line_number_digits: usize,
pub line_numbers: bool,
pub runnables: bool,
pub breakpoints: bool,
@@ -609,6 +610,10 @@ pub struct GutterContent {
///
/// Default: true
pub line_numbers: Option,
+ /// Minimum number of characters to reserve space for in the gutter.
+ ///
+ /// Default: 4
+ pub min_line_number_digits: Option,
/// Whether to show runnable buttons in the gutter.
///
/// Default: true
diff --git a/crates/editor/src/element.rs b/crates/editor/src/element.rs
index 36bad30005042cd3cee576ab187429b26c74e90e..1f820162d444f7e3b71ec0d21b0c940e830605c9 100644
--- a/crates/editor/src/element.rs
+++ b/crates/editor/src/element.rs
@@ -6,10 +6,10 @@ use crate::{
Editor, EditorMode, EditorSettings, EditorSnapshot, EditorStyle, FILE_HEADER_HEIGHT,
FocusedBlock, GutterDimensions, HalfPageDown, HalfPageUp, HandleInput, HoveredCursor,
InlayHintRefreshReason, InlineCompletion, JumpData, LineDown, LineHighlight, LineUp,
- MAX_LINE_LEN, MIN_LINE_NUMBER_DIGITS, MINIMAP_FONT_SIZE, MULTI_BUFFER_EXCERPT_HEADER_HEIGHT,
- OpenExcerpts, PageDown, PageUp, PhantomBreakpointIndicator, Point, RowExt, RowRangeExt,
- SelectPhase, SelectedTextHighlight, Selection, SelectionDragState, SoftWrap,
- StickyHeaderExcerpt, ToPoint, ToggleFold,
+ MAX_LINE_LEN, MINIMAP_FONT_SIZE, MULTI_BUFFER_EXCERPT_HEADER_HEIGHT, OpenExcerpts, PageDown,
+ PageUp, PhantomBreakpointIndicator, Point, RowExt, RowRangeExt, SelectPhase,
+ SelectedTextHighlight, Selection, SelectionDragState, SoftWrap, StickyHeaderExcerpt, ToPoint,
+ ToggleFold,
code_context_menus::{CodeActionsMenu, MENU_ASIDE_MAX_WIDTH, MENU_ASIDE_MIN_WIDTH, MENU_GAP},
display_map::{
Block, BlockContext, BlockStyle, DisplaySnapshot, EditorMargins, FoldId, HighlightedChunk,
@@ -2826,7 +2826,8 @@ impl EditorElement {
let available_width = gutter_dimensions.left_padding - git_gutter_width;
let editor = self.editor.clone();
- let is_wide = max_line_number_length >= MIN_LINE_NUMBER_DIGITS
+ let is_wide = max_line_number_length
+ >= EditorSettings::get_global(cx).gutter.min_line_number_digits as u32
&& row_info
.buffer_row
.is_some_and(|row| (row + 1).ilog10() + 1 == max_line_number_length)