Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
editor: Fix git-hunk toggling for adjacent hunks (#43187)
Sean Hagstrom2d071b0cb64e
diff --git a/crates/editor/src/editor_tests.rs b/crates/editor/src/editor_tests.rsindex 48e59f7b742..c0112c5eda4 100644--- a/crates/editor/src/editor_tests.rs+++ b/crates/editor/src/editor_tests.rs@@ -20880,6 +20880,36 @@ async fn test_toggling_adjacent_diff_hunks(cx: &mut TestAppContext) {.to_string(),);+ cx.update_editor(|editor, window, cx| {+ editor.move_up(&MoveUp, window, cx);+ editor.toggle_selected_diff_hunks(&Default::default(), window, cx);+ });+ cx.assert_state_with_diff(+ indoc! { "+ ˇone+ - two+ three+ five+ "}+ .to_string(),+ );++ cx.update_editor(|editor, window, cx| {+ editor.move_down(&MoveDown, window, cx);+ editor.move_down(&MoveDown, window, cx);+ editor.toggle_selected_diff_hunks(&Default::default(), window, cx);+ });+ cx.assert_state_with_diff(+ indoc! { "+ one+ - two+ ˇthree+ - four+ five+ "}+ .to_string(),+ );+cx.set_state(indoc! { "oneˇTWO@@ -20919,6 +20949,66 @@ async fn test_toggling_adjacent_diff_hunks(cx: &mut TestAppContext) {);}+#[gpui::test]+async fn test_toggling_adjacent_diff_hunks_2(+ executor: BackgroundExecutor,+ cx: &mut TestAppContext,+) {+ init_test(cx, |_| {});++ let mut cx = EditorTestContext::new(cx).await;++ let diff_base = r#"+ lineA+ lineB+ lineC+ lineD+ "#+ .unindent();++ cx.set_state(+ &r#"+ ˇlineA1+ lineB+ lineD+ "#+ .unindent(),+ );+ cx.set_head_text(&diff_base);+ executor.run_until_parked();++ cx.update_editor(|editor, window, cx| {+ editor.toggle_selected_diff_hunks(&ToggleSelectedDiffHunks, window, cx);+ });+ executor.run_until_parked();+ cx.assert_state_with_diff(+ r#"+ - lineA+ + ˇlineA1+ lineB+ lineD+ "#+ .unindent(),+ );++ cx.update_editor(|editor, window, cx| {+ editor.move_down(&MoveDown, window, cx);+ editor.move_right(&MoveRight, window, cx);+ editor.toggle_selected_diff_hunks(&ToggleSelectedDiffHunks, window, cx);+ });+ executor.run_until_parked();+ cx.assert_state_with_diff(+ r#"+ - lineA+ + lineA1+ lˇineB+ - lineC+ lineD+ "#+ .unindent(),+ );+}+#[gpui::test]async fn test_edits_around_expanded_deletion_hunks(executor: BackgroundExecutor,diff --git a/crates/multi_buffer/src/multi_buffer.rs b/crates/multi_buffer/src/multi_buffer.rsindex 5b343ecc579..0c0e87b60a7 100644--- a/crates/multi_buffer/src/multi_buffer.rs+++ b/crates/multi_buffer/src/multi_buffer.rs@@ -2610,9 +2610,8 @@ impl MultiBuffer {for range in ranges {let range = range.to_point(&snapshot);let start = snapshot.point_to_offset(Point::new(range.start.row, 0));- let end = snapshot.point_to_offset(Point::new(range.end.row + 1, 0));- let start = start.saturating_sub_usize(1);- let end = snapshot.len().min(end + 1usize);+ let end = (snapshot.point_to_offset(Point::new(range.end.row + 1, 0)) + 1usize)+ .min(snapshot.len());cursor.seek(&start, Bias::Right);while let Some(item) = cursor.item() {if *cursor.start() >= end {