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", Rename),
52 ContextMenuItem::item("Go To Definition", GoToDefinition),
53 ContextMenuItem::item("Go To Type Definition", GoToTypeDefinition),
54 ContextMenuItem::item("Find All References", FindAllReferences),
55 ContextMenuItem::item(
56 "Code Actions",
57 ToggleCodeActions {
58 deployed_from_indicator: false,
59 },
60 ),
61 ],
62 cx,
63 );
64 });
65 cx.notify();
66}
67
68#[cfg(test)]
69mod tests {
70 use indoc::indoc;
71
72 use crate::test::EditorLspTestContext;
73
74 use super::*;
75
76 #[gpui::test]
77 async fn test_mouse_context_menu(cx: &mut gpui::TestAppContext) {
78 let mut cx = EditorLspTestContext::new_rust(
79 lsp::ServerCapabilities {
80 hover_provider: Some(lsp::HoverProviderCapability::Simple(true)),
81 ..Default::default()
82 },
83 cx,
84 )
85 .await;
86
87 cx.set_state(indoc! {"
88 fn te|st()
89 do_work();"});
90 let point = cx.display_point(indoc! {"
91 fn test()
92 do_w|ork();"});
93 cx.update_editor(|editor, cx| {
94 deploy_context_menu(
95 editor,
96 &DeployMouseContextMenu {
97 position: Default::default(),
98 point,
99 },
100 cx,
101 )
102 });
103
104 cx.assert_editor_state(indoc! {"
105 fn test()
106 do_w|ork();"});
107 cx.editor(|editor, app| assert!(editor.mouse_context_menu.read(app).visible()));
108 }
109}