From 4121cf3d696cb430b0aaa73ffd67f917cf65d705 Mon Sep 17 00:00:00 2001 From: Cole Miller Date: Fri, 14 Feb 2025 13:25:40 -0500 Subject: [PATCH] Scroll editor to enable using stage/skip hunk controls in series --- crates/editor/src/editor.rs | 18 +++++++++++++++++ crates/editor/src/element.rs | 38 ++++++++++++++++++++++++++++++++++-- 2 files changed, 54 insertions(+), 2 deletions(-) diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs index 7a9af28614..7afd1b2db2 100644 --- a/crates/editor/src/editor.rs +++ b/crates/editor/src/editor.rs @@ -10526,6 +10526,24 @@ impl Editor { self.go_to_hunk_after_position(&snapshot, selection.head(), window, cx); } + fn hunk_after_position( + &self, + snapshot: &EditorSnapshot, + position: Point, + ) -> Option { + let mut hunk = snapshot + .buffer_snapshot + .diff_hunks_in_range(position..snapshot.buffer_snapshot.max_point()) + .find(|hunk| hunk.row_range.start.0 > position.row); + if hunk.is_none() { + hunk = snapshot + .buffer_snapshot + .diff_hunks_in_range(Point::zero()..position) + .find(|hunk| hunk.row_range.end.0 < position.row) + } + hunk + } + fn go_to_hunk_after_position( &mut self, snapshot: &EditorSnapshot, diff --git a/crates/editor/src/element.rs b/crates/editor/src/element.rs index 91cab249bd..44f9f9108c 100644 --- a/crates/editor/src/element.rs +++ b/crates/editor/src/element.rs @@ -9054,7 +9054,24 @@ fn diff_hunk_controls( editor.update(cx, |editor, cx| { let snapshot = editor.snapshot(window, cx); let position = hunk_range.end.to_point(&snapshot.buffer_snapshot); - editor.go_to_hunk_after_position(&snapshot, position, window, cx); + let Some(next_hunk) = editor.hunk_after_position(&snapshot, position) + else { + return; + }; + let next_hunk_start_point = Point::new(next_hunk.row_range.start.0, 0); + let next_hunk_display_start = + snapshot.point_to_display_point(next_hunk_start_point, Bias::Left); + let row_delta = next_hunk_display_start.row().0 as i32 - row as i32; + let scroll_delta = row_delta as f32; + editor.apply_scroll_delta( + gpui::Point::new(0., scroll_delta), + window, + cx, + ); + editor.change_selections(None, window, cx, |s| { + let point = Point::new(next_hunk.row_range.start.0, 0); + s.select_ranges([point..point]); + }); editor.expand_selected_diff_hunks(cx); }); } @@ -9080,10 +9097,27 @@ fn diff_hunk_controls( }) .on_click({ let editor = editor.clone(); - move |_event, _window, cx| { + move |_event, window, cx| { editor.update(cx, |editor, cx| { editor .stage_or_unstage_diff_hunks(&[hunk_range.start..hunk_range.start], cx); + let snapshot = editor.snapshot(window, cx); + let position = hunk_range.end.to_point(&snapshot.buffer_snapshot); + let Some(next_hunk) = editor.hunk_after_position(&snapshot, position) + else { + return; + }; + let next_hunk_start_point = Point::new(next_hunk.row_range.start.0, 0); + let next_hunk_display_start = + snapshot.point_to_display_point(next_hunk_start_point, Bias::Left); + let row_delta = next_hunk_display_start.row().0 as i32 - row as i32; + let scroll_delta = row_delta as f32; + editor.apply_scroll_delta(gpui::Point::new(0., scroll_delta), window, cx); + editor.change_selections(None, window, cx, |s| { + let point = Point::new(next_hunk.row_range.start.0, 0); + s.select_ranges([point..point]); + }); + editor.expand_selected_diff_hunks(cx); }); } }),