Skip to content

Commit 8d09dd6

Browse files
mrnuggetbennetbo
authored andcommitted
git blame gutter: Use smallest possible space (zed-industries#18145)
Before: ![screenshot-2024-09-26-15 00 20@2x](https://github.com/user-attachments/assets/f6706325-5bef-404e-a0b4-63a5121969fa) After: ![screenshot-2024-09-26-15 02 24@2x](https://github.com/user-attachments/assets/739d0831-0b4a-457f-917e-10f3a662e74d) Release Notes: - Improved the git blame gutter to take up only the space required to display the longest git author name in the current file. --------- Co-authored-by: Bennet Bo Fenner <[email protected]>
1 parent 3cb87cd commit 8d09dd6

File tree

3 files changed

+65
-22
lines changed

3 files changed

+65
-22
lines changed

crates/editor/src/editor.rs

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -663,7 +663,7 @@ pub struct EditorSnapshot {
663663
show_git_diff_gutter: Option<bool>,
664664
show_code_actions: Option<bool>,
665665
show_runnables: Option<bool>,
666-
render_git_blame_gutter: bool,
666+
git_blame_gutter_max_author_length: Option<usize>,
667667
pub display_snapshot: DisplaySnapshot,
668668
pub placeholder_text: Option<Arc<str>>,
669669
is_focused: bool,
@@ -673,7 +673,7 @@ pub struct EditorSnapshot {
673673
gutter_hovered: bool,
674674
}
675675

676-
const GIT_BLAME_GUTTER_WIDTH_CHARS: f32 = 53.;
676+
const GIT_BLAME_MAX_AUTHOR_CHARS_DISPLAYED: usize = 20;
677677

678678
#[derive(Default, Debug, Clone, Copy)]
679679
pub struct GutterDimensions {
@@ -2211,14 +2211,27 @@ impl Editor {
22112211
}
22122212

22132213
pub fn snapshot(&mut self, cx: &mut WindowContext) -> EditorSnapshot {
2214+
let git_blame_gutter_max_author_length = self
2215+
.render_git_blame_gutter(cx)
2216+
.then(|| {
2217+
if let Some(blame) = self.blame.as_ref() {
2218+
let max_author_length =
2219+
blame.update(cx, |blame, cx| blame.max_author_length(cx));
2220+
Some(max_author_length)
2221+
} else {
2222+
None
2223+
}
2224+
})
2225+
.flatten();
2226+
22142227
EditorSnapshot {
22152228
mode: self.mode,
22162229
show_gutter: self.show_gutter,
22172230
show_line_numbers: self.show_line_numbers,
22182231
show_git_diff_gutter: self.show_git_diff_gutter,
22192232
show_code_actions: self.show_code_actions,
22202233
show_runnables: self.show_runnables,
2221-
render_git_blame_gutter: self.render_git_blame_gutter(cx),
2234+
git_blame_gutter_max_author_length,
22222235
display_snapshot: self.display_map.update(cx, |map, cx| map.snapshot(cx)),
22232236
scroll_anchor: self.scroll_manager.anchor(),
22242237
ongoing_scroll: self.scroll_manager.ongoing_scroll(),
@@ -12989,9 +13002,19 @@ impl EditorSnapshot {
1298913002

1299013003
let show_runnables = self.show_runnables.unwrap_or(gutter_settings.runnables);
1299113004

12992-
let git_blame_entries_width = self
12993-
.render_git_blame_gutter
12994-
.then_some(em_width * GIT_BLAME_GUTTER_WIDTH_CHARS);
13005+
let git_blame_entries_width =
13006+
self.git_blame_gutter_max_author_length
13007+
.map(|max_author_length| {
13008+
// Length of the author name, but also space for the commit hash,
13009+
// the spacing and the timestamp.
13010+
let max_char_count = max_author_length
13011+
.min(GIT_BLAME_MAX_AUTHOR_CHARS_DISPLAYED)
13012+
+ 7 // length of commit sha
13013+
+ 14 // length of max relative timestamp ("60 minutes ago")
13014+
+ 4; // gaps and margins
13015+
13016+
em_advance * max_char_count
13017+
});
1299513018

1299613019
let mut left_padding = git_blame_entries_width.unwrap_or(Pixels::ZERO);
1299713020
left_padding += if show_code_actions || show_runnables {

crates/editor/src/element.rs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use crate::{
2121
EditorSnapshot, EditorStyle, ExpandExcerpts, FocusedBlock, GutterDimensions, HalfPageDown,
2222
HalfPageUp, HandleInput, HoveredCursor, HoveredHunk, LineDown, LineUp, OpenExcerpts, PageDown,
2323
PageUp, Point, RowExt, RowRangeExt, SelectPhase, Selection, SoftWrap, ToPoint,
24-
CURSORS_VISIBLE_FOR, MAX_LINE_LEN,
24+
CURSORS_VISIBLE_FOR, GIT_BLAME_MAX_AUTHOR_CHARS_DISPLAYED, MAX_LINE_LEN,
2525
};
2626
use client::ParticipantIndex;
2727
use collections::{BTreeMap, HashMap};
@@ -1445,7 +1445,7 @@ impl EditorElement {
14451445
AvailableSpace::MaxContent
14461446
};
14471447
let scroll_top = scroll_position.y * line_height;
1448-
let start_x = em_width * 1;
1448+
let start_x = em_width;
14491449

14501450
let mut last_used_color: Option<(PlayerColor, Oid)> = None;
14511451

@@ -4228,7 +4228,7 @@ fn render_blame_entry(
42284228
let short_commit_id = blame_entry.sha.display_short();
42294229

42304230
let author_name = blame_entry.author.as_deref().unwrap_or("<no name>");
4231-
let name = util::truncate_and_trailoff(author_name, 20);
4231+
let name = util::truncate_and_trailoff(author_name, GIT_BLAME_MAX_AUTHOR_CHARS_DISPLAYED);
42324232

42334233
let details = blame.read(cx).details_for_entry(&blame_entry);
42344234

@@ -4240,22 +4240,21 @@ fn render_blame_entry(
42404240

42414241
h_flex()
42424242
.w_full()
4243+
.justify_between()
42434244
.font_family(style.text.font().family)
42444245
.line_height(style.text.line_height)
42454246
.id(("blame", ix))
4246-
.children([
4247-
div()
4248-
.text_color(sha_color.cursor)
4249-
.child(short_commit_id)
4250-
.mr_2(),
4251-
div()
4252-
.w_full()
4253-
.h_flex()
4254-
.justify_between()
4255-
.text_color(cx.theme().status().hint)
4256-
.child(name)
4257-
.child(relative_timestamp),
4258-
])
4247+
.text_color(cx.theme().status().hint)
4248+
.pr_2()
4249+
.gap_2()
4250+
.child(
4251+
h_flex()
4252+
.items_center()
4253+
.gap_2()
4254+
.child(div().text_color(sha_color.cursor).child(short_commit_id))
4255+
.child(name),
4256+
)
4257+
.child(relative_timestamp)
42594258
.on_mouse_down(MouseButton::Right, {
42604259
let blame_entry = blame_entry.clone();
42614260
let details = details.clone();

crates/editor/src/git/blame.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,27 @@ impl GitBlame {
207207
})
208208
}
209209

210+
pub fn max_author_length(&mut self, cx: &mut ModelContext<Self>) -> usize {
211+
self.sync(cx);
212+
213+
let mut max_author_length = 0;
214+
215+
for entry in self.entries.iter() {
216+
let author_len = entry
217+
.blame
218+
.as_ref()
219+
.and_then(|entry| entry.author.as_ref())
220+
.map(|author| author.len());
221+
if let Some(author_len) = author_len {
222+
if author_len > max_author_length {
223+
max_author_length = author_len;
224+
}
225+
}
226+
}
227+
228+
max_author_length
229+
}
230+
210231
pub fn blur(&mut self, _: &mut ModelContext<Self>) {
211232
self.focused = false;
212233
}

0 commit comments

Comments
 (0)