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        let focus = cx.focused();
 74        ui::ContextMenu::build(cx, |menu, _cx| {
 75            let builder = menu
 76                .action("Rename Symbol", Box::new(Rename))
 77                .action("Go to Definition", Box::new(GoToDefinition))
 78                .action("Go to Type Definition", Box::new(GoToTypeDefinition))
 79                .action("Go to Implementation", Box::new(GoToImplementation))
 80                .action("Find All References", Box::new(FindAllReferences))
 81                .action(
 82                    "Code Actions",
 83                    Box::new(ToggleCodeActions {
 84                        deployed_from_indicator: None,
 85                    }),
 86                )
 87                .separator()
 88                .action("Reveal in Finder", Box::new(RevealInFinder))
 89                .action("Open in Terminal", Box::new(OpenInTerminal));
 90            match focus {
 91                Some(focus) => builder.context(focus),
 92                None => builder,
 93            }
 94        })
 95    };
 96    let mouse_context_menu = MouseContextMenu::new(position, context_menu, cx);
 97    editor.mouse_context_menu = Some(mouse_context_menu);
 98    cx.notify();
 99}
100
101#[cfg(test)]
102mod tests {
103    use super::*;
104    use crate::{editor_tests::init_test, test::editor_lsp_test_context::EditorLspTestContext};
105    use indoc::indoc;
106
107    #[gpui::test]
108    async fn test_mouse_context_menu(cx: &mut gpui::TestAppContext) {
109        init_test(cx, |_| {});
110
111        let mut cx = EditorLspTestContext::new_rust(
112            lsp::ServerCapabilities {
113                hover_provider: Some(lsp::HoverProviderCapability::Simple(true)),
114                ..Default::default()
115            },
116            cx,
117        )
118        .await;
119
120        cx.set_state(indoc! {"
121            fn teˇst() {
122                do_work();
123            }
124        "});
125        let point = cx.display_point(indoc! {"
126            fn test() {
127                do_wˇork();
128            }
129        "});
130        cx.editor(|editor, _app| assert!(editor.mouse_context_menu.is_none()));
131        cx.update_editor(|editor, cx| deploy_context_menu(editor, Default::default(), point, cx));
132
133        cx.assert_editor_state(indoc! {"
134            fn test() {
135                do_wˇork();
136            }
137        "});
138        cx.editor(|editor, _app| assert!(editor.mouse_context_menu.is_some()));
139    }
140}