Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

bug: Extend LSP go-to-definition fallback to context menu and cmd+click (#10102)

Shahmir Varqha4d91af013d76
diff --git a/frontend/src/components/editor/cell/cell-context-menu.tsx b/frontend/src/components/editor/cell/cell-context-menu.tsx
index cbbd04e1c1..8bd44fd6b1 100644
--- a/frontend/src/components/editor/cell/cell-context-menu.tsx
+++ b/frontend/src/components/editor/cell/cell-context-menu.tsx
@@ -23,7 +23,7 @@ import { toast } from "@/components/ui/use-toast";
import { useCellData, useCellRuntime } from "@/core/cells/cells";
import { CellOutputId } from "@/core/cells/ids";
import { isOutputEmpty } from "@/core/cells/outputs";
-import { goToDefinitionAtCursorPosition } from "@/core/codemirror/go-to-definition/utils";
+import { goToDefinitionWithLspFallback } from "@/core/codemirror/go-to-definition/utils";
import { sendToPanelManager } from "@/core/vscode/vscode-bindings";
import { copyImageToClipboard, copyToClipboard } from "@/utils/copy";
import { getImageExtension } from "@/utils/filenames";
@@ -170,7 +170,7 @@ export const CellActionsContextMenu = ({
// Only suppress focus restoration when we actually navigated;
// otherwise let Radix return focus to the trigger cell.
suppressCloseAutoFocus.current =
- goToDefinitionAtCursorPosition(editorView);
+ goToDefinitionWithLspFallback(editorView);
}
},
},
diff --git a/frontend/src/core/codemirror/go-to-definition/__tests__/utils.test.ts b/frontend/src/core/codemirror/go-to-definition/__tests__/utils.test.ts
index 02b5501558..38f3ecbeab 100644
--- a/frontend/src/core/codemirror/go-to-definition/__tests__/utils.test.ts
+++ b/frontend/src/core/codemirror/go-to-definition/__tests__/utils.test.ts
@@ -2,13 +2,17 @@
import { python } from "@codemirror/lang-python";
import { EditorState } from "@codemirror/state";
-import { EditorView } from "@codemirror/view";
-import { afterEach, describe, expect, test } from "vitest";
+import { EditorView, keymap } from "@codemirror/view";
+import { afterEach, describe, expect, test, vi } from "vitest";
import { cellId, variableName } from "@/__tests__/branded";
import { initialNotebookState, notebookAtom } from "@/core/cells/cells";
import { store } from "@/core/state/jotai";
import { variablesAtom } from "@/core/variables/state";
-import { goToDefinitionAtCursorPosition } from "../utils";
+import {
+ goToDefinitionAtCursorPosition,
+ goToDefinitionWithLspFallback,
+ requestLspGoToDefinition,
+} from "../utils";
async function tick(): Promise<void> {
await new Promise((resolve) => requestAnimationFrame(resolve));
@@ -181,3 +185,71 @@ print(mymodule)`;
);
});
});
+
+describe("goToDefinitionWithLspFallback", () => {
+ test("falls through to LSP when marimo cannot resolve the symbol", () => {
+ const lspGoToDefinition = vi.fn(() => true);
+ const view = new EditorView({
+ state: EditorState.create({
+ doc: "parser.add_argument('--foo')",
+ selection: { anchor: "parser.add_argument".indexOf("add_argument") },
+ extensions: [
+ python(),
+ keymap.of([{ key: "F12", run: lspGoToDefinition }]),
+ ],
+ }),
+ parent: document.body,
+ });
+ views.push(view);
+
+ const result = goToDefinitionWithLspFallback(view);
+
+ expect(result).toBe(true);
+ expect(lspGoToDefinition).toHaveBeenCalledOnce();
+ });
+
+ test("does not invoke LSP when marimo resolves the symbol", async () => {
+ const lspGoToDefinition = vi.fn(() => true);
+ const code = "a = 10\nprint(a)";
+ const view = new EditorView({
+ state: EditorState.create({
+ doc: code,
+ selection: { anchor: code.indexOf("a", 3) },
+ extensions: [
+ python(),
+ keymap.of([{ key: "F12", run: lspGoToDefinition }]),
+ ],
+ }),
+ parent: document.body,
+ });
+ views.push(view);
+
+ const result = goToDefinitionWithLspFallback(view);
+
+ expect(result).toBe(true);
+ expect(lspGoToDefinition).not.toHaveBeenCalled();
+ await tick();
+ expect(view.state.selection.main.head).toBe(0);
+ });
+
+ test("falls through with a modified shortcut like Ctrl-F12", () => {
+ const lspGoToDefinition = vi.fn(() => true);
+ const view = new EditorView({
+ state: EditorState.create({
+ doc: "parser.add_argument('--foo')",
+ selection: { anchor: "parser.add_argument".indexOf("add_argument") },
+ extensions: [
+ python(),
+ keymap.of([{ key: "Ctrl-F12", run: lspGoToDefinition }]),
+ ],
+ }),
+ parent: document.body,
+ });
+ views.push(view);
+
+ const result = requestLspGoToDefinition(view, "Ctrl-F12");
+
+ expect(result).toBe(true);
+ expect(lspGoToDefinition).toHaveBeenCalledOnce();
+ });
+});
diff --git a/frontend/src/core/codemirror/go-to-definition/extension.ts b/frontend/src/core/codemirror/go-to-definition/extension.ts
index 78988a752b..5486ab8263 100644
--- a/frontend/src/core/codemirror/go-to-definition/extension.ts
+++ b/frontend/src/core/codemirror/go-to-definition/extension.ts
@@ -1,7 +1,7 @@
/* Copyright 2026 Marimo. All rights reserved. */
import { EditorView } from "@codemirror/view";
import { createUnderlinePlugin, underlineField } from "./underline";
-import { goToDefinition } from "./utils";
+import { goToDefinitionWithLspFallback } from "./utils";
/**
* Create a go-to-definition extension.
@@ -9,8 +9,8 @@ import { goToDefinition } from "./utils";
export function goToDefinitionBundle() {
return [
underlineField,
- createUnderlinePlugin((view, variableName) => {
- goToDefinition(view, variableName);
+ createUnderlinePlugin((view) => {
+ goToDefinitionWithLspFallback(view);
}),
EditorView.baseTheme({
".underline": {
diff --git a/frontend/src/core/codemirror/go-to-definition/underline.ts b/frontend/src/core/codemirror/go-to-definition/underline.ts
index 39929ae7ce..f1b746810c 100644
--- a/frontend/src/core/codemirror/go-to-definition/underline.ts
+++ b/frontend/src/core/codemirror/go-to-definition/underline.ts
@@ -55,12 +55,9 @@ class MetaUnderlineVariablePlugin {
private view: EditorView;
private commandClickMode: boolean;
private hoveredRange: { from: number; to: number; position: number } | null;
- private onClick: (view: EditorView, variableName: string) => void;
+ private onClick: (view: EditorView) => void;
- constructor(
- view: EditorView,
- onClick: (view: EditorView, variableName: string) => void,
- ) {
+ constructor(view: EditorView, onClick: (view: EditorView) => void) {
this.view = view;
this.commandClickMode = false;
this.hoveredRange = null;
@@ -191,20 +188,16 @@ class MetaUnderlineVariablePlugin {
// If we have a hovered range, go to it
private click = (event: MouseEvent) => {
if (this.hoveredRange) {
- const variableName = this.view.state.doc.sliceString(
- this.hoveredRange.from,
- this.hoveredRange.to,
- );
event.preventDefault();
event.stopPropagation();
- this.onClick(this.view, variableName);
- // Move the cursor to the clicked position
+ // Move the cursor before resolving so LSP uses the clicked position.
this.view.dispatch({
selection: {
head: this.hoveredRange.position,
anchor: this.hoveredRange.position,
},
});
+ this.onClick(this.view);
}
};
@@ -217,7 +210,5 @@ class MetaUnderlineVariablePlugin {
}
}
-export const createUnderlinePlugin = (
- onClick: (view: EditorView, variableName: string) => void,
-) =>
+export const createUnderlinePlugin = (onClick: (view: EditorView) => void) =>
ViewPlugin.define((view) => new MetaUnderlineVariablePlugin(view, onClick));
diff --git a/frontend/src/core/codemirror/go-to-definition/utils.ts b/frontend/src/core/codemirror/go-to-definition/utils.ts
index 8eb09b7a41..cd5cd05c83 100644
--- a/frontend/src/core/codemirror/go-to-definition/utils.ts
+++ b/frontend/src/core/codemirror/go-to-definition/utils.ts
@@ -2,8 +2,14 @@
import { closeCompletion } from "@codemirror/autocomplete";
import type { EditorState } from "@codemirror/state";
-import { closeHoverTooltips, type EditorView } from "@codemirror/view";
+import {
+ closeHoverTooltips,
+ type EditorView,
+ type KeyBinding,
+ keymap,
+} from "@codemirror/view";
import type { CellId } from "@/core/cells/ids";
+import { hotkeysAtom, platformAtom } from "../../config/config";
import { notebookAtom } from "../../cells/cells";
import { store } from "../../state/jotai";
import { variablesAtom } from "../../variables/state";
@@ -11,6 +17,20 @@ import type { VariableName, Variables } from "../../variables/types";
import { getPositionAtWordBounds } from "../completion/hints";
import { goToLine, goToVariableDefinition } from "./commands";
+function keymapBindingMatchesHotkey(
+ binding: KeyBinding,
+ hotkey: string,
+): boolean {
+ const platform = store.get(platformAtom);
+ const bindingKey =
+ platform === "mac"
+ ? (binding.mac ?? binding.key)
+ : platform === "windows"
+ ? (binding.win ?? binding.key)
+ : (binding.linux ?? binding.key);
+ return bindingKey === hotkey;
+}
+
/**
* Get the word under the cursor.
*/
@@ -68,6 +88,34 @@ export function goToDefinitionAtCursorPosition(view: EditorView): boolean {
return goToDefinition(view, word, position);
}
+/**
+ * Invoke the editor's go-to-definition keymap handlers for the configured
+ * hotkey. Matches CodeMirror key strings directly (including customized
+ * shortcuts like `Ctrl-F12`) instead of synthesizing keyboard events.
+ */
+export function requestLspGoToDefinition(
+ view: EditorView,
+ hotkey = store.get(hotkeysAtom).getHotkey("cell.goToDefinition").key,
+): boolean {
+ for (const binding of view.state.facet(keymap).flat()) {
+ if (keymapBindingMatchesHotkey(binding, hotkey) && binding.run?.(view)) {
+ return true;
+ }
+ }
+ return false;
+}
+
+/**
+ * Go to the definition under the cursor, falling back to the language server
+ * when marimo cannot resolve the symbol (e.g. external library code).
+ */
+export function goToDefinitionWithLspFallback(view: EditorView): boolean {
+ if (goToDefinitionAtCursorPosition(view)) {
+ return true;
+ }
+ return requestLspGoToDefinition(view);
+}
+
/**
* Go to the definition of the variable under the cursor.
* @param view The editor view at which the command was invoked.