mouse_context_menu.rs

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