1use crate::{
2 Copy, CopyPermalinkToLine, Cut, DisplayPoint, Editor, EditorMode, FindAllReferences,
3 GoToDefinition, GoToImplementation, GoToTypeDefinition, Paste, Rename, RevealInFinder,
4 SelectMode, ToggleCodeActions,
5};
6use gpui::{DismissEvent, Pixels, Point, Subscription, View, ViewContext};
7use workspace::OpenInTerminal;
8
9pub struct MouseContextMenu {
10 pub(crate) position: Point<Pixels>,
11 pub(crate) context_menu: View<ui::ContextMenu>,
12 _subscription: Subscription,
13}
14
15impl MouseContextMenu {
16 pub(crate) fn new(
17 position: Point<Pixels>,
18 context_menu: View<ui::ContextMenu>,
19 cx: &mut ViewContext<Editor>,
20 ) -> Self {
21 let context_menu_focus = context_menu.focus_handle(cx);
22 cx.focus(&context_menu_focus);
23
24 let _subscription =
25 cx.subscribe(&context_menu, move |this, _, _event: &DismissEvent, cx| {
26 this.mouse_context_menu.take();
27 if context_menu_focus.contains_focused(cx) {
28 this.focus(cx);
29 }
30 });
31
32 Self {
33 position,
34 context_menu,
35 _subscription,
36 }
37 }
38}
39
40pub fn deploy_context_menu(
41 editor: &mut Editor,
42 position: Point<Pixels>,
43 point: DisplayPoint,
44 cx: &mut ViewContext<Editor>,
45) {
46 if !editor.is_focused(cx) {
47 editor.focus(cx);
48 }
49
50 // Don't show context menu for inline editors
51 if editor.mode() != EditorMode::Full {
52 return;
53 }
54
55 let context_menu = if let Some(custom) = editor.custom_context_menu.take() {
56 let menu = custom(editor, point, cx);
57 editor.custom_context_menu = Some(custom);
58 if menu.is_none() {
59 return;
60 }
61 menu.unwrap()
62 } else {
63 // Don't show the context menu if there isn't a project associated with this editor
64 if editor.project.is_none() {
65 return;
66 }
67
68 // Move the cursor to the clicked location so that dispatched actions make sense
69 editor.change_selections(None, cx, |s| {
70 s.clear_disjoint();
71 s.set_pending_display_range(point..point, SelectMode::Character);
72 });
73
74 let focus = cx.focused();
75 ui::ContextMenu::build(cx, |menu, _cx| {
76 let builder = menu
77 .action("Rename Symbol", Box::new(Rename))
78 .action("Go to Definition", Box::new(GoToDefinition))
79 .action("Go to Type Definition", Box::new(GoToTypeDefinition))
80 .action("Go to Implementation", Box::new(GoToImplementation))
81 .action("Find All References", Box::new(FindAllReferences))
82 .action(
83 "Code Actions",
84 Box::new(ToggleCodeActions {
85 deployed_from_indicator: None,
86 }),
87 )
88 .separator()
89 .action("Cut", Box::new(Cut))
90 .action("Copy", Box::new(Copy))
91 .action("Paste", Box::new(Paste))
92 .separator()
93 .action("Reveal in Finder", Box::new(RevealInFinder))
94 .action("Open in Terminal", Box::new(OpenInTerminal))
95 .action("Copy Permalink", Box::new(CopyPermalinkToLine));
96 match focus {
97 Some(focus) => builder.context(focus),
98 None => builder,
99 }
100 })
101 };
102 let mouse_context_menu = MouseContextMenu::new(position, context_menu, cx);
103 editor.mouse_context_menu = Some(mouse_context_menu);
104 cx.notify();
105}
106
107#[cfg(test)]
108mod tests {
109 use super::*;
110 use crate::{editor_tests::init_test, test::editor_lsp_test_context::EditorLspTestContext};
111 use indoc::indoc;
112
113 #[gpui::test]
114 async fn test_mouse_context_menu(cx: &mut gpui::TestAppContext) {
115 init_test(cx, |_| {});
116
117 let mut cx = EditorLspTestContext::new_rust(
118 lsp::ServerCapabilities {
119 hover_provider: Some(lsp::HoverProviderCapability::Simple(true)),
120 ..Default::default()
121 },
122 cx,
123 )
124 .await;
125
126 cx.set_state(indoc! {"
127 fn teˇst() {
128 do_work();
129 }
130 "});
131 let point = cx.display_point(indoc! {"
132 fn test() {
133 do_wˇork();
134 }
135 "});
136 cx.editor(|editor, _app| assert!(editor.mouse_context_menu.is_none()));
137 cx.update_editor(|editor, cx| deploy_context_menu(editor, Default::default(), point, cx));
138
139 cx.assert_editor_state(indoc! {"
140 fn test() {
141 do_wˇork();
142 }
143 "});
144 cx.editor(|editor, _app| assert!(editor.mouse_context_menu.is_some()));
145 }
146}