Skip to content

Commit

Permalink
use actual blame element width for scroll width
Browse files Browse the repository at this point in the history
  • Loading branch information
0xtimsb committed Nov 23, 2024
1 parent 073af85 commit 64cd91b
Showing 1 changed file with 46 additions and 56 deletions.
102 changes: 46 additions & 56 deletions crates/editor/src/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5040,66 +5040,13 @@ impl Element for EditorElement {
);
let text_width = bounds.size.width - gutter_dimensions.width;

let blame_overflow = self.editor.update(cx, |editor, cx| {
if !editor.render_git_blame_inline(cx) {
return None;
}

let cursor_position = editor.selections.newest_anchor().head();
let buffer_snapshot = editor.buffer.read(cx).snapshot(cx);
let buffer_row =
MultiBufferRow(cursor_position.to_point(&buffer_snapshot).row);
let line_width = buffer_snapshot.line_len(buffer_row) as f32 * em_advance;

let blame_length = editor.blame.as_ref().map_or(0., |blame| {
blame
.update(cx, |blame, cx| {
blame.blame_for_rows([Some(buffer_row)], cx).next()
})
.flatten()
.map_or(0., |entry| {
const MAX_TIMESTAMP_LENGTH: f32 = 14.;
const GAP_AND_GLYPHS: f32 = 5.;

let author_length =
entry.author.map_or(0., |author| author.len() as f32);
let max_char_count =
author_length + MAX_TIMESTAMP_LENGTH + GAP_AND_GLYPHS;
max_char_count
})
});

let blame_width =
(INLINE_BLAME_PADDING_EM_WIDTHS + blame_length) * em_advance;

let longest_line_width =
layout_line(snapshot.longest_row(), &snapshot, &style, text_width, cx)
.width;

// Blame is outside the scroll bounds.
if line_width > longest_line_width {
return Some(blame_width);
}

// Blame is partially outside the scroll bounds.
if line_width + blame_width > longest_line_width {
return Some(line_width + blame_width - longest_line_width);
}

// Blame is inside the scroll bounds.
None
});

let right_margin = if snapshot.mode == EditorMode::Full {
EditorElement::SCROLLBAR_WIDTH
} else {
px(0.)
};

let overscroll = size(
em_width + right_margin + blame_overflow.unwrap_or_default(),
px(0.),
);
let overscroll = size(em_width + right_margin, px(0.));

let editor_width =
text_width - gutter_dimensions.margin - overscroll.width - em_width;
Expand Down Expand Up @@ -5309,8 +5256,51 @@ impl Element for EditorElement {
cx,
)
.width;
let mut scroll_width =
longest_line_width.max(max_visible_line_width) + overscroll.width;

let blame_overflow = self.editor.update(cx, |editor, cx| {
if !editor.render_git_blame_inline(cx) {
return None;
}

let cursor_position = editor.selections.newest_anchor().head();
let buffer_snapshot = editor.buffer.read(cx).snapshot(cx);
let buffer_row =
MultiBufferRow(cursor_position.to_point(&buffer_snapshot).row);
let line_width = buffer_snapshot.line_len(buffer_row) as f32 * em_advance;

let blame_width = editor.blame.as_ref().map_or(Pixels::ZERO, |blame| {
blame
.update(cx, |blame, cx| {
blame.blame_for_rows([Some(buffer_row)], cx).next()
})
.flatten()
.map_or(Pixels::ZERO, |entry| {
let workspace =
editor.workspace.as_ref().map(|(w, _)| w.clone());
let mut element = render_inline_blame_entry(
blame, entry, &style, workspace, cx,
);
element.layout_as_root(AvailableSpace::min_size(), cx).width
})
});

let blame_width_with_padding =
(INLINE_BLAME_PADDING_EM_WIDTHS * em_advance) + blame_width;

// If blame overflows the longest line, return the overflow amount.
if line_width + blame_width_with_padding > longest_line_width {
return Some(
line_width + blame_width_with_padding - longest_line_width,
);
}

// Else, blame is inside the scroll bounds.
None
});

let mut scroll_width = longest_line_width.max(max_visible_line_width)
+ overscroll.width
+ blame_overflow.unwrap_or_default();

let blocks = cx.with_element_namespace("blocks", |cx| {
self.render_blocks(
Expand Down

0 comments on commit 64cd91b

Please sign in to comment.