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.

Fix adding breakpoint/bookmark via gutter context menu using the wrong row (#59269)

guopenghuie9c484165dc8
diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs
index f5a05444fc4..aa2a5db2f17 100644
--- a/crates/editor/src/editor.rs
+++ b/crates/editor/src/editor.rs
@@ -5859,14 +5859,16 @@ impl Editor {
window: &mut Window,
cx: &mut Context<Self>,
) {
- let snapshot = self.buffer.read(cx).snapshot(cx);
- let source = snapshot.anchor_before(Point::new(display_row.0, 0u32));
+ let display_snapshot = self.display_snapshot(cx);
+ let display_point = display_row.as_display_point();
+ let source = display_snapshot.display_point_to_anchor(display_point, Bias::Left);
let anchor = position.unwrap_or(source);
// Every entry in this menu either requires a worktree-file-backed buffer
// (breakpoints, bookmarks, run to cursor) or is meaningless without one
// (git blame), so don't open it for e.g. untitled buffers.
- if !snapshot
+ if !display_snapshot
+ .buffer_snapshot()
.anchor_to_buffer_anchor(anchor)
.is_some_and(|(_, buffer_snapshot)| {
project::File::from_dyn(buffer_snapshot.file()).is_some()
diff --git a/crates/editor/src/editor_tests.rs b/crates/editor/src/editor_tests.rs
index 5e43b80e5f2..c3bdea117a4 100644
--- a/crates/editor/src/editor_tests.rs
+++ b/crates/editor/src/editor_tests.rs
@@ -5,6 +5,7 @@ use crate::{
edit_prediction_tests::FakeEditPredictionDelegate,
element::{StickyHeader, header_jump_data},
linked_editing_ranges::LinkedEditingRanges,
+ mouse_context_menu::MenuPosition,
runnables::RunnableTasks,
scroll::scroll_amount::ScrollAmount,
test::{
@@ -31515,6 +31516,116 @@ async fn test_gutter_context_menu_hidden_for_unsaved_buffer(cx: &mut TestAppCont
});
}
+#[gpui::test]
+async fn test_gutter_context_menu_anchor_in_multibuffer(cx: &mut TestAppContext) {
+ init_test(cx, |_| {});
+
+ let fs = FakeFs::new(cx.executor());
+ let root_path = path!("/root");
+ fs.insert_tree(
+ root_path,
+ json!({
+ "first.rs": "fn one() {}\nfn two() {}\nfn three() {}\n",
+ "second.rs": "fn four() {}\nfn five() {}\nfn six() {}\n",
+ }),
+ )
+ .await;
+
+ let project = Project::test(fs, [root_path.as_ref()], cx).await;
+ let window = cx.add_window(|window, cx| MultiWorkspace::test_new(project.clone(), window, cx));
+ let cx = &mut VisualTestContext::from_window(*window, cx);
+ let worktree_id = project.read_with(cx, |project, cx| {
+ project.worktrees(cx).next().unwrap().read(cx).id()
+ });
+
+ let buffer_a = project
+ .update(cx, |project, cx| {
+ project.open_buffer((worktree_id, rel_path("first.rs")), cx)
+ })
+ .await
+ .unwrap();
+
+ let buffer_b = project
+ .update(cx, |project, cx| {
+ project.open_buffer((worktree_id, rel_path("second.rs")), cx)
+ })
+ .await
+ .unwrap();
+
+ let multi_buffer = cx.new(|cx| {
+ let mut multi_buffer = MultiBuffer::new(ReadWrite);
+
+ multi_buffer.set_excerpts_for_path(
+ PathKey::sorted(0),
+ buffer_a.clone(),
+ [Point::new(0, 0)..Point::new(2, 0)],
+ 0,
+ cx,
+ );
+
+ multi_buffer.set_excerpts_for_path(
+ PathKey::sorted(1),
+ buffer_b.clone(),
+ [Point::new(0, 0)..Point::new(2, 0)],
+ 0,
+ cx,
+ );
+
+ multi_buffer
+ });
+
+ let (editor, cx) = cx.add_window_view(|window, cx| {
+ Editor::new(
+ EditorMode::full(),
+ multi_buffer,
+ Some(project.clone()),
+ window,
+ cx,
+ )
+ });
+
+ // Open the gutter context menu on a display row inside the second excerpt,
+ // ensuring that excerpt headers will now play a part in offsetting display
+ // rows from buffer rows.
+ editor.update_in(cx, |editor, window, cx| {
+ let display_snapshot = editor.display_snapshot(cx);
+ let buffer_snapshot = display_snapshot.buffer_snapshot();
+ let buffer_anchor = buffer_b.read(cx).snapshot().anchor_before(Point::new(1, 0));
+ let multibuffer_anchor = buffer_snapshot.anchor_in_buffer(buffer_anchor).unwrap();
+ let buffer_point = buffer_snapshot.summary_for_anchor::<Point>(&multibuffer_anchor);
+ let display_row = display_snapshot
+ .point_to_display_point(buffer_point, Bias::Left)
+ .row();
+
+ assert_ne!(
+ display_row.0, buffer_point.row,
+ "display row should be offset from the multibuffer row by excerpt headers"
+ );
+
+ editor.set_gutter_context_menu(display_row, None, Default::default(), window, cx);
+ });
+
+ // We'll now confirm that the `MenuPosition`'s source has been set to the
+ // correct buffer anchor (1, 0), instead of simply taking the display row.
+ editor.update(cx, |editor, cx| {
+ let menu = editor.mouse_context_menu.as_ref().unwrap();
+ let snapshot = editor.buffer().read(cx).snapshot(cx);
+ let MenuPosition::PinnedToEditor { source, .. } = &menu.position else {
+ panic!("expected gutter context menu to be pinned to the editor");
+ };
+
+ let (buffer_anchor, buffer_snapshot) = snapshot
+ .anchor_to_buffer_anchor(*source)
+ .expect("menu anchor should resolve to a buffer");
+
+ assert_eq!(buffer_snapshot.remote_id(), buffer_b.read(cx).remote_id());
+ assert_eq!(
+ buffer_snapshot.summary_for_anchor::<Point>(&buffer_anchor),
+ Point::new(1, 0),
+ );
+ });
+}
+
#[gpui::test]
async fn test_breakpoint_after_save_as_existing_path(cx: &mut TestAppContext) {
init_test(cx, |_| {});