1use context_menu::ContextMenuItem;
2use gpui::{geometry::vector::Vector2F, impl_internal_actions, MutableAppContext, ViewContext};
3
4use crate::{
5 DisplayPoint, Editor, EditorMode, Event, FindAllReferences, GoToDefinition, GoToTypeDefinition,
6 Rename, SelectMode, ToggleCodeActions,
7};
8
9#[derive(Clone, PartialEq)]
10pub struct DeployMouseContextMenu {
11 pub position: Vector2F,
12 pub point: DisplayPoint,
13}
14
15impl_internal_actions!(editor, [DeployMouseContextMenu]);
16
17pub fn init(cx: &mut MutableAppContext) {
18 cx.add_action(deploy_context_menu);
19}
20
21pub fn deploy_context_menu(
22 editor: &mut Editor,
23 &DeployMouseContextMenu { position, point }: &DeployMouseContextMenu,
24 cx: &mut ViewContext<Editor>,
25) {
26 if !editor.focused {
27 cx.focus_self();
28 cx.emit(Event::Activate);
29 }
30
31 // Don't show context menu for inline editors
32 if editor.mode() != EditorMode::Full {
33 return;
34 }
35
36 // Don't show the context menu if there isn't a project associated with this editor
37 if editor.project.is_none() {
38 return;
39 }
40
41 // Move the cursor to the clicked location so that dispatched actions make sense
42 editor.change_selections(None, cx, |s| {
43 s.clear_disjoint();
44 s.set_pending_display_range(point..point, SelectMode::Character);
45 });
46
47 editor.mouse_context_menu.update(cx, |menu, cx| {
48 menu.show(
49 position,
50 vec![
51 ContextMenuItem::item("Rename Symbol", None, Rename),
52 ContextMenuItem::item("Go To Definition", None, GoToDefinition),
53 ContextMenuItem::item("Go To Type Definition", None, GoToTypeDefinition),
54 ContextMenuItem::item("Find All References", None, FindAllReferences),
55 ContextMenuItem::item(
56 "Code Actions",
57 None,
58 ToggleCodeActions {
59 deployed_from_indicator: false,
60 },
61 ),
62 ],
63 cx,
64 );
65 });
66 cx.notify();
67}
68
69#[cfg(test)]
70mod tests {
71 use indoc::indoc;
72
73 use crate::test::EditorLspTestContext;
74
75 use super::*;
76
77 #[gpui::test]
78 async fn test_mouse_context_menu(cx: &mut gpui::TestAppContext) {
79 let mut cx = EditorLspTestContext::new_rust(
80 lsp::ServerCapabilities {
81 hover_provider: Some(lsp::HoverProviderCapability::Simple(true)),
82 ..Default::default()
83 },
84 cx,
85 )
86 .await;
87
88 cx.set_state(indoc! {"
89 fn te|st()
90 do_work();"});
91 let point = cx.display_point(indoc! {"
92 fn test()
93 do_w|ork();"});
94 cx.update_editor(|editor, cx| {
95 deploy_context_menu(
96 editor,
97 &DeployMouseContextMenu {
98 position: Default::default(),
99 point,
100 },
101 cx,
102 )
103 });
104
105 cx.assert_editor_state(indoc! {"
106 fn test()
107 do_w|ork();"});
108 cx.editor(|editor, app| assert!(editor.mouse_context_menu.read(app).visible()));
109 }
110}