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
fix: lsp lensing (#5721)
Myles Scolnick68c36c1c9b5e
diff --git a/frontend/src/core/codemirror/copilot/__tests__/getCodes.test.ts b/frontend/src/core/codemirror/copilot/__tests__/getCodes.test.tsindex 2e156431ff..4345dbe2db 100644--- a/frontend/src/core/codemirror/copilot/__tests__/getCodes.test.ts+++ b/frontend/src/core/codemirror/copilot/__tests__/getCodes.test.ts@@ -91,6 +91,47 @@ describe("getTopologicalCellIds", () => {// Assert the resultexpect(result).toEqual(["cell1", "cell2", "cell3", "cell4"]);});++ it("should return sorted basic cell ids", () => {+ const cellIds = [Cells.cell1, Cells.cell2];+ // Cell 1:+ // mo.++ // Cell 2:+ // import marimo as mo++ let result = getTopologicalCellIds(cellIds, {+ [Variables.var1]: {+ name: Variables.var1,+ declaredBy: [Cells.cell2],+ usedBy: [Cells.cell1],+ },+ });+ expect(result).toEqual(["cell2", "cell1"]);++ // Swap them around+ result = getTopologicalCellIds([Cells.cell2, Cells.cell1], {+ [Variables.var1]: {+ name: Variables.var1,+ declaredBy: [Cells.cell2],+ usedBy: [Cells.cell1],+ },+ });+ expect(result).toEqual(["cell2", "cell1"]);+ });++ it("should put new cells (with no dependencies) at the end", () => {+ const cellIds = [Cells.cell1, Cells.cell2, Cells.cell3, Cells.cell4];+ const variables: Variables = {+ [Variables.var1]: {+ name: Variables.var1,+ declaredBy: [Cells.cell2],+ usedBy: [Cells.cell3],+ },+ };+ const result = getTopologicalCellIds(cellIds, variables);+ expect(result).toEqual(["cell2", "cell1", "cell3", "cell4"]);+ });});describe("getCodes", () => {diff --git a/frontend/src/core/codemirror/copilot/getCodes.ts b/frontend/src/core/codemirror/copilot/getCodes.tsindex 1fa38e2a02..5108bcc0a8 100644--- a/frontend/src/core/codemirror/copilot/getCodes.ts+++ b/frontend/src/core/codemirror/copilot/getCodes.ts@@ -74,19 +74,22 @@ export function getTopologicalCellIds(cellIds: CellId[], variables: Variables) {const adjacency = new Map<CellId, CellId[]>();cellIds.forEach((id) => adjacency.set(id, []));+ // Start with all cells with no declaredBy or usedBy+ const noDepCells = new Set<CellId>(cellIds); // Cells with no declaredBy+ const noUsedByCells = new Set<CellId>(cellIds); // Cells with no usedBy+// Link "declaredBy -> usedBy"for (const { declaredBy, usedBy } of Object.values(variables)) {- if (!declaredBy || !usedBy) {- continue;- }- const declArr = Array.isArray(declaredBy) ? declaredBy : [declaredBy];- declArr.forEach((declCell) => {- usedBy.forEach((useCell) => {+ for (const declCell of declaredBy) {+ noDepCells.delete(declCell);+ for (const useCell of usedBy) {+ noUsedByCells.delete(useCell);+if (useCell !== declCell) {adjacency.get(declCell)?.push(useCell);}- });- });+ }+ }}// Kahn's algorithm for topological sort@@ -121,5 +124,7 @@ export function getTopologicalCellIds(cellIds: CellId[], variables: Variables) {});}- return sorted;+ // Put noDepCells at the end+ const filteredSorted = sorted.filter((id) => !noDepCells.has(id));+ return [...filteredSorted, ...noDepCells];}diff --git a/frontend/src/core/codemirror/lsp/__tests__/notebook-lsp.test.ts b/frontend/src/core/codemirror/lsp/__tests__/notebook-lsp.test.tsindex 02bed04796..327c2ed310 100644--- a/frontend/src/core/codemirror/lsp/__tests__/notebook-lsp.test.ts+++ b/frontend/src/core/codemirror/lsp/__tests__/notebook-lsp.test.ts@@ -37,6 +37,24 @@ describe("createNotebookLens", () => {expect(transformed.character).toBe(0);});+ it("order of code should not matter", () => {+ const cellIds: CellId[] = [Cells.cell1, Cells.cell2];+ let codes: Record<CellId, string> = {+ [Cells.cell1]: "before\ntext",+ [Cells.cell2]: "cell1\ncell2",+ };+ let lens = createNotebookLens(cellIds, codes);+ expect(lens.mergedText).toBe("before\ntext\ncell1\ncell2");++ // Swap them around+ codes = {+ [Cells.cell2]: "cell1\ncell2",+ [Cells.cell1]: "before\ntext",+ };+ lens = createNotebookLens(cellIds, codes);+ expect(lens.mergedText).toBe("before\ntext\ncell1\ncell2");+ });+it("should transform ranges to merged doc", () => {const cellIds: CellId[] = [Cells.cell1, Cells.cell2];const codes: Record<CellId, string> = {diff --git a/frontend/src/core/codemirror/lsp/lens.ts b/frontend/src/core/codemirror/lsp/lens.tsindex c741e2566a..9570d144fc 100644--- a/frontend/src/core/codemirror/lsp/lens.ts+++ b/frontend/src/core/codemirror/lsp/lens.ts@@ -55,7 +55,7 @@ export function createNotebookLens(return cellLineOffsets.get(cellId) ?? 0;}- const mergedText = Object.values(codes).join("\n");+ const mergedText = sortedCellIds.map((cellId) => codes[cellId]).join("\n");return {cellIds: sortedCellIds,