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)?;
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("Find All References", Box::new(FindAllReferences))
239 .separator()
240 .action("Rename Symbol", Box::new(Rename))
241 .action("Format Buffer", Box::new(Format))
242 .when(has_selections, |cx| {
243 cx.action("Format Selections", Box::new(FormatSelections))
244 })
245 .action(
246 "Show Code Actions",
247 Box::new(ToggleCodeActions {
248 deployed_from: None,
249 quick_launch: false,
250 }),
251 )
252 .when(!disable_ai && has_selections, |this| {
253 this.action("Add to Agent Thread", Box::new(AddSelectionToThread))
254 })
255 .separator()
256 .action("Cut", Box::new(Cut))
257 .action("Copy", Box::new(Copy))
258 .action("Copy and Trim", Box::new(CopyAndTrim))
259 .action("Paste", Box::new(Paste))
260 .separator()
261 .action_disabled_when(
262 !has_reveal_target,
263 if cfg!(target_os = "macos") {
264 "Reveal in Finder"
265 } else {
266 "Reveal in File Manager"
267 },
268 Box::new(RevealInFileManager),
269 )
270 .action_disabled_when(
271 !has_reveal_target,
272 "Open in Terminal",
273 Box::new(OpenInTerminal),
274 )
275 .action_disabled_when(
276 !has_git_repo,
277 "Copy Permalink",
278 Box::new(CopyPermalinkToLine),
279 );
280 match focus {
281 Some(focus) => builder.context(focus),
282 None => builder,
283 }
284 })
285 };
286
287 editor.mouse_context_menu = match position {
288 Some(position) => MouseContextMenu::pinned_to_editor(
289 editor,
290 source_anchor,
291 position,
292 context_menu,
293 window,
294 cx,
295 ),
296 None => {
297 let character_size = editor.character_dimensions(window);
298 let menu_position = MenuPosition::PinnedToEditor {
299 source: source_anchor,
300 offset: gpui::point(character_size.em_width, character_size.line_height),
301 };
302 Some(MouseContextMenu::new(
303 editor,
304 menu_position,
305 context_menu,
306 window,
307 cx,
308 ))
309 }
310 };
311 cx.notify();
312}
313
314#[cfg(test)]
315mod tests {
316 use super::*;
317 use crate::{editor_tests::init_test, test::editor_lsp_test_context::EditorLspTestContext};
318 use indoc::indoc;
319
320 #[gpui::test]
321 async fn test_mouse_context_menu(cx: &mut gpui::TestAppContext) {
322 init_test(cx, |_| {});
323
324 let mut cx = EditorLspTestContext::new_rust(
325 lsp::ServerCapabilities {
326 hover_provider: Some(lsp::HoverProviderCapability::Simple(true)),
327 ..Default::default()
328 },
329 cx,
330 )
331 .await;
332
333 cx.set_state(indoc! {"
334 fn teˇst() {
335 do_work();
336 }
337 "});
338 let point = cx.display_point(indoc! {"
339 fn test() {
340 do_wˇork();
341 }
342 "});
343 cx.editor(|editor, _window, _app| assert!(editor.mouse_context_menu.is_none()));
344
345 cx.update_editor(|editor, window, cx| {
346 deploy_context_menu(editor, Some(Default::default()), point, window, cx);
347
348 // Assert that, even after deploying the editor's mouse context
349 // menu, the editor's focus handle still contains the focused
350 // element. The pane's tab bar relies on this to determine whether
351 // to show the tab bar buttons and there was a small flicker when
352 // deploying the mouse context menu that would cause this to not be
353 // true, making it so that the buttons would disappear for a couple
354 // of frames.
355 assert!(editor.focus_handle.contains_focused(window, cx));
356 });
357
358 cx.assert_editor_state(indoc! {"
359 fn test() {
360 do_wˇork();
361 }
362 "});
363 cx.editor(|editor, _window, _app| assert!(editor.mouse_context_menu.is_some()));
364 }
365}