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