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.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
[codex] Fix reactive go-to-definition lookup (#9747)
Khanh Le7d16cd98566e
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.tsindex 63e11cc2e2..3c19136ddb 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@@ -39,6 +39,43 @@ afterEach(() => {});describe("goToDefinitionAtCursorPosition", () => {+ test("jumps to a reactive variable definition in another cell", async () => {+ const definingCell = cellId("defining-cell");+ const usageCell = cellId("usage-cell");+ const definingCode = "a = 10";+ const usageCode = "print(a)";++ const definingView = createEditor(definingCode, definingCode.length);+ const usageView = createEditor(usageCode, usageCode.indexOf("a"));+ views.push(definingView, usageView);++ const notebook = initialNotebookState();+ notebook.cellHandles[definingCell] = {+ current: { editorView: definingView, editorViewOrNull: definingView },+ };+ notebook.cellHandles[usageCell] = {+ current: { editorView: usageView, editorViewOrNull: usageView },+ };++ store.set(notebookAtom, notebook);+ store.set(variablesAtom, {+ [variableName("a")]: {+ dataType: "int",+ declaredBy: [definingCell],+ name: variableName("a"),+ usedBy: [usageCell],+ value: "10",+ },+ });++ const result = goToDefinitionAtCursorPosition(usageView);++ expect(result).toBe(true);+ await tick();+ expect(definingView.state.selection.main.head).toBe(0);+ expect(usageView.state.selection.main.head).toBe(usageCode.indexOf("a"));+ });+test("prefers the current-cell local definition over a reactive global", async () => {const globalCell = cellId("global-cell");const localCell = cellId("local-cell");diff --git a/frontend/src/core/codemirror/go-to-definition/commands.ts b/frontend/src/core/codemirror/go-to-definition/commands.tsindex 5ba4f47bd3..23c065755d 100644--- a/frontend/src/core/codemirror/go-to-definition/commands.ts+++ b/frontend/src/core/codemirror/go-to-definition/commands.ts@@ -403,22 +403,29 @@ function findScopedDefinitionPosition(}/**- * This function will select the first occurrence of the given variable name,- * for a given editor view.+ * This function selects a scoped definition for the given variable name, when+ * a usage position is available, or optionally falls back to the first matching+ * variable name in the given editor view.* @param view The editor view which contains the variable name.* @param variableName The name of the variable to select, if found in the editor.* @param usagePosition The position of the variable usage, if available.+ * @param fallbackToFirstMatch Whether to fall back to the first matching+ * variable name when no scoped definition is found. Defaults to true.*/export function goToVariableDefinition(view: EditorView,variableName: string,usagePosition?: number,+ fallbackToFirstMatch = true,): boolean {const { state } = view;- const from =- (usagePosition !== undefined- ? findScopedDefinitionPosition(state, variableName, usagePosition)- : null) ?? findFirstMatchingVariable(state, variableName);+ let from: number | null = null;+ if (usagePosition !== undefined) {+ from = findScopedDefinitionPosition(state, variableName, usagePosition);+ }+ if (from === null && fallbackToFirstMatch) {+ from = findFirstMatchingVariable(state, variableName);+ }if (from === null) {return false;diff --git a/frontend/src/core/codemirror/go-to-definition/utils.ts b/frontend/src/core/codemirror/go-to-definition/utils.tsindex 8eb09b7a41..24fbc4cb71 100644--- a/frontend/src/core/codemirror/go-to-definition/utils.ts+++ b/frontend/src/core/codemirror/go-to-definition/utils.ts@@ -82,6 +82,7 @@ export function goToDefinition(view,variableName,usagePosition,+ false,);if (foundLocally) {return true;