1use crate::{
2 Copy, CopyAndTrim, CopyPermalinkToLine, Cut, DisplayPoint, DisplaySnapshot, Editor,
3 EvaluateSelectedText, FindAllReferences, GoToDeclaration, GoToDefinition, GoToImplementation,
4 GoToTypeDefinition, Paste, Rename, RevealInFileManager, RunToCursor, SelectMode,
5 SelectionEffects, SelectionExt, ToDisplayPoint, ToggleCodeActions,
6 actions::{Format, FormatSelections},
7 selections_collection::SelectionsCollection,
8};
9use gpui::prelude::FluentBuilder;
10use gpui::{Context, DismissEvent, Entity, Focusable as _, Pixels, Point, Subscription, Window};
11use project::DisableAiSettings;
12use settings::Settings;
13use std::ops::Range;
14use text::PointUtf16;
15use workspace::OpenInTerminal;
16use zed_actions::agent::AddSelectionToThread;
17
18#[derive(Debug)]
19pub enum MenuPosition {
20 /// When the editor is scrolled, the context menu stays on the exact
21 /// same position on the screen, never disappearing.
22 PinnedToScreen(Point<Pixels>),
23 /// When the editor is scrolled, the context menu follows the position it is associated with.
24 /// Disappears when the position is no longer visible.
25 PinnedToEditor {
26 source: multi_buffer::Anchor,
27 offset: Point<Pixels>,
28 },
29}
30
31pub struct MouseContextMenu {
32 pub(crate) position: MenuPosition,
33 pub(crate) context_menu: Entity<ui::ContextMenu>,
34 _dismiss_subscription: Subscription,
35 _cursor_move_subscription: Subscription,
36}
37
38impl std::fmt::Debug for MouseContextMenu {
39 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
40 f.debug_struct("MouseContextMenu")
41 .field("position", &self.position)
42 .field("context_menu", &self.context_menu)
43 .finish()
44 }
45}
46
47impl MouseContextMenu {
48 pub(crate) fn pinned_to_editor(
49 editor: &mut Editor,
50 source: multi_buffer::Anchor,
51 position: Point<Pixels>,
52 context_menu: Entity<ui::ContextMenu>,
53 window: &mut Window,
54 cx: &mut Context<Editor>,
55 ) -> Option<Self> {
56 let editor_snapshot = editor.snapshot(window, cx);
57 let content_origin = editor.last_bounds?.origin
58 + Point {
59 x: editor.gutter_dimensions.width,
60 y: Pixels::ZERO,
61 };
62 let source_position = editor.to_pixel_point(source, &editor_snapshot, window, cx)?;
63 let menu_position = MenuPosition::PinnedToEditor {
64 source,
65 offset: position - (source_position + content_origin),
66 };
67 Some(MouseContextMenu::new(
68 editor,
69 menu_position,
70 context_menu,
71 window,
72 cx,
73 ))
74 }
75
76 pub(crate) fn new(
77 editor: &Editor,
78 position: MenuPosition,
79 context_menu: Entity<ui::ContextMenu>,
80 window: &mut Window,
81 cx: &mut Context<Editor>,
82 ) -> Self {
83 let context_menu_focus = context_menu.focus_handle(cx);
84
85 // Since `ContextMenu` is rendered in a deferred fashion its focus
86 // handle is not linked to the Editor's until after the deferred draw
87 // callback runs.
88 // We need to wait for that to happen before focusing it, so that
89 // calling `contains_focused` on the editor's focus handle returns
90 // `true` when the `ContextMenu` is focused.
91 let focus_handle = context_menu_focus.clone();
92 cx.on_next_frame(window, move |_, window, cx| {
93 cx.on_next_frame(window, move |_, window, _cx| {
94 window.focus(&focus_handle);
95 });
96 });
97
98 let _dismiss_subscription = cx.subscribe_in(&context_menu, window, {
99 let context_menu_focus = context_menu_focus.clone();
100 move |editor, _, _event: &DismissEvent, window, cx| {
101 editor.mouse_context_menu.take();
102 if context_menu_focus.contains_focused(window, cx) {
103 window.focus(&editor.focus_handle(cx));
104 }
105 }
106 });
107
108 let selection_init = editor.selections.newest_anchor().clone();
109
110 let _cursor_move_subscription = cx.subscribe_in(
111 &cx.entity(),
112 window,
113 move |editor, _, event: &crate::EditorEvent, window, cx| {
114 let crate::EditorEvent::SelectionsChanged { local: true } = event else {
115 return;
116 };
117 let display_snapshot = &editor
118 .display_map
119 .update(cx, |display_map, cx| display_map.snapshot(cx));
120 let selection_init_range = selection_init.display_range(display_snapshot);
121 let selection_now_range = editor
122 .selections
123 .newest_anchor()
124 .display_range(display_snapshot);
125 if selection_now_range == selection_init_range {
126 return;
127 }
128 editor.mouse_context_menu.take();
129 if context_menu_focus.contains_focused(window, cx) {
130 window.focus(&editor.focus_handle(cx));
131 }
132 },
133 );
134
135 Self {
136 position,
137 context_menu,
138 _dismiss_subscription,
139 _cursor_move_subscription,
140 }
141 }
142}
143
144fn display_ranges<'a>(
145 display_map: &'a DisplaySnapshot,
146 selections: &'a SelectionsCollection,
147) -> impl Iterator<Item = Range<DisplayPoint>> + 'a {
148 let pending = selections.pending_anchor();
149 selections
150 .disjoint_anchors()
151 .iter()
152 .chain(pending)
153 .map(move |s| s.start.to_display_point(display_map)..s.end.to_display_point(display_map))
154}
155
156pub fn deploy_context_menu(
157 editor: &mut Editor,
158 position: Option<Point<Pixels>>,
159 point: DisplayPoint,
160 window: &mut Window,
161 cx: &mut Context<Editor>,
162) {
163 if !editor.is_focused(window) {
164 window.focus(&editor.focus_handle(cx));
165 }
166
167 // Don't show context menu for inline editors
168 if !editor.mode().is_full() {
169 return;
170 }
171
172 let display_map = editor.display_snapshot(cx);
173 let source_anchor = display_map.display_point_to_anchor(point, text::Bias::Right);
174 let context_menu = if let Some(custom) = editor.custom_context_menu.take() {
175 let menu = custom(editor, point, window, cx);
176 editor.custom_context_menu = Some(custom);
177 let Some(menu) = menu else {
178 return;
179 };
180 menu
181 } else {
182 // Don't show the context menu if there isn't a project associated with this editor
183 let Some(project) = editor.project.clone() else {
184 return;
185 };
186
187 let snapshot = editor.snapshot(window, cx);
188 let display_map = editor.display_snapshot(cx);
189 let buffer = snapshot.buffer_snapshot();
190 let anchor = buffer.anchor_before(point.to_point(&display_map));
191 if !display_ranges(&display_map, &editor.selections).any(|r| r.contains(&point)) {
192 // Move the cursor to the clicked location so that dispatched actions make sense
193 editor.change_selections(SelectionEffects::no_scroll(), window, cx, |s| {
194 s.clear_disjoint();
195 s.set_pending_anchor_range(anchor..anchor, SelectMode::Character);
196 });
197 }
198
199 let focus = window.focused(cx);
200 let has_reveal_target = editor.target_file(cx).is_some();
201 let has_selections = editor
202 .selections
203 .all::<PointUtf16>(&display_map)
204 .into_iter()
205 .any(|s| !s.is_empty());
206 let has_git_repo = buffer
207 .buffer_id_for_anchor(anchor)
208 .is_some_and(|buffer_id| {
209 project
210 .read(cx)
211 .git_store()
212 .read(cx)
213 .repository_and_path_for_buffer_id(buffer_id, cx)
214 .is_some()
215 });
216
217 let evaluate_selection = window.is_action_available(&EvaluateSelectedText, cx);
218 let run_to_cursor = window.is_action_available(&RunToCursor, cx);
219 let disable_ai = DisableAiSettings::get_global(cx).disable_ai;
220
221 ui::ContextMenu::build(window, cx, |menu, _window, _cx| {
222 let builder = menu
223 .on_blur_subscription(Subscription::new(|| {}))
224 .when(run_to_cursor, |builder| {
225 builder.action("Run to Cursor", Box::new(RunToCursor))
226 })
227 .when(evaluate_selection && has_selections, |builder| {
228 builder.action("Evaluate Selection", Box::new(EvaluateSelectedText))
229 })
230 .when(
231 run_to_cursor || (evaluate_selection && has_selections),
232 |builder| builder.separator(),
233 )
234 .action("Go to Definition", Box::new(GoToDefinition))
235 .action("Go to Declaration", Box::new(GoToDeclaration))
236 .action("Go to Type Definition", Box::new(GoToTypeDefinition))
237 .action("Go to Implementation", Box::new(GoToImplementation))
238 .action(
239 "Find All References",
240 Box::new(FindAllReferences::default()),
241 )
242 .separator()
243 .action("Rename Symbol", Box::new(Rename))
244 .action("Format Buffer", Box::new(Format))
245 .when(has_selections, |cx| {
246 cx.action("Format Selections", Box::new(FormatSelections))
247 })
248 .action(
249 "Show Code Actions",
250 Box::new(ToggleCodeActions {
251 deployed_from: None,
252 quick_launch: false,
253 }),
254 )
255 .when(!disable_ai && has_selections, |this| {
256 this.action("Add to Agent Thread", Box::new(AddSelectionToThread))
257 })
258 .separator()
259 .action("Cut", Box::new(Cut))
260 .action("Copy", Box::new(Copy))
261 .action("Copy and Trim", Box::new(CopyAndTrim))
262 .action("Paste", Box::new(Paste))
263 .separator()
264 .action_disabled_when(
265 !has_reveal_target,
266 if cfg!(target_os = "macos") {
267 "Reveal in Finder"
268 } else {
269 "Reveal in File Manager"
270 },
271 Box::new(RevealInFileManager),
272 )
273 .action_disabled_when(
274 !has_reveal_target,
275 "Open in Terminal",
276 Box::new(OpenInTerminal),
277 )
278 .action_disabled_when(
279 !has_git_repo,
280 "Copy Permalink",
281 Box::new(CopyPermalinkToLine),
282 )
283 .action_disabled_when(
284 !has_git_repo,
285 "View File History",
286 Box::new(git::FileHistory),
287 );
288 match focus {
289 Some(focus) => builder.context(focus),
290 None => builder,
291 }
292 })
293 };
294
295 editor.mouse_context_menu = match position {
296 Some(position) => MouseContextMenu::pinned_to_editor(
297 editor,
298 source_anchor,
299 position,
300 context_menu,
301 window,
302 cx,
303 ),
304 None => {
305 let character_size = editor.character_dimensions(window);
306 let menu_position = MenuPosition::PinnedToEditor {
307 source: source_anchor,
308 offset: gpui::point(character_size.em_width, character_size.line_height),
309 };
310 Some(MouseContextMenu::new(
311 editor,
312 menu_position,
313 context_menu,
314 window,
315 cx,
316 ))
317 }
318 };
319 cx.notify();
320}
321
322#[cfg(test)]
323mod tests {
324 use super::*;
325 use crate::{editor_tests::init_test, test::editor_lsp_test_context::EditorLspTestContext};
326 use indoc::indoc;
327
328 #[gpui::test]
329 async fn test_mouse_context_menu(cx: &mut gpui::TestAppContext) {
330 init_test(cx, |_| {});
331
332 let mut cx = EditorLspTestContext::new_rust(
333 lsp::ServerCapabilities {
334 hover_provider: Some(lsp::HoverProviderCapability::Simple(true)),
335 ..Default::default()
336 },
337 cx,
338 )
339 .await;
340
341 cx.set_state(indoc! {"
342 fn teˇst() {
343 do_work();
344 }
345 "});
346 let point = cx.display_point(indoc! {"
347 fn test() {
348 do_wˇork();
349 }
350 "});
351 cx.editor(|editor, _window, _app| assert!(editor.mouse_context_menu.is_none()));
352
353 cx.update_editor(|editor, window, cx| {
354 deploy_context_menu(editor, Some(Default::default()), point, window, cx);
355
356 // Assert that, even after deploying the editor's mouse context
357 // menu, the editor's focus handle still contains the focused
358 // element. The pane's tab bar relies on this to determine whether
359 // to show the tab bar buttons and there was a small flicker when
360 // deploying the mouse context menu that would cause this to not be
361 // true, making it so that the buttons would disappear for a couple
362 // of frames.
363 assert!(editor.focus_handle.contains_focused(window, cx));
364 });
365
366 cx.assert_editor_state(indoc! {"
367 fn test() {
368 do_wˇork();
369 }
370 "});
371 cx.editor(|editor, _window, _app| assert!(editor.mouse_context_menu.is_some()));
372 }
373}