From aa89632286487304ed2a6626e786dd42233277eb Mon Sep 17 00:00:00 2001 From: Mikayla Maki Date: Wed, 17 May 2023 11:40:30 -0700 Subject: [PATCH] Add diff hunks to the scroll bar --- crates/editor/src/element.rs | 49 ++++++++++++++++++++++++++++++++++-- 1 file changed, 47 insertions(+), 2 deletions(-) diff --git a/crates/editor/src/element.rs b/crates/editor/src/element.rs index c1710b7337..1d4aff39de 100644 --- a/crates/editor/src/element.rs +++ b/crates/editor/src/element.rs @@ -1022,15 +1022,16 @@ impl EditorElement { let mut first_row_y_offset = 0.0; // Impose a minimum height on the scrollbar thumb + let row_height = height / max_row; let min_thumb_height = style.min_height_factor * cx.font_cache.line_height(self.style.text.font_size); - let thumb_height = (row_range.end - row_range.start) * height / max_row; + let thumb_height = (row_range.end - row_range.start) * row_height; if thumb_height < min_thumb_height { first_row_y_offset = (min_thumb_height - thumb_height) / 2.0; height -= min_thumb_height - thumb_height; } - let y_for_row = |row: f32| -> f32 { top + first_row_y_offset + row * height / max_row }; + let y_for_row = |row: f32| -> f32 { top + first_row_y_offset + row * row_height }; let thumb_top = y_for_row(row_range.start) - first_row_y_offset; let thumb_bottom = y_for_row(row_range.end) + first_row_y_offset; @@ -1044,6 +1045,50 @@ impl EditorElement { background: style.track.background_color, ..Default::default() }); + + let diff_style = cx.global::().theme.editor.diff.clone(); + for hunk in layout + .position_map + .snapshot + .buffer_snapshot + .git_diff_hunks_in_range(0..(max_row.floor() as u32), false) + { + let start_y = y_for_row(hunk.buffer_range.start as f32); + let mut end_y = if hunk.buffer_range.start == hunk.buffer_range.end { + y_for_row((hunk.buffer_range.end + 1) as f32) + } else { + y_for_row((hunk.buffer_range.end) as f32) + }; + + if end_y - start_y < 1. { + end_y = start_y + 1.; + } + let bounds = RectF::from_points(vec2f(left, start_y), vec2f(right, end_y)); + + let color = match hunk.status() { + DiffHunkStatus::Added => diff_style.inserted, + DiffHunkStatus::Modified => diff_style.modified, + DiffHunkStatus::Removed => diff_style.deleted, + }; + + let border = Border { + width: 1., + color: style.thumb.border.color, + overlay: false, + top: false, + right: true, + bottom: false, + left: true, + }; + + scene.push_quad(Quad { + bounds, + background: Some(color), + border, + corner_radius: style.thumb.corner_radius, + }) + } + scene.push_quad(Quad { bounds: thumb_bounds, border: style.thumb.border,