Skip to content

Commit 3cb87cd

Browse files
galeneliasnoaccOS
authored andcommitted
Fix minimum gutter line number spacing (zed-industries#18021)
I was inspecting how Zed did the layout in the editor, specifically for the gutter, and noticed that `em_width * X` is being used as the 'width of X consecutive characters'. Howevever, that math didn't work for me, because em_width doesn't account for the space between characters, so you can't just multiply it by a character count. One place this is actually noticeable is in the logic for `min_width_for_number_on_gutter`, where we try to reserve 4 characters of line number space. However, once you actually hit 4 characters, the actual width is bigger, causing things to resize. This seems clearly counter to the intent of the code. It seems the more correct logic is to use `em_advance` which accounts for the space between the characters. I am leaving the rest of the uses of `em_width` for generic padding. It is also possible that `column_pixels()` would be the more correct fix here, but it wasn't straightforward to use that due to it residing EditorElement source file. On my MacBook this increases the width of the gutter by 6 pixels when there are <999 lines in the file, otherwise it's identical. It might be worth doing some more general audit of some of the other uses of em_width as a concept. (e.g. `git_blame_entries_width`) https://github.com/user-attachments/assets/f2a28cd5-9bb6-4109-bf41-1838e56a75f9 Release Notes: - Fix a slight gutter flicker when going over 999 lines
1 parent 0ff7180 commit 3cb87cd

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

crates/editor/src/editor.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12956,6 +12956,7 @@ impl EditorSnapshot {
1295612956
font_id: FontId,
1295712957
font_size: Pixels,
1295812958
em_width: Pixels,
12959+
em_advance: Pixels,
1295912960
max_line_number_width: Pixels,
1296012961
cx: &AppContext,
1296112962
) -> GutterDimensions {
@@ -12976,7 +12977,7 @@ impl EditorSnapshot {
1297612977
.unwrap_or(gutter_settings.line_numbers);
1297712978
let line_gutter_width = if show_line_numbers {
1297812979
// Avoid flicker-like gutter resizes when the line number gains another digit and only resize the gutter on files with N*10^5 lines.
12979-
let min_width_for_number_on_gutter = em_width * 4.0;
12980+
let min_width_for_number_on_gutter = em_advance * 4.0;
1298012981
max_line_number_width.max(min_width_for_number_on_gutter)
1298112982
} else {
1298212983
0.0.into()

crates/editor/src/element.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4970,6 +4970,7 @@ impl Element for EditorElement {
49704970
font_id,
49714971
font_size,
49724972
em_width,
4973+
em_advance,
49734974
self.max_line_number_width(&snapshot, cx),
49744975
cx,
49754976
);
@@ -6283,10 +6284,21 @@ fn compute_auto_height_layout(
62836284
.unwrap()
62846285
.size
62856286
.width;
6287+
let em_advance = cx
6288+
.text_system()
6289+
.advance(font_id, font_size, 'm')
6290+
.unwrap()
6291+
.width;
62866292

62876293
let mut snapshot = editor.snapshot(cx);
6288-
let gutter_dimensions =
6289-
snapshot.gutter_dimensions(font_id, font_size, em_width, max_line_number_width, cx);
6294+
let gutter_dimensions = snapshot.gutter_dimensions(
6295+
font_id,
6296+
font_size,
6297+
em_width,
6298+
em_advance,
6299+
max_line_number_width,
6300+
cx,
6301+
);
62906302

62916303
editor.gutter_dimensions = gutter_dimensions;
62926304
let text_width = width - gutter_dimensions.width;

0 commit comments

Comments
 (0)