fix(editor): show navigation link for bound click modifiers on hover (#7446)

Cmd+Alt-click (Ctrl+Alt elsewhere) is bound to Go to Implementation in the IntelliJ keymap, but the hover affordance only recognised Cmd/Ctrl alone, so the symbol kept plain text and the I-beam cursor. The navigation modifier tracker now also holds for any modifiers the active keymap binds to a navigation left-click (declaration, implementation, type declaration, super method); the LSP link hover and the plain hover popups follow it, and rebinding or removing the mouse binding removes the affordance.

Co-authored-by: happut <[email protected]>
Цей коміт міститься в:
wangfeiтаhapput authored and GitHub committed 2026-12-02 17:18:31 +08:00
1 джерело e7da5e0937
коміт 607e4e095d
4 files changed
+74 -11

No files matched your search

+19 -1
Переглянути файл
@@ -4,6 +4,7 @@ import { EditorState } from "@codemirror/state";
import { EditorView, activateHover, hasHoverTooltips } from "@codemirror/view";
import { CONTRIBUTED_HOVER, type ContributedHover } from "@/editor/api";
import { usePlatform } from "@/platform/platformInfo";
import { useKeymap, defaultPreset } from "@/platform/keymap";
import {
isNavigationGesture,
navigationModifierHeld,
@@ -44,6 +45,7 @@ describe.each([
view = null;
document.body.replaceChildren();
resetNavigationModifier();
useKeymap.setState({ preset: defaultPreset(), overrides: {} });
});
it("tracks the platform's modifier alone, pressed and released", () => {
@@ -52,7 +54,7 @@ describe.each([
expect(navigationModifierHeld()).toBe(true);
key("keyup", mod);
expect(navigationModifierHeld()).toBe(false);
// With ⇧/⌥ it is another gesture; the other platform's Ctrl/⌘ is not it.
// With ⇧ it is another gesture; the other platform's Ctrl/⌘ is not it.
key("keydown", "Shift", { [flag]: true, shiftKey: true });
expect(navigationModifierHeld()).toBe(false);
key("keydown", mod === "Meta" ? "Control" : "Meta", { [other]: true });
@@ -62,6 +64,22 @@ describe.each([
expect(isNavigationGesture(new MouseEvent("mousemove", { [flag]: true }))).toBe(true);
});
it("tracks a bound navigation click's modifiers (⌘⌥ / Ctrl+Alt), as the hover affordance", () => {
useKeymap.setState({ preset: os === "macos" ? "intellij-macos" : "intellij-windows", overrides: {} });
expect(navigationModifierHeld()).toBe(false);
key("keydown", mod, { [flag]: true });
key("keydown", "Alt", { [flag]: true, altKey: true });
expect(navigationModifierHeld()).toBe(true);
key("keyup", "Alt", { [flag]: true });
expect(navigationModifierHeld()).toBe(true);
key("keyup", mod);
expect(navigationModifierHeld()).toBe(false);
// Without the mouse binding the gesture clicks nowhere, so it advertises nothing.
useKeymap.setState({ overrides: { "navigate.implementation": [] } });
key("keydown", mod, { [flag]: true, altKey: true });
expect(navigationModifierHeld()).toBe(false);
});
it("learns the state from mouse moves and forgets it when the window loses focus", () => {
expect(navigationModifierHeld()).toBe(false);
window.dispatchEvent(new MouseEvent("mousemove", { [flag]: true }));
+7 -2
Переглянути файл
@@ -96,7 +96,12 @@ import { editorPrefs } from "@/platform/editorSettings";
import { isVirtualUri, virtualContainer } from "@/platform/virtualDocs";
import { isAddCaretGesture, isRectangularSelectionGesture } from "@/editor/core/cm/multiCaret";
import { dispatchMouseShortcut, shortcutLabel } from "@/platform/keymap";
import { isNavigationGesture, onNavigationModifier, navigationModifierHeld } from "@/platform/navigationModifier";
import {
isNavigationGesture,
isNavigationHoverGesture,
onNavigationModifier,
navigationModifierHeld,
} from "@/platform/navigationModifier";
import { create } from "zustand";
import { tx } from "@/platform/i18n";
import { cssVariableAt, STYLESHEETS } from "./cssVariables";
@@ -1371,7 +1376,7 @@ const linkPlugin = ViewPlugin.fromClass(
// Not through a popup or an inlay hint (which navigates on its own ⌘-click) to the text under it.
const over = e.target instanceof Element ? e.target : null;
this.last = over?.closest(".cm-tooltip, .cm-lsp-inlay") ? null : { x: e.clientX, y: e.clientY };
this.show(isNavigationGesture(e));
this.show(isNavigationHoverGesture(e));
},
mouseleave() {
this.last = null;
+12
Переглянути файл
@@ -123,6 +123,18 @@ describe("mouse navigation", () => {
expect(event.defaultPrevented).toBe(true);
});
it("underlines the symbol for the Option+Command hover, like the click it advertises", async () => {
view.contentDOM.dispatchEvent(new MouseEvent("mousemove", { bubbles: true, metaKey: true, altKey: true }));
await vi.waitFor(() => expect(view.dom.querySelector(".cm-lsp-link")).not.toBeNull());
});
it("keeps the text cursor for Option+Command hover when the mouse binding is removed", async () => {
useKeymap.setState({ overrides: { "navigate.implementation": ["Meta+Alt+B"] } });
view.contentDOM.dispatchEvent(new MouseEvent("mousemove", { bubbles: true, metaKey: true, altKey: true }));
await new Promise((resolve) => setTimeout(resolve, 20));
expect(view.dom.querySelector(".cm-lsp-link")).toBeNull();
});
it.each([
{ os: "macos", mods: { metaKey: true } },
{ os: "windows", mods: { ctrlKey: true } },
+36 -8
Переглянути файл
@@ -1,10 +1,11 @@
/**
* The navigation modifier (⌘ on macOS, Ctrl elsewhere) as a live, window-wide
* state: ⌘/Ctrl-hover shows the navigation link and its brief info, and every
* plain hover popup (quick documentation, problems, debugger values) yields to
* it. Tracked on the window so an editor sees the key even when it isn't focused.
* The navigation hover gesture (⌘/Ctrl, or the modifiers of a bound navigation
* click such as ⌘⌥/Ctrl+Alt for implementations) as a live, window-wide state:
* it shows the navigation link and its brief info, and every plain hover popup
* (quick documentation, problems, debugger values) yields to it. Tracked on the
* window so an editor sees the key even when it isn't focused.
*/
import { hasPrimaryModifier, isMacHost } from "./keymap";
import { bindingsFor, hasPrimaryModifier, isMacHost, mouseStrokeOf, normalizeBinding } from "./keymap";
import { notifyEach } from "./listeners";
type ModifierEvent = Pick<MouseEvent, "metaKey" | "ctrlKey" | "altKey" | "shiftKey">;
@@ -14,6 +15,33 @@ export function isNavigationGesture(e: ModifierEvent): boolean {
return hasPrimaryModifier(e) && !e.altKey && !e.shiftKey && !(isMacHost() ? e.ctrlKey : e.metaKey);
}
/** Left-click actions that navigate: their modifiers carry the link affordance too. */
const NAVIGATION_MOUSE_ACTIONS = [
"navigate.declaration",
"navigate.implementation",
"navigate.typeDeclaration",
"navigate.superMethod",
];
function isBoundNavigationClick(e: ModifierEvent): boolean {
const stroke = mouseStrokeOf({
button: 0,
metaKey: e.metaKey,
ctrlKey: e.ctrlKey,
altKey: e.altKey,
shiftKey: e.shiftKey,
});
return (
stroke !== null &&
NAVIGATION_MOUSE_ACTIONS.some((id) => bindingsFor(id).some((binding) => normalizeBinding(binding) === stroke))
);
}
/** The declaration gesture, or the modifiers of a navigation click the keymap binds (e.g. ⌘⌥). */
export function isNavigationHoverGesture(e: ModifierEvent): boolean {
return isNavigationGesture(e) || isBoundNavigationClick(e);
}
let held = false;
let pointer: Element | null = null;
const listeners = new Set<(held: boolean) => void>();
@@ -27,11 +55,11 @@ function setHeld(value: boolean) {
function install() {
if (installed || typeof window === "undefined") return;
const onKey = (e: KeyboardEvent) => setHeld(isNavigationGesture(e));
const onKey = (e: KeyboardEvent) => setHeld(isNavigationHoverGesture(e));
// Also from pointer events: a key released outside the window never reports its keyup.
const onMouse = (e: MouseEvent) => {
pointer = e.target instanceof Element ? e.target : null;
setHeld(isNavigationGesture(e));
setHeld(isNavigationHoverGesture(e));
};
const onBlur = () => setHeld(false);
const opts = { capture: true, passive: true } as const;
@@ -50,7 +78,7 @@ function install() {
};
}
/** Whether ⌘ (macOS) / Ctrl is held on its own right now. */
/** Whether a navigation hover gesture (⌘/Ctrl, or a bound navigation click's modifiers) is held. */
export function navigationModifierHeld(): boolean {
install();
return held;