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: Disable gutter breakpoint and bookmark actions in unsaved buf… (#61310)

saberoueslati502cbccb0bc7
diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs
index 682ffcbe27f..4a93aae5dfd 100644
--- a/crates/editor/src/editor.rs
+++ b/crates/editor/src/editor.rs
@@ -5831,13 +5831,23 @@ impl Editor {
window: &mut Window,
cx: &mut Context<Self>,
) {
- let source = self
- .buffer
- .read(cx)
- .snapshot(cx)
- .anchor_before(Point::new(display_row.0, 0u32));
+ let snapshot = self.buffer.read(cx).snapshot(cx);
+ let source = snapshot.anchor_before(Point::new(display_row.0, 0u32));
+ 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
+ .anchor_to_buffer_anchor(anchor)
+ .is_some_and(|(_, buffer_snapshot)| {
+ project::File::from_dyn(buffer_snapshot.file()).is_some()
+ })
+ {
+ return;
+ }
- let context_menu = self.gutter_context_menu(position.unwrap_or(source), window, cx);
+ let context_menu = self.gutter_context_menu(anchor, window, cx);
self.mouse_context_menu = MouseContextMenu::pinned_to_editor(
self,
diff --git a/crates/editor/src/editor_tests.rs b/crates/editor/src/editor_tests.rs
index e69bee21561..d8af07824e1 100644
--- a/crates/editor/src/editor_tests.rs
+++ b/crates/editor/src/editor_tests.rs
@@ -31090,6 +31090,168 @@ async fn test_breakpoint_toggling(cx: &mut TestAppContext) {
assert_breakpoint(&breakpoints, &abs_path, vec![]);
}
+async fn build_gutter_hover_test_editor(saved: bool, cx: &mut TestAppContext) -> EditorTestContext {
+ init_test(cx, |_| {});
+
+ let fs = FakeFs::new(cx.executor());
+ fs.insert_tree(
+ path!("/a"),
+ json!({
+ "main.rs": "fn main() {}\n",
+ }),
+ )
+ .await;
+ let project = Project::test(fs, [path!("/a").as_ref()], cx).await;
+
+ let buffer = if saved {
+ let worktree_id = project.read_with(cx, |project, cx| {
+ project.worktrees(cx).next().unwrap().read(cx).id()
+ });
+ project
+ .update(cx, |project, cx| {
+ project.open_buffer((worktree_id, rel_path("main.rs")), cx)
+ })
+ .await
+ .unwrap()
+ } else {
+ let buffer = project
+ .update(cx, |project, cx| project.create_buffer(None, true, cx))
+ .await
+ .unwrap();
+ buffer.update(cx, |buffer, cx| {
+ buffer.edit([(0..0, "fn main() {}\n")], None, cx);
+ });
+ buffer
+ };
+
+ let window = cx.add_window(|window, cx| {
+ let editor = build_editor_with_project(
+ project,
+ MultiBuffer::build_from_buffer(buffer, cx),
+ window,
+ cx,
+ );
+ window.focus(&editor.focus_handle(cx), cx);
+ editor
+ });
+
+ EditorTestContext::for_editor(window, cx).await
+}
+
+fn hover_over_gutter_row_zero(cx: &mut EditorTestContext) {
+ cx.update(|window, cx| {
+ window.refresh();
+ let _ = window.draw(cx);
+ });
+
+ let gutter_bounds = cx.update_editor(|editor, _, _| {
+ editor
+ .last_position_map
+ .as_ref()
+ .expect("expected editor position map")
+ .gutter_hitbox
+ .bounds
+ });
+
+ let hover_position = gutter_bounds.center();
+ cx.simulate_mouse_move(hover_position, None, Modifiers::none());
+}
+
+#[gpui::test]
+async fn test_gutter_hover_button_shown_for_saved_buffer(cx: &mut TestAppContext) {
+ let mut cx = build_gutter_hover_test_editor(true, cx).await;
+
+ hover_over_gutter_row_zero(&mut cx);
+ cx.update_editor(|editor, _, _| {
+ assert!(
+ editor.gutter_hover_button.0.is_some(),
+ "expected gutter hover button to be armed for a saved buffer"
+ );
+ assert!(!editor.gutter_hover_button.0.as_ref().unwrap().is_active);
+ });
+
+ cx.executor().advance_clock(Duration::from_millis(250));
+ cx.run_until_parked();
+ cx.update_editor(|editor, _, _| {
+ assert!(
+ editor
+ .gutter_hover_button
+ .0
+ .as_ref()
+ .is_some_and(|indicator| indicator.is_active),
+ "expected gutter hover button to become active after the debounce"
+ );
+ });
+}
+
+#[gpui::test]
+async fn test_gutter_hover_button_hidden_for_unsaved_buffer(cx: &mut TestAppContext) {
+ let mut cx = build_gutter_hover_test_editor(false, cx).await;
+
+ hover_over_gutter_row_zero(&mut cx);
+ cx.update_editor(|editor, _, _| {
+ assert!(
+ editor.gutter_hover_button.0.is_none(),
+ "expected no gutter hover button for an unsaved buffer"
+ );
+ assert!(
+ editor.gutter_hover_button.1.is_none(),
+ "expected no debounce task to be armed for an unsaved buffer"
+ );
+ });
+
+ cx.executor().advance_clock(Duration::from_millis(250));
+ cx.run_until_parked();
+ cx.update_editor(|editor, _, _| {
+ assert!(editor.gutter_hover_button.0.is_none());
+ });
+}
+
+fn right_click_gutter_row_zero(cx: &mut EditorTestContext) {
+ cx.update(|window, cx| {
+ window.refresh();
+ let _ = window.draw(cx);
+ });
+
+ let gutter_bounds = cx.update_editor(|editor, _, _| {
+ editor
+ .last_position_map
+ .as_ref()
+ .expect("expected editor position map")
+ .gutter_hitbox
+ .bounds
+ });
+
+ let click_position = gutter_bounds.center();
+ cx.simulate_mouse_down(click_position, MouseButton::Right, Modifiers::none());
+}
+
+#[gpui::test]
+async fn test_gutter_context_menu_shown_for_saved_buffer(cx: &mut TestAppContext) {
+ let mut cx = build_gutter_hover_test_editor(true, cx).await;
+
+ right_click_gutter_row_zero(&mut cx);
+ cx.update_editor(|editor, _, _| {
+ assert!(
+ editor.mouse_context_menu.is_some(),
+ "expected gutter context menu to open for a saved buffer"
+ );
+ });
+}
+
+#[gpui::test]
+async fn test_gutter_context_menu_hidden_for_unsaved_buffer(cx: &mut TestAppContext) {
+ let mut cx = build_gutter_hover_test_editor(false, cx).await;
+
+ right_click_gutter_row_zero(&mut cx);
+ cx.update_editor(|editor, _, _| {
+ assert!(
+ editor.mouse_context_menu.is_none(),
+ "expected no gutter context menu for an unsaved buffer"
+ );
+ });
+}
+
#[gpui::test]
async fn test_breakpoint_after_save_as_existing_path(cx: &mut TestAppContext) {
init_test(cx, |_| {});
diff --git a/crates/editor/src/element/mouse.rs b/crates/editor/src/element/mouse.rs
index 5c0709c8d2d..4d174e71c08 100644
--- a/crates/editor/src/element/mouse.rs
+++ b/crates/editor/src/element/mouse.rs
@@ -184,11 +184,16 @@ impl EditorElement {
.snapshot
.display_point_to_anchor(valid_point, Bias::Left);
+ // Breakpoints and bookmarks are keyed by absolute file path, so the
+ // gutter button would be a no-op for buffers without a worktree file
+ // (e.g. untitled buffers). Hide it there.
if position_map
.snapshot
.buffer_snapshot()
.anchor_to_buffer_anchor(buffer_anchor)
- .is_some()
+ .is_some_and(|(_, buffer_snapshot)| {
+ project::File::from_dyn(buffer_snapshot.file()).is_some()
+ })
{
let is_visible = editor
.gutter_hover_button