Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

editor: Expand excerpts from deleted diff hunks (#63946)

Ibrahim Khanc6124d3525ce
diff --git a/crates/editor/src/editor_tests.rs b/crates/editor/src/editor_tests.rs
index 2fe4da1e264..f57cc2450d6 100644
--- a/crates/editor/src/editor_tests.rs
+++ b/crates/editor/src/editor_tests.rs
@@ -5189,6 +5189,213 @@ async fn test_newline_below_with_cursor_on_deleted_hunk(cx: &mut TestAppContext)
);
}
+#[gpui::test]
+fn test_expand_excerpts_with_selection_ending_at_excerpt_boundary(cx: &mut TestAppContext) {
+ init_test(cx, |_| {});
+
+ for (direction, expected) in [
+ (ExpandExcerptDirection::Up, "a0\na1\na2\na3\nb1\nb2\nb3"),
+ (ExpandExcerptDirection::Down, "a1\na2\na3\na4\nb1\nb2\nb3"),
+ (
+ ExpandExcerptDirection::UpAndDown,
+ "a0\na1\na2\na3\na4\nb1\nb2\nb3",
+ ),
+ ] {
+ let mut cx = EditorTestContext::new_multibuffer(
+ cx,
+ ["a0\n«a1\na2\na3»\na4", "b0\n«b1\nb2\nb3»\nb4"],
+ );
+ cx.update_editor(|editor, window, cx| {
+ editor.change_selections(Default::default(), window, cx, |selections| {
+ selections.select_ranges([Point::zero()..Point::new(3, 0)]);
+ });
+ match direction {
+ ExpandExcerptDirection::Up => {
+ editor.expand_excerpts_up(&ExpandExcerptsUp { lines: 1 }, window, cx)
+ }
+ ExpandExcerptDirection::Down => {
+ editor.expand_excerpts_down(&ExpandExcerptsDown { lines: 1 }, window, cx)
+ }
+ ExpandExcerptDirection::UpAndDown => {
+ editor.expand_excerpts(&ExpandExcerpts { lines: 1 }, window, cx)
+ }
+ }
+ });
+ assert_eq!(cx.buffer_text(), expected, "{direction:?}");
+ }
+}
+
+#[gpui::test]
+fn test_expand_excerpts_with_trailing_empty_excerpt(cx: &mut TestAppContext) {
+ init_test(cx, |_| {});
+
+ for (direction, expected) in [
+ (ExpandExcerptDirection::Up, "aaa\none\n"),
+ (ExpandExcerptDirection::Down, "aaa\n"),
+ (ExpandExcerptDirection::UpAndDown, "aaa\none\n"),
+ ] {
+ for select_all in [false, true] {
+ let mut cx = EditorTestContext::new_multibuffer(cx, ["«aaa»", "zero\none\n«»"]);
+ cx.update_editor(|editor, window, cx| {
+ if select_all {
+ editor.select_all(&SelectAll, window, cx);
+ } else {
+ editor.move_to_end(&MoveToEnd, window, cx);
+ }
+ match direction {
+ ExpandExcerptDirection::Up => {
+ editor.expand_excerpts_up(&ExpandExcerptsUp { lines: 1 }, window, cx)
+ }
+ ExpandExcerptDirection::Down => {
+ editor.expand_excerpts_down(&ExpandExcerptsDown { lines: 1 }, window, cx)
+ }
+ ExpandExcerptDirection::UpAndDown => {
+ editor.expand_excerpts(&ExpandExcerpts { lines: 1 }, window, cx)
+ }
+ }
+ });
+ assert_eq!(
+ cx.buffer_text(),
+ expected,
+ "{direction:?}, select_all={select_all}"
+ );
+ }
+ }
+}
+
+#[gpui::test]
+async fn test_expand_excerpts_with_selection_on_deleted_hunk(cx: &mut TestAppContext) {
+ init_test(cx, |_| {});
+ let mut cx = EditorTestContext::new(cx).await;
+
+ cx.set_state("zero\none\ntwo\nthree\nfour\nfive\nsix\nsevenˇ");
+ let buffer = cx.multibuffer(|multi_buffer, _| multi_buffer.as_singleton().unwrap());
+ cx.update_multibuffer(|multi_buffer, cx| {
+ let path_key = PathKey::for_buffer(&buffer, cx);
+ multi_buffer.set_excerpts_for_path(
+ path_key,
+ buffer,
+ [Point::new(2, 0)..Point::new(5, 4)],
+ 0,
+ cx,
+ );
+ });
+ cx.set_head_text("zero\none\ntwo\nthree\ndeleted\nfour\nfive\nsix\nseven");
+ cx.run_until_parked();
+ cx.update_editor(|editor, window, cx| {
+ editor.expand_all_diff_hunks(&Default::default(), window, cx);
+ });
+ cx.run_until_parked();
+ assert_eq!(cx.buffer_text(), "two\nthree\ndeleted\nfour\nfive");
+
+ cx.update_editor(|editor, window, cx| {
+ editor.change_selections(Default::default(), window, cx, |selections| {
+ let cursor = DisplayPoint::new(DisplayRow(2), 0);
+ selections.select_display_ranges([cursor..cursor]);
+ });
+ });
+ assert!(cx.update_editor(|editor, _, _| {
+ editor
+ .selections
+ .newest_anchor()
+ .head()
+ .diff_base_anchor()
+ .is_some()
+ }));
+ cx.update_editor(|editor, window, cx| {
+ editor.expand_excerpts(&ExpandExcerpts { lines: 1 }, window, cx);
+ });
+ assert_eq!(
+ cx.buffer_text(),
+ "one\ntwo\nthree\ndeleted\nfour\nfive\nsix"
+ );
+
+ cx.update_editor(|editor, window, cx| {
+ editor.change_selections(Default::default(), window, cx, |selections| {
+ selections.select_display_ranges([
+ DisplayPoint::new(DisplayRow(3), 1)..DisplayPoint::new(DisplayRow(3), 4)
+ ]);
+ });
+ });
+ cx.update_editor(|editor, window, cx| {
+ editor.expand_excerpts(&ExpandExcerpts { lines: 1 }, window, cx);
+ });
+ assert_eq!(
+ cx.buffer_text(),
+ "zero\none\ntwo\nthree\ndeleted\nfour\nfive\nsix\nseven"
+ );
+}
+
+#[gpui::test]
+async fn test_expand_excerpts_with_selection_in_removed_path(cx: &mut TestAppContext) {
+ init_test(cx, |_| {});
+
+ let buffer_a = cx.new(|cx| Buffer::local("a0\na1\na2\na3\na4", cx));
+ let buffer_b = cx.new(|cx| Buffer::local("b0\nb1\nb2\nb3\nb4", cx));
+ let multibuffer = cx.new(|cx| {
+ let mut multibuffer = MultiBuffer::new(ReadWrite);
+ multibuffer.set_excerpts_for_path(
+ PathKey::sorted(0),
+ buffer_a,
+ [Point::new(1, 0)..Point::new(3, 2)],
+ 0,
+ cx,
+ );
+ multibuffer.set_excerpts_for_path(
+ PathKey::sorted(1),
+ buffer_b.clone(),
+ [Point::new(1, 0)..Point::new(3, 2)],
+ 0,
+ cx,
+ );
+ multibuffer
+ });
+
+ let fs = FakeFs::new(cx.executor());
+ let project = Project::test(fs, [path!("/").as_ref()], cx).await;
+ let (editor, cx) = cx.add_window_view(|window, cx| {
+ build_editor_with_project(project, multibuffer.clone(), window, cx)
+ });
+ // The second path's excerpt begins with a deleted hunk.
+ editor.update_in(cx, |editor, window, cx| {
+ let diff = cx.new(|cx| {
+ BufferDiff::new_with_base_text(
+ "b0\ndeleted\nb1\nb2\nb3\nb4",
+ &buffer_b.read(cx).text_snapshot(),
+ cx,
+ )
+ });
+ editor
+ .buffer
+ .update(cx, |buffer, cx| buffer.add_diff(diff, cx));
+ editor.expand_all_diff_hunks(&Default::default(), window, cx);
+ });
+ cx.run_until_parked();
+ editor.update(cx, |editor, cx| {
+ assert_eq!(editor.text(cx), "a1\na2\na3\ndeleted\nb1\nb2\nb3");
+ });
+
+ editor.update_in(cx, |editor, window, cx| {
+ editor.change_selections(Default::default(), window, cx, |selections| {
+ selections.select_ranges([Point::new(1, 0)..Point::new(1, 0)]);
+ });
+ });
+ multibuffer.update(cx, |multibuffer, cx| {
+ multibuffer.remove_excerpts(PathKey::sorted(0), cx);
+ });
+ cx.run_until_parked();
+ editor.update(cx, |editor, cx| {
+ assert_eq!(editor.text(cx), "deleted\nb1\nb2\nb3");
+ });
+
+ editor.update_in(cx, |editor, window, cx| {
+ editor.expand_excerpts(&ExpandExcerpts { lines: 1 }, window, cx);
+ });
+ editor.update(cx, |editor, cx| {
+ assert_eq!(editor.text(cx), "b0\ndeleted\nb1\nb2\nb3\nb4");
+ });
+}
+
#[gpui::test]
fn test_newline_below_multibuffer(cx: &mut TestAppContext) {
init_test(cx, |_| {});
@@ -35674,6 +35881,37 @@ async fn test_bookmarks_tab_highlights_bookmark_on_trailing_empty_line(cx: &mut
);
}
+#[gpui::test]
+async fn test_expand_excerpts_in_bookmarks_tab_with_trailing_empty_excerpt(
+ cx: &mut TestAppContext,
+) {
+ let (workspace, _pane, project, editor, mut cx) =
+ init_bookmarks_tab_test(cx, json!({ "main.rs": "aaa", "other.rs": "zero\none\n" })).await;
+ cx.update(|_, cx| {
+ SettingsStore::update_global(cx, |store, cx| {
+ store.update_user_settings(cx, |settings| {
+ settings.editor.excerpt_context_lines = Some(0);
+ });
+ });
+ });
+ toggle_bookmark_on_row(&editor, 0, &mut cx);
+ let other_editor = open_bookmarks_test_editor(&workspace, &project, "other.rs", &mut cx).await;
+ toggle_bookmark_on_row(&other_editor, 2, &mut cx);
+
+ let bookmarks_editor = open_and_find_bookmarks_tab(&workspace, &mut cx);
+ assert_eq!(
+ bookmarks_editor.read_with(&cx, |editor, cx| editor.text(cx)),
+ "aaa\n"
+ );
+ cx.dispatch_action(SelectAll);
+ cx.dispatch_action(ExpandExcerptsUp { lines: 1 });
+ cx.run_until_parked();
+ assert_eq!(
+ bookmarks_editor.read_with(&cx, |editor, cx| editor.text(cx)),
+ "aaa\none\n"
+ );
+}
+
#[gpui::test]
async fn test_bookmarks_tab_keeps_unopenable_paths(cx: &mut TestAppContext) {
let (workspace, _pane, project, editor, mut cx) = init_bookmarks_tab_test(
diff --git a/crates/editor/src/navigation.rs b/crates/editor/src/navigation.rs
index 1fc78b31981..c74e150735c 100644
--- a/crates/editor/src/navigation.rs
+++ b/crates/editor/src/navigation.rs
@@ -2359,10 +2359,11 @@ impl Editor {
.iter()
.flat_map(|selection| {
snapshot
- .range_to_buffer_ranges(selection.range())
- .into_iter()
- .filter_map(|(buffer_snapshot, range, _)| {
- snapshot.anchor_in_excerpt(buffer_snapshot.anchor_after(range.start))
+ .range_to_buffer_ranges_with_deleted_hunks(selection.range())
+ .filter_map(|(buffer_snapshot, range, deleted_hunk_anchor)| {
+ deleted_hunk_anchor.or_else(|| {
+ snapshot.anchor_in_excerpt(buffer_snapshot.anchor_after(range.start))
+ })
})
})
.collect::<Vec<_>>();
diff --git a/crates/multi_buffer/src/multi_buffer.rs b/crates/multi_buffer/src/multi_buffer.rs
index b5fe19682b7..1d98e24e8b4 100644
--- a/crates/multi_buffer/src/multi_buffer.rs
+++ b/crates/multi_buffer/src/multi_buffer.rs
@@ -3774,10 +3774,18 @@ impl MultiBufferSnapshot {
cursor.seek(&start);
std::iter::from_fn(move || {
- let region = cursor.region()?;
- if region.range.start > end {
- return None;
- }
+ let Some(region) = cursor.region().filter(|region| {
+ region.range.start < end || (region.range.start == end && start == end)
+ }) else {
+ let excerpt = cursor.excerpt()?;
+ if excerpt.text_summary.len != 0 || end != self.len() {
+ return None;
+ }
+ let buffer = excerpt.buffer_snapshot(self);
+ let offset = BufferOffset(excerpt.range.context.start.to_offset(buffer));
+ cursor.next_excerpt();
+ return Some((buffer, offset..offset, None));
+ };
let start_overshoot = start.saturating_sub(region.range.start);
let end_overshoot = end.saturating_sub(region.range.start);
let start = region
diff --git a/crates/multi_buffer/src/multi_buffer_tests.rs b/crates/multi_buffer/src/multi_buffer_tests.rs
index 651e618b67e..462b646e0d0 100644
--- a/crates/multi_buffer/src/multi_buffer_tests.rs
+++ b/crates/multi_buffer/src/multi_buffer_tests.rs
@@ -1322,6 +1322,45 @@ fn test_expand_excerpts(cx: &mut App) {
);
}
+#[gpui::test]
+fn test_expand_excerpts_with_anchor_for_removed_path(cx: &mut App) {
+ let buffer_a = cx.new(|cx| Buffer::local(sample_text(10, 3, 'a'), cx));
+ let buffer_b = cx.new(|cx| Buffer::local(sample_text(10, 3, 'a'), cx));
+ let multibuffer = cx.new(|_| MultiBuffer::new(Capability::ReadWrite));
+
+ multibuffer.update(cx, |multibuffer, cx| {
+ multibuffer.set_excerpts_for_path(
+ PathKey::sorted(0),
+ buffer_a,
+ vec![Point::new(3, 0)..Point::new(3, 3)],
+ 1,
+ cx,
+ );
+ multibuffer.set_excerpts_for_path(
+ PathKey::sorted(1),
+ buffer_b,
+ vec![Point::new(3, 0)..Point::new(3, 3)],
+ 1,
+ cx,
+ );
+ });
+
+ let anchor_in_a = multibuffer
+ .read(cx)
+ .snapshot(cx)
+ .anchor_before(Point::new(1, 0));
+ multibuffer.update(cx, |multibuffer, cx| {
+ multibuffer.remove_excerpts(PathKey::sorted(0), cx);
+ });
+ assert_eq!(multibuffer.read(cx).snapshot(cx).text(), "ccc\nddd\neee");
+
+ multibuffer.update(cx, |multibuffer, cx| {
+ multibuffer.expand_excerpts([anchor_in_a], 1, ExpandExcerptDirection::UpAndDown, cx);
+ });
+
+ assert_eq!(multibuffer.read(cx).snapshot(cx).text(), "ccc\nddd\neee");
+}
+
#[gpui::test(iterations = 100)]
async fn test_set_anchored_excerpts_for_path(cx: &mut TestAppContext) {
let buffer_1 = cx.new(|cx| Buffer::local(sample_text(20, 3, 'a'), cx));
@@ -6263,6 +6302,24 @@ fn test_range_to_buffer_ranges(cx: &mut App) {
"Should include trailing empty excerpts"
);
assert_eq!(ranges_half_open_max[1].1, BufferOffset(0)..BufferOffset(0));
+
+ for snapshot in [&snapshot, &snapshot_trailing] {
+ for start in 0..=snapshot.len().0 {
+ for end in start..=snapshot.len().0 {
+ let range = MultiBufferOffset(start)..MultiBufferOffset(end);
+ let expected = snapshot
+ .range_to_buffer_ranges(range.clone())
+ .into_iter()
+ .map(|(buffer, range, _)| (buffer.remote_id(), range, None))
+ .collect::<Vec<_>>();
+ let actual = snapshot
+ .range_to_buffer_ranges_with_deleted_hunks(range.clone())
+ .map(|(buffer, range, anchor)| (buffer.remote_id(), range, anchor))
+ .collect::<Vec<_>>();
+ assert_eq!(actual, expected, "{range:?}");
+ }
+ }
+ }
}
#[gpui::test]
diff --git a/crates/multi_buffer/src/path_key.rs b/crates/multi_buffer/src/path_key.rs
index 2b268924ccb..2c1483f0896 100644
--- a/crates/multi_buffer/src/path_key.rs
+++ b/crates/multi_buffer/src/path_key.rs
@@ -258,6 +258,7 @@ impl MultiBuffer {
cursor.seek_forward(path, Bias::Left);
let Some((buffer, buffer_snapshot)) = cursor
.item()
+ .filter(|excerpt| &excerpt.path_key == path)
.map(|excerpt| (excerpt.buffer(&self), excerpt.buffer_snapshot(&snapshot)))
else {
continue;