mouse_context_menu.rs

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