mouse_context_menu.rs

  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 super::*;
 71    use crate::test::EditorLspTestContext;
 72    use indoc::indoc;
 73
 74    #[gpui::test]
 75    async fn test_mouse_context_menu(cx: &mut gpui::TestAppContext) {
 76        let mut cx = EditorLspTestContext::new_rust(
 77            lsp::ServerCapabilities {
 78                hover_provider: Some(lsp::HoverProviderCapability::Simple(true)),
 79                ..Default::default()
 80            },
 81            cx,
 82        )
 83        .await;
 84
 85        cx.set_state(indoc! {"
 86            fn teˇst() {
 87                do_work();
 88            }
 89        "});
 90        let point = cx.display_point(indoc! {"
 91            fn test() {
 92                do_wˇork();
 93            }
 94        "});
 95        cx.update_editor(|editor, cx| {
 96            deploy_context_menu(
 97                editor,
 98                &DeployMouseContextMenu {
 99                    position: Default::default(),
100                    point,
101                },
102                cx,
103            )
104        });
105
106        cx.assert_editor_state(indoc! {"
107            fn test() {
108                do_wˇork();
109            }
110        "});
111        cx.editor(|editor, app| assert!(editor.mouse_context_menu.read(app).visible()));
112    }
113}