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.

Go to definition functionality added to hotkey and context menu (#1411)

Robert McKnightd2a253f33a95
diff --git a/frontend/src/components/editor/cell/cell-context-menu.tsx b/frontend/src/components/editor/cell/cell-context-menu.tsx
index f2a97d3b15..48063c18d1 100644
--- a/frontend/src/components/editor/cell/cell-context-menu.tsx
+++ b/frontend/src/components/editor/cell/cell-context-menu.tsx
@@ -18,7 +18,9 @@ import {
CopyIcon,
ImageIcon,
ScissorsIcon,
+ SearchIcon,
} from "lucide-react";
+import { goToDefinition } from "@/core/codemirror/go-to-definition";
interface Props extends CellActionButtonProps {
children: React.ReactNode;
@@ -81,6 +83,17 @@ export const CellActionsContextMenu = ({ children, ...props }: Props) => {
}
},
},
+ {
+ label: "Go to Definition",
+ icon: <SearchIcon size={13} strokeWidth={1.5} />,
+ handle: () => {
+ const { getEditorView } = props;
+ const editorView = getEditorView();
+ if (editorView) {
+ goToDefinition(editorView);
+ }
+ },
+ },
];
return (
diff --git a/frontend/src/core/cells/cells.ts b/frontend/src/core/cells/cells.ts
index 64731e8d7b..da947abc45 100644
--- a/frontend/src/core/cells/cells.ts
+++ b/frontend/src/core/cells/cells.ts
@@ -261,6 +261,7 @@ const {
cell: state.cellHandles[focusCellId],
config: state.cellData[focusCellId].config,
codeFocus: before ? "bottom" : "top",
+ variableName: undefined,
});
return state;
},
@@ -571,6 +572,7 @@ const {
cell: state.cellHandles[nextCellId],
config: state.cellData[nextCellId].config,
codeFocus: before ? "bottom" : "top",
+ variableName: undefined,
});
return state;
},
@@ -596,6 +598,7 @@ const {
cell: state.cellHandles[nextCellId],
config: state.cellData[nextCellId].config,
codeFocus: undefined,
+ variableName: undefined,
});
}
diff --git a/frontend/src/core/cells/scrollCellIntoView.ts b/frontend/src/core/cells/scrollCellIntoView.ts
index 573668633b..4ebc3f2cee 100644
--- a/frontend/src/core/cells/scrollCellIntoView.ts
+++ b/frontend/src/core/cells/scrollCellIntoView.ts
@@ -4,17 +4,20 @@ import { Logger } from "../../utils/Logger";
import { CellId, HTMLCellId } from "./ids";
import { CellHandle } from "@/components/editor/Cell";
import { CellConfig } from "./types";
+import { goToDefinition } from "../codemirror/find-replace/search-highlight";
export function focusAndScrollCellIntoView({
cellId,
cell,
config,
codeFocus,
+ variableName,
}: {
cellId: CellId;
cell: RefObject<CellHandle>;
config: CellConfig;
codeFocus: "top" | "bottom" | undefined;
+ variableName: string | undefined;
}) {
if (!cell) {
return;
@@ -53,6 +56,8 @@ export function focusAndScrollCellIntoView({
head: lastLine.from,
},
});
+ } else if (variableName) {
+ goToDefinition(editor, variableName);
}
}
diff --git a/frontend/src/core/codemirror/cells/extensions.ts b/frontend/src/core/codemirror/cells/extensions.ts
index 6d509a0493..550d14fb1f 100644
--- a/frontend/src/core/codemirror/cells/extensions.ts
+++ b/frontend/src/core/codemirror/cells/extensions.ts
@@ -9,6 +9,7 @@ import { getEditorCodeAsPython } from "../language/utils";
import { formattingChangeEffect } from "../format";
import { closeCompletion, completionStatus } from "@codemirror/autocomplete";
import { isAtEndOfEditor, isAtStartOfEditor } from "../utils";
+import { goToDefinition } from "../go-to-definition";
export interface MovementCallbacks
extends Pick<
@@ -240,6 +241,15 @@ export function cellMovementBundle(
return true;
},
},
+ {
+ key: HOTKEYS.getHotkey("cell.goToDefinition").key,
+ preventDefault: true,
+ stopPropagation: true,
+ run: (ev) => {
+ goToDefinition(ev);
+ return true;
+ },
+ },
{
key: HOTKEYS.getHotkey("cell.splitCell").key,
preventDefault: true,
@@ -251,7 +261,6 @@ export function cellMovementBundle(
ev.contentDOM.blur();
moveToNextCell({ cellId, before: false }); // focus new cell
});
-
return true;
},
},
diff --git a/frontend/src/core/codemirror/go-to-definition.ts b/frontend/src/core/codemirror/go-to-definition.ts
new file mode 100644
index 0000000000..cfbb4186f2
--- /dev/null
+++ b/frontend/src/core/codemirror/go-to-definition.ts
@@ -0,0 +1,50 @@
+/* Copyright 2024 Marimo. All rights reserved. */
+import { EditorState } from "@codemirror/state";
+import { EditorView } from "@codemirror/view";
+import { getPositionAtWordBounds } from "../codemirror/completion/hints";
+import { VariableName, Variables } from "../variables/types";
+import { focusAndScrollCellIntoView } from "../cells/scrollCellIntoView";
+import { store } from "../state/jotai";
+import { notebookAtom } from "../cells/cells";
+import { variablesAtom } from "../variables/state";
+
+const getWordUnderCursor = (state: EditorState) => {
+ const { from, to } = state.selection.main;
+ let variableName: string;
+
+ if (from === to) {
+ const { startToken, endToken } = getPositionAtWordBounds(state.doc, from);
+ variableName = state.doc.sliceString(startToken, endToken);
+ } else {
+ variableName = state.doc.sliceString(from, to);
+ }
+
+ return variableName;
+};
+
+const getCellIdOfDefinition = (variables: Variables, variableName: string) => {
+ const variable = variables[variableName as VariableName];
+ if (!variable || variable.declaredBy.length === 0) {
+ return null;
+ }
+ const focusCellId = variable.declaredBy[0];
+ return focusCellId;
+};
+
+export function goToDefinition(view: EditorView) {
+ const state = view.state;
+ const variables = store.get(variablesAtom);
+ const variableName = getWordUnderCursor(state);
+ const focusCellId = getCellIdOfDefinition(variables, variableName);
+
+ if (focusCellId) {
+ const notebookState = store.get(notebookAtom);
+ focusAndScrollCellIntoView({
+ cellId: focusCellId,
+ cell: notebookState.cellHandles[focusCellId],
+ config: notebookState.cellData[focusCellId].config,
+ codeFocus: undefined,
+ variableName,
+ });
+ }
+}
diff --git a/frontend/src/core/hotkeys/__tests__/shortcuts.test.ts b/frontend/src/core/hotkeys/__tests__/shortcuts.test.ts
index bc46620fe7..b8d33ff009 100644
--- a/frontend/src/core/hotkeys/__tests__/shortcuts.test.ts
+++ b/frontend/src/core/hotkeys/__tests__/shortcuts.test.ts
@@ -33,6 +33,12 @@ describe("parseShortcut", () => {
expect(shortcut(event)).toBe(true);
});
+ it("should recognize cell.goToDefinition hotkey", () => {
+ const shortcut = parseShortcut("F12");
+ const event = new KeyboardEvent("keydown", { key: "F12" });
+ expect(shortcut(event)).toBe(true);
+ });
+
it("should not recognize incorrect single key shortcuts", () => {
const shortcut = parseShortcut("a");
const event = new KeyboardEvent("keydown", { key: "b" });
diff --git a/frontend/src/core/hotkeys/hotkeys.ts b/frontend/src/core/hotkeys/hotkeys.ts
index b4449ce489..dc5e14a6ab 100644
--- a/frontend/src/core/hotkeys/hotkeys.ts
+++ b/frontend/src/core/hotkeys/hotkeys.ts
@@ -301,6 +301,11 @@ const DEFAULT_HOT_KEY = {
group: "Navigation",
key: "Mod-Shift-s",
},
+ "cell.goToDefinition": {
+ name: "Go to Definition",
+ group: "Navigation",
+ key: "F12",
+ },
} satisfies Record<string, Hotkey>;
export type HotkeyAction = keyof typeof DEFAULT_HOT_KEY;