Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
editor: Push `em` width calculations down into `EditorSnapshot::gutter_dimensions` (#24062)
Marshall Bowers52a3013d7397
diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rsindex 3520d7b7bb9..86fbdefbd5c 100644--- a/crates/editor/src/editor.rs+++ b/crates/editor/src/editor.rs@@ -15089,15 +15089,16 @@ impl EditorSnapshot {&self,font_id: FontId,font_size: Pixels,- em_width: Pixels,- em_advance: Pixels,max_line_number_width: Pixels,cx: &App,- ) -> GutterDimensions {+ ) -> Option<GutterDimensions> {if !self.show_gutter {- return GutterDimensions::default();+ return None;}+let descent = cx.text_system().descent(font_id, font_size);+ let em_width = cx.text_system().em_width(font_id, font_size).log_err()?;+ let em_advance = cx.text_system().em_advance(font_id, font_size).log_err()?;let show_git_gutter = self.show_git_diff_gutter.unwrap_or_else(|| {matches!(@@ -15126,13 +15127,16 @@ impl EditorSnapshot {let git_blame_entries_width =self.git_blame_gutter_max_author_length.map(|max_author_length| {- // Length of the author name, but also space for the commit hash,- // the spacing and the timestamp.+ const MAX_RELATIVE_TIMESTAMP: &str = "60 minutes ago";++ /// The number of characters to dedicate to gaps and margins.+ const SPACING_WIDTH: usize = 4;+let max_char_count = max_author_length.min(GIT_BLAME_MAX_AUTHOR_CHARS_DISPLAYED)- + 7 // length of commit sha- + 14 // length of max relative timestamp ("60 minutes ago")- + 4; // gaps and margins+ + ::git::SHORT_SHA_LENGTH+ + MAX_RELATIVE_TIMESTAMP.len()+ + SPACING_WIDTH;em_advance * max_char_count});@@ -15158,13 +15162,13 @@ impl EditorSnapshot {px(0.)};- GutterDimensions {+ Some(GutterDimensions {left_padding,right_padding,width: line_gutter_width + left_padding + right_padding,margin: -descent,git_blame_entries_width,- }+ })}pub fn render_crease_toggle(diff --git a/crates/editor/src/element.rs b/crates/editor/src/element.rsindex 7c37416bfc2..ab51fb10f5c 100644--- a/crates/editor/src/element.rs+++ b/crates/editor/src/element.rs@@ -6476,14 +6476,14 @@ impl Element for EditorElement {let letter_size = size(em_width, line_height);- let gutter_dimensions = snapshot.gutter_dimensions(- font_id,- font_size,- em_width,- em_advance,- self.max_line_number_width(&snapshot, window, cx),- cx,- );+ let gutter_dimensions = snapshot+ .gutter_dimensions(+ font_id,+ font_size,+ self.max_line_number_width(&snapshot, window, cx),+ cx,+ )+ .unwrap_or_default();let text_width = bounds.size.width - gutter_dimensions.width;let editor_width = text_width - gutter_dimensions.margin - em_width;@@ -8043,17 +8043,11 @@ fn compute_auto_height_layout(let font_size = style.text.font_size.to_pixels(window.rem_size());let line_height = style.text.line_height_in_pixels(window.rem_size());let em_width = window.text_system().em_width(font_id, font_size).unwrap();- let em_advance = window.text_system().em_advance(font_id, font_size).unwrap();let mut snapshot = editor.snapshot(window, cx);- let gutter_dimensions = snapshot.gutter_dimensions(- font_id,- font_size,- em_width,- em_advance,- max_line_number_width,- cx,- );+ let gutter_dimensions = snapshot+ .gutter_dimensions(font_id, font_size, max_line_number_width, cx)+ .unwrap_or_default();editor.gutter_dimensions = gutter_dimensions;let text_width = width - gutter_dimensions.width;diff --git a/crates/git/src/git.rs b/crates/git/src/git.rsindex af093c353a2..3ee0d23be7b 100644--- a/crates/git/src/git.rs+++ b/crates/git/src/git.rs@@ -45,6 +45,9 @@ actions!(]);+/// The length of a Git short SHA.+pub const SHORT_SHA_LENGTH: usize = 7;+#[derive(Clone, Copy, Eq, Hash, PartialEq)]pub struct Oid(libgit::Oid);@@ -64,7 +67,7 @@ impl Oid {/// Returns this [`Oid`] as a short SHA.pub fn display_short(&self) -> String {- self.to_string().chars().take(7).collect()+ self.to_string().chars().take(SHORT_SHA_LENGTH).collect()}}