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
Arrange cells through the document model (#11551)
Trevor Manz6aa55b3f214b
diff --git a/frontend/src/__mocks__/document.tsx b/frontend/src/__mocks__/document.tsxindex 65a04d4901..7c82ce6c35 100644--- a/frontend/src/__mocks__/document.tsx+++ b/frontend/src/__mocks__/document.tsx@@ -1,6 +1,6 @@/* Copyright 2026 Marimo. All rights reserved. */-import { atom, type createStore, Provider } from "jotai";+import { atom, type createStore, type PrimitiveAtom, Provider } from "jotai";import type { ReactNode } from "react";import {createDocumentModel,@@ -12,21 +12,30 @@ import type { Document } from "@/core/notebook/replica";import { fakeBackend } from "@/core/notebook/replica/__tests__/fixtures";import { activeDocumentModelAtom } from "@/core/session/active-notebook";-/** A started document model over `document`, for components that edit cells. */+/**+ * A started document model over `document`, for components that edit cells.+ * Setting `documentAtom` delivers a document as the host would.+ */export function mockDocumentModel(store: ReturnType<typeof createStore>,document: Document | null = documentFixture(),-): { model: DocumentModel; stop: () => void } {+): {+ model: DocumentModel;+ documentAtom: PrimitiveAtom<Document | null>;+ stop: () => void;+} {const controller = new AbortController();+ const documentAtom = atom(document);const model = createDocumentModel({store,- documentAtom: atom(document),+ documentAtom,backendAtom: atom(fakeBackend()),});model.start({ signal: controller.signal });store.set(activeDocumentModelAtom, model);return {model,+ documentAtom,stop: () => {controller.abort();if (store.get(activeDocumentModelAtom) === model) {diff --git a/frontend/src/__mocks__/notebook.ts b/frontend/src/__mocks__/notebook.tsindex ab5b801f48..534a54e278 100644--- a/frontend/src/__mocks__/notebook.ts+++ b/frontend/src/__mocks__/notebook.ts@@ -11,7 +11,6 @@ import {createCellRuntimeState,} from "@/core/cells/types";import type { MarimoError } from "@/core/kernel/messages";-import { MultiColumn } from "@/utils/id-tree";import { Objects } from "@/utils/objects";export const MockNotebook = {@@ -46,7 +45,7 @@ export const MockNotebook = {serializedEditorState: null,...data,})),- cellIds: MultiColumn.from([Object.keys(cellData) as CellId[]]),+ cellIds: Object.keys(cellData) as CellId[],cellRuntime: Objects.mapValues(cellData, (_data, cid) =>createCellRuntimeState({ ...cellRuntime[cid] }),),diff --git a/frontend/src/components/datasources/datasources.tsx b/frontend/src/components/datasources/datasources.tsxindex 7017f95ebe..88db696070 100644--- a/frontend/src/components/datasources/datasources.tsx+++ b/frontend/src/components/datasources/datasources.tsx@@ -134,7 +134,7 @@ const sortedTablesAtom = atom((get) => {return 0;}- const index = cellIds.inOrderIds.indexOf(variable.declaredBy[0]);+ const index = cellIds.indexOf(variable.declaredBy[0]);if (index === -1) {return 0;}diff --git a/frontend/src/components/dependency-graph/custom-node.tsx b/frontend/src/components/dependency-graph/custom-node.tsxindex 9359df80c3..d2b43f338c 100644--- a/frontend/src/components/dependency-graph/custom-node.tsx+++ b/frontend/src/components/dependency-graph/custom-node.tsx@@ -30,7 +30,7 @@ const EQUALITY_CHECK = (export const CustomNode = memo((props: CustomNodeProps) => {const { data, selected } = props; // must match the equality checkconst cell = useAtomValue(data.atom);- const cellIndex = useCellIds().inOrderIds.indexOf(cell.id);+ const cellIndex = useCellIds().indexOf(cell.id);const handleColor = selected ? "var(--gray-9)" : "var(--gray-3)";const edgeMarkers = use(EdgeMarkerContext);diff --git a/frontend/src/components/dependency-graph/minimap-content.tsx b/frontend/src/components/dependency-graph/minimap-content.tsxindex 4aa8ccf4ba..398c9bcec0 100644--- a/frontend/src/components/dependency-graph/minimap-content.tsx+++ b/frontend/src/components/dependency-graph/minimap-content.tsx@@ -7,10 +7,11 @@ import {cellGraphsAtom,isVariableAffectedBySelectedCell,} from "@/components/editor/chrome/wrapper/minimap-state";-import { useCellData, useCellIds, useCellRuntime } from "@/core/cells/cells";+import { useCellData, useCellRuntime } from "@/core/cells/cells";import { useCellActions } from "@/core/cells/actions";import { cellFocusAtom, useCellFocusActions } from "@/core/cells/focus";import type { CellId } from "@/core/cells/ids";+import { useLayout } from "@/core/cells/layout";import { useVariables } from "@/core/variables/state";import { cn } from "@/utils/cn";import { extractCellPreview } from "./utils/cell-preview";@@ -380,28 +381,25 @@ function isNonReferenceableCell(graph: CellGraph): boolean {* Shows a scrollable list of cells with dependency visualization.*/export const MinimapContent: React.FC = () => {- const cellIds = useCellIds();+ const layout = useLayout();+ const rows = layout.columns.flatMap((column, index) =>+ column.ids.map((cellId, position) => ({+ cellId,+ startsColumn: index > 0 && position === 0,+ })),+ );const cellPositions: Record<CellId, number> = Object.fromEntries(- cellIds.inOrderIds.map((id, idx) => [id, idx]),+ rows.map(({ cellId }, idx) => [cellId, idx]),);- const columnBoundaries: number[] = [];- let cellCount = 0;- for (const [idx, column] of cellIds.getColumns().entries()) {- if (idx > 0) {- columnBoundaries.push(cellCount);- }- cellCount += column.inOrderIds.length;- }return (<div className="overflow-y-auto overflow-x-hidden flex-1 scrollbar-none h-full max-w-80"><div className="py-3 pl-3 pr-4 relative min-h-full">- {cellIds.inOrderIds.map((cellId, idx) => {- const isColumnBoundary = columnBoundaries.includes(idx);+ {rows.map(({ cellId, startsColumn }, idx) => {return (<React.Fragment key={cellId}>{/* Subtle visual divider between nodes */}- {isColumnBoundary && (+ {startsColumn && (<divclassName="absolute left-5 w-[36px] h-px bg-(--gray-4) pointer-events-none"aria-hidden="true"diff --git a/frontend/src/components/editor/actions/useSetCodeVisibility.ts b/frontend/src/components/editor/actions/useSetCodeVisibility.tsindex 3101e40d5e..274b8cc204 100644--- a/frontend/src/components/editor/actions/useSetCodeVisibility.ts+++ b/frontend/src/components/editor/actions/useSetCodeVisibility.ts@@ -29,7 +29,7 @@ export const useSetCodeVisibility = () => {const newConfigs: Record<CellId, Partial<CellConfig>> = {};- for (const cellId of notebook.cellIds.inOrderIds) {+ for (const cellId of notebook.cellIds) {const cell = notebook.cellData[cellId];if (cell === undefined || cell.config.hide_code === hidden) {continue;diff --git a/frontend/src/components/editor/actions/useSetOutputsExpanded.ts b/frontend/src/components/editor/actions/useSetOutputsExpanded.tsindex 5d89e7e432..fea5c7e525 100644--- a/frontend/src/components/editor/actions/useSetOutputsExpanded.ts+++ b/frontend/src/components/editor/actions/useSetOutputsExpanded.ts@@ -19,7 +19,7 @@ export function outputExpansionConfigs(expanded: boolean,): Record<CellId, Partial<CellConfig>> {const configs: Record<CellId, Partial<CellConfig>> = {};- for (const cellId of notebook.cellIds.inOrderIds) {+ for (const cellId of notebook.cellIds) {const cell = notebook.cellData[cellId];if (cell === undefined ||diff --git a/frontend/src/components/editor/alerts/stdin-blocking-alert.tsx b/frontend/src/components/editor/alerts/stdin-blocking-alert.tsxindex d7dd07dbb8..585d081bc4 100644--- a/frontend/src/components/editor/alerts/stdin-blocking-alert.tsx+++ b/frontend/src/components/editor/alerts/stdin-blocking-alert.tsx@@ -12,7 +12,7 @@ const hasBlockingStdinAtom = atom((get) => {const notebook = get(notebookAtom);// Check each cell in order- for (const cellId of notebook.cellIds.inOrderIds) {+ for (const cellId of notebook.cellIds) {// Check if the cell is idle and has unresolved stdinconst runtime = notebook.cellRuntime[cellId];if (runtime.status === "idle") {diff --git a/frontend/src/components/editor/cell/code/cell-editor.tsx b/frontend/src/components/editor/cell/code/cell-editor.tsxindex 1575c4ebe7..7bd555330f 100644--- a/frontend/src/components/editor/cell/code/cell-editor.tsx+++ b/frontend/src/components/editor/cell/code/cell-editor.tsx@@ -236,7 +236,7 @@ const CellEditorInternal = ({addOrAppendSetupCell: (code) => {const notebook = getNotebook();// No setup cell yet: create one with this code at the top.- if (!notebook.cellIds.setupCellExists()) {+ if (!notebook.cellIds.includes(SETUP_CELL_ID)) {cellActions.addSetupCell({ code });return;}diff --git a/frontend/src/components/editor/cell/useSplitCell.tsx b/frontend/src/components/editor/cell/useSplitCell.tsxindex e6bf5c0b79..8e95aee466 100644--- a/frontend/src/components/editor/cell/useSplitCell.tsx+++ b/frontend/src/components/editor/cell/useSplitCell.tsx@@ -11,16 +11,18 @@ import { getEditorCodeAsPython } from "@/core/codemirror/language/utils";export function useSplitCellCallback() {const { splitCell, undoSplitCell } = useCellActions();- return useEvent((opts: { cellId: CellId }) => {+ return useEvent((opts: { cellId: CellId }): CellId | null => {// Save snapshot of code for undoconst cellEditorView = getCellEditorView(opts.cellId);if (!cellEditorView) {- return;+ return null;}const code = getEditorCodeAsPython(cellEditorView);- // Optimistic update- splitCell(opts);+ const newCellId = splitCell(opts);+ if (newCellId === null) {+ return null;+ }const { dismiss } = toast({title: "Cell split",@@ -30,7 +32,7 @@ export function useSplitCellCallback() {size="sm"variant="outline"onClick={() => {- undoSplitCell({ ...opts, snapshot: code });+ undoSplitCell({ ...opts, newCellId, snapshot: code });dismiss();}}>@@ -38,5 +40,6 @@ export function useSplitCellCallback() {</UndoButton>),});+ return newCellId;});}diff --git a/frontend/src/components/editor/chrome/panels/dependency-graph-panel.tsx b/frontend/src/components/editor/chrome/panels/dependency-graph-panel.tsxindex 88cc056560..f4305c464f 100644--- a/frontend/src/components/editor/chrome/panels/dependency-graph-panel.tsx+++ b/frontend/src/components/editor/chrome/panels/dependency-graph-panel.tsx@@ -57,7 +57,7 @@ const DependencyGraphPanel: React.FC = () => {<DependencyGraphcellAtoms={cells}variables={variables}- cellIds={cellIds.inOrderIds}+ cellIds={cellIds}/>)}</div>diff --git a/frontend/src/components/editor/chrome/panels/session-panel.tsx b/frontend/src/components/editor/chrome/panels/session-panel.tsxindex 778c1e0a3a..2522e80c6b 100644--- a/frontend/src/components/editor/chrome/panels/session-panel.tsx+++ b/frontend/src/components/editor/chrome/panels/session-panel.tsx@@ -88,7 +88,7 @@ const SessionPanel: React.FC = () => {No variables defined</div>) : (- <VariableTable cellIds={cellIds.inOrderIds} variables={variables} />+ <VariableTable cellIds={cellIds} variables={variables} />)}</PanelAccordionContent></PanelAccordionItem>diff --git a/frontend/src/components/editor/chrome/panels/variable-panel.tsx b/frontend/src/components/editor/chrome/panels/variable-panel.tsxindex 1290088937..5052d5dfca 100644--- a/frontend/src/components/editor/chrome/panels/variable-panel.tsx+++ b/frontend/src/components/editor/chrome/panels/variable-panel.tsx@@ -22,11 +22,7 @@ const VariablePanel: React.FC = () => {}return (- <VariableTable- className="flex-1"- cellIds={cellIds.inOrderIds}- variables={variables}- />+ <VariableTable className="flex-1" cellIds={cellIds} variables={variables} />);};diff --git a/frontend/src/components/editor/chrome/wrapper/minimap-state.ts b/frontend/src/components/editor/chrome/wrapper/minimap-state.tsindex e0877c1f57..6f8ab25b74 100644--- a/frontend/src/components/editor/chrome/wrapper/minimap-state.ts+++ b/frontend/src/components/editor/chrome/wrapper/minimap-state.ts@@ -104,7 +104,7 @@ export function buildCellGraph(export const cellGraphsAtom = atom((get) => {const cellIds = get(cellIdsAtom);const variables = get(variablesAtom);- return buildCellGraph(cellIds.inOrderIds, variables);+ return buildCellGraph(cellIds, variables);});/**diff --git a/frontend/src/components/editor/columns/__tests__/cell-column.test.tsx b/frontend/src/components/editor/columns/__tests__/cell-column.test.tsxindex e34a436df7..850828a7c0 100644--- a/frontend/src/components/editor/columns/__tests__/cell-column.test.tsx+++ b/frontend/src/components/editor/columns/__tests__/cell-column.test.tsx@@ -3,7 +3,7 @@ import { render } from "@testing-library/react";import { createStore, Provider } from "jotai";import { describe, expect, it } from "vitest";import { TooltipProvider } from "@/components/ui/tooltip";-import type { CellColumnId } from "@/utils/id-tree";+import type { CellColumnId } from "@/core/cells/layout";import { Column } from "../cell-column";const columnId = "col-1" as CellColumnId;diff --git a/frontend/src/components/editor/columns/cell-column.tsx b/frontend/src/components/editor/columns/cell-column.tsxindex 0eb5d8da0c..2d5fc9873c 100644--- a/frontend/src/components/editor/columns/cell-column.tsx+++ b/frontend/src/components/editor/columns/cell-column.tsx@@ -4,7 +4,7 @@ import React, { memo, useRef } from "react";import type { AppConfig } from "@/core/config/config-schema";import { useResizeHandle } from "@/hooks/useResizeHandle";import { cn } from "@/utils/cn";-import type { CellColumnId } from "@/utils/id-tree";+import type { CellColumnId } from "@/core/cells/layout";import { SortableColumn } from "./sortable-column";import { storageFn } from "./storage";diff --git a/frontend/src/components/editor/columns/sortable-column.tsx b/frontend/src/components/editor/columns/sortable-column.tsxindex 9e4316ccb2..e1f2035fb2 100644--- a/frontend/src/components/editor/columns/sortable-column.tsx+++ b/frontend/src/components/editor/columns/sortable-column.tsx@@ -21,7 +21,7 @@ import {import { Tooltip } from "@/components/ui/tooltip";import { useCellActions } from "@/core/cells/actions";import { cn } from "@/utils/cn";-import type { CellColumnId } from "@/utils/id-tree";+import type { CellColumnId } from "@/core/cells/layout";import { mergeRefs } from "../../../utils/mergeRefs";interface Props extends React.HTMLAttributes<HTMLDivElement> {diff --git a/frontend/src/components/editor/links/__tests__/cell-link.test.tsx b/frontend/src/components/editor/links/__tests__/cell-link.test.tsxindex 90ab9f8e0f..f04af3732e 100644--- a/frontend/src/components/editor/links/__tests__/cell-link.test.tsx+++ b/frontend/src/components/editor/links/__tests__/cell-link.test.tsx@@ -15,7 +15,7 @@ vi.mock("@/core/cells/actions", () => ({useCellActions: () => ({ showCellIfHidden }),}));vi.mock("@/core/cells/cells", () => ({- useCellIds: () => ({ inOrderIds: [targetId] }),+ useCellIds: () => [targetId],useCellNames: () => ({ [targetId]: "Target" }),}));vi.mock("@/core/saving/filename", () => ({ useFilename: () => "notebook.py" }));diff --git a/frontend/src/components/editor/links/cell-link-list.tsx b/frontend/src/components/editor/links/cell-link-list.tsxindex 9dd6b38ced..b548b43d9d 100644--- a/frontend/src/components/editor/links/cell-link-list.tsx+++ b/frontend/src/components/editor/links/cell-link-list.tsx@@ -25,7 +25,7 @@ export const CellLinkList: React.FC<Props> = ({}) => {const cellIndex = useCellIds();const sortedCellIds = cellIds.toSorted((a, b) => {- return cellIndex.inOrderIds.indexOf(a) - cellIndex.inOrderIds.indexOf(b);+ return cellIndex.indexOf(a) - cellIndex.indexOf(b);});if (cellIds.length === 0) {diff --git a/frontend/src/components/editor/links/cell-link.tsx b/frontend/src/components/editor/links/cell-link.tsxindex 44f528400f..a4418f909a 100644--- a/frontend/src/components/editor/links/cell-link.tsx+++ b/frontend/src/components/editor/links/cell-link.tsx@@ -27,7 +27,7 @@ export const CellLink = (props: Props): JSX.Element => {const { className, cellId, variant, onClick, formatCellName, skipScroll } =props;const cellName = useCellNames()[cellId] ?? "";- const cellIndex = useCellIds().inOrderIds.indexOf(cellId);+ const cellIndex = useCellIds().indexOf(cellId);const { showCellIfHidden } = useCellActions();const formatName = formatCellName ?? ((name: string) => name);diff --git a/frontend/src/components/editor/navigation/__tests__/navigation.test.ts b/frontend/src/components/editor/navigation/__tests__/navigation.test.tsindex 29c29bca42..f9a402152f 100644--- a/frontend/src/components/editor/navigation/__tests__/navigation.test.ts+++ b/frontend/src/components/editor/navigation/__tests__/navigation.test.ts@@ -125,7 +125,6 @@ afterAll(() => {});import { defaultUserConfig } from "@/core/config/config-schema";-import { MultiColumn } from "@/utils/id-tree";import { focusCell, focusCellEditor } from "../focus-utils";import {type CellSelectionState,@@ -1427,12 +1426,27 @@ describe("useCellNavigationProps", () => {},},});- // Override cellIds to be multi-column- notebookState.cellIds = MultiColumn.from([- [cellId1, cellId3],- [cellId2, cellId4, cellId5],- ]);store.set(notebookAtom, notebookState);+ // The layout reads columns from the document's cells.+ stopModel();+ const inColumn = (id: CellId, column: number) =>+ cell(id, "", { config: { ...CONFIG, column } });+ ({ model, stop: stopModel } = mockDocumentModel(+ store,+ withCells(+ documentOf(),+ {+ c1: null,+ c2: null,+ [cellId1]: inColumn(cellId1, 0),+ [cellId3]: inColumn(cellId3, 0),+ [cellId2]: inColumn(cellId2, 1),+ [cellId4]: inColumn(cellId4, 1),+ [cellId5]: inColumn(cellId5, 1),+ },+ [cellId1, cellId3, cellId2, cellId4, cellId5],+ ),+ ));// Mock DOM elements and their positionsconst mockGetElementById = vi.spyOn(document, "getElementById");diff --git a/frontend/src/components/editor/navigation/__tests__/selection.test.ts b/frontend/src/components/editor/navigation/__tests__/selection.test.tsindex 23aebd8bc4..083903dfe5 100644--- a/frontend/src/components/editor/navigation/__tests__/selection.test.ts+++ b/frontend/src/components/editor/navigation/__tests__/selection.test.ts@@ -3,8 +3,7 @@import { beforeEach, describe, expect, it } from "vitest";import { cellId } from "@/__tests__/branded";import type { CellId } from "@/core/cells/ids";-import type { Layout } from "@/core/cells/layout";-import type { CellColumnId } from "@/utils/id-tree";+import type { CellColumnId, Layout } from "@/core/cells/layout";import type { CellSelectionState } from "../selection";import { exportedForTesting } from "../selection";diff --git a/frontend/src/components/editor/navigation/clipboard.ts b/frontend/src/components/editor/navigation/clipboard.tsindex 7e87ccb807..5c3f55f267 100644--- a/frontend/src/components/editor/navigation/clipboard.ts+++ b/frontend/src/components/editor/navigation/clipboard.ts@@ -6,6 +6,8 @@ import { toast } from "@/components/ui/use-toast";import { getNotebook } from "@/core/cells/cells";import { useCellActions } from "@/core/cells/actions";import type { CellId } from "@/core/cells/ids";+import { columnOf, layoutAtom } from "@/core/cells/layout";+import { store } from "@/core/state/jotai";import {usePendingCutActions,usePendingCutState,@@ -140,10 +142,10 @@ export function useCellClipboard() {// Check if we have pending cut cells (internal move)if (pendingCutState.cellIds.size > 0) {const pendingCellIds = [...pendingCutState.cellIds];- const notebook = getNotebook();+ const layout = store.get(layoutAtom);const previousPlacements = pendingCellIds.map((id) => {- const column = notebook.cellIds.findWithId(id);- return { columnId: column.id, index: column.indexOfOrThrow(id) };+ const column = columnOf(layout, id);+ return { columnId: column.id, index: column.ids.indexOf(id) };});actions.moveCellsRelativeTo({diff --git a/frontend/src/components/editor/renderers/cell-array.tsx b/frontend/src/components/editor/renderers/cell-array.tsxindex daddeeee8a..ba102383bd 100644--- a/frontend/src/components/editor/renderers/cell-array.tsx+++ b/frontend/src/components/editor/renderers/cell-array.tsx@@ -29,14 +29,9 @@ import { canInteractWithAppAtom } from "@/core/network/connection";import { useBoolean } from "@/hooks/useBoolean";import { cn } from "@/utils/cn";import { Functions } from "@/utils/functions";-import { findColumn, useLayout } from "@/core/cells/layout";-import type { CellColumnId } from "@/utils/id-tree";+import { type CellColumnId, findColumn, useLayout } from "@/core/cells/layout";import { invariant } from "@/utils/invariant";-import {- columnIdsAtom,- useCellIds,- useScrollKey,-} from "../../../core/cells/cells";+import { useCellIds, useScrollKey } from "../../../core/cells/cells";import { useCellActions } from "../../../core/cells/actions";import { formatAll } from "../../../core/codemirror/format";import type { AppConfig, UserConfig } from "../../../core/config/config-schema";@@ -61,7 +56,7 @@ interface CellArrayProps {}export const CellArray: React.FC<CellArrayProps> = (props) => {- const columnIds = useAtomValue(columnIdsAtom);+ const columnIds = useLayout().columns.map((column) => column.id);// Setup context for sortingreturn (@@ -116,7 +111,7 @@ const CellArrayInternal: React.FC<CellArrayProps> = ({const cellIds = useCellIds();const scrollKey = useScrollKey();- const columnIds = cellIds.getColumnIds();+ const columnIds = useLayout().columns.map((column) => column.id);// Scroll to a cell targeted by a previous actionconst scrollToTarget = actions.scrollToTarget;@@ -140,7 +135,7 @@ const CellArrayInternal: React.FC<CellArrayProps> = ({<StdinBlockingAlert /><NotebookBanner width={appConfig.width} />{/* Only show if not cells, otherwise running a single cell will start the connection */}- {cellIds.idLength === 0 && <NotStartedConnectionAlert />}+ {cellIds.length === 0 && <NotStartedConnectionAlert />}<divclassName={cn(appConfig.width === "columns" &&@@ -192,8 +187,8 @@ const CellColumn: React.FC<{const column = findColumn(useLayout(), columnId);invariant(column, `Expected column for: ${columnId}`);- const hasOnlyOneCell = cellIds.hasOnlyOneId();- const hasSetupCell = cellIds.inOrderIds.includes(SETUP_CELL_ID);+ const hasOnlyOneCell = cellIds.length === 1;+ const hasSetupCell = cellIds.includes(SETUP_CELL_ID);const isPresenting = mode === "present";return (diff --git a/frontend/src/components/editor/renderers/slides-layout/slides-layout.tsx b/frontend/src/components/editor/renderers/slides-layout/slides-layout.tsxindex 979b525ed3..63c1672ee6 100644--- a/frontend/src/components/editor/renderers/slides-layout/slides-layout.tsx+++ b/frontend/src/components/editor/renderers/slides-layout/slides-layout.tsx@@ -1,7 +1,7 @@/* Copyright 2026 Marimo. All rights reserved. */import React, { useMemo, useState } from "react";import { useAtomValue } from "jotai";-import { numColumnsAtom } from "@/core/cells/cells";+import { numColumnsAtom } from "@/core/cells/layout";import type { CellId } from "@/core/cells/ids";import { kioskModeAtom } from "@/core/mode";import type { ICellRendererProps } from "../types";diff --git a/frontend/src/components/editor/renderers/vertical-layout/__tests__/useFocusFirstEditor.test.ts b/frontend/src/components/editor/renderers/vertical-layout/__tests__/useFocusFirstEditor.test.tsindex a0eca82d2e..77e7192f3a 100644--- a/frontend/src/components/editor/renderers/vertical-layout/__tests__/useFocusFirstEditor.test.ts+++ b/frontend/src/components/editor/renderers/vertical-layout/__tests__/useFocusFirstEditor.test.ts@@ -25,7 +25,7 @@ describe("useFocusFirstEditor", () => {const getNotebookSpy = vi.spyOn(cellsModule, "getNotebook").mockReturnValue({- cellIds: { iterateTopLevelIds: ["cell-1", "cell-2"] },+ cellIds: ["cell-1", "cell-2"],cellData: {"cell-1": { config: { hide_code: false } },"cell-2": { config: { hide_code: false } },@@ -52,7 +52,7 @@ describe("useFocusFirstEditor", () => {const getNotebookSpy = vi.spyOn(cellsModule, "getNotebook").mockReturnValue({- cellIds: { iterateTopLevelIds: ["cell-1", "cell-2"] },+ cellIds: ["cell-1", "cell-2"],cellData: {"cell-1": { config: { hide_code: true } }, // hidden"cell-2": { config: { hide_code: false } }, // visible@@ -94,7 +94,7 @@ describe("useFocusFirstEditor", () => {// Mock getNotebookconst mockEditor = { focus: vi.fn() };vi.spyOn(cellsModule, "getNotebook").mockReturnValue({- cellIds: { iterateTopLevelIds: [] },+ cellIds: [],cellData: {},cellHandles: {"cell-123": { current: { editorView: mockEditor } },@@ -129,7 +129,7 @@ describe("useFocusFirstEditor", () => {const mockEditor = { focus: vi.fn() };vi.spyOn(cellsModule, "getNotebook").mockReturnValue({- cellIds: { iterateTopLevelIds: ["cell-1"] },+ cellIds: ["cell-1"],cellData: {"cell-1": { config: { hide_code: false } },},diff --git a/frontend/src/components/editor/renderers/vertical-layout/useFocusFirstEditor.ts b/frontend/src/components/editor/renderers/vertical-layout/useFocusFirstEditor.tsindex 79865070bd..94768e4989 100644--- a/frontend/src/components/editor/renderers/vertical-layout/useFocusFirstEditor.ts+++ b/frontend/src/components/editor/renderers/vertical-layout/useFocusFirstEditor.ts@@ -52,7 +52,7 @@ function focusFirstEditor() {const { cellIds, cellData, cellHandles } = getNotebook();// Focus on the first cell if it's been mounted and is not hidden- for (const cellId of cellIds.iterateTopLevelIds) {+ for (const cellId of cellIds) {const handle = cellHandles[cellId];const hidden = cellData[cellId]?.config.hide_code;if (!hidden && handle?.current?.editorView) {diff --git a/frontend/src/components/editor/stdin-blocking-alert.tsx b/frontend/src/components/editor/stdin-blocking-alert.tsxindex 13dd1666b8..e41ae456c9 100644--- a/frontend/src/components/editor/stdin-blocking-alert.tsx+++ b/frontend/src/components/editor/stdin-blocking-alert.tsx@@ -13,7 +13,7 @@ const hasBlockingStdinAtom = atom((get) => {const notebook = get(notebookAtom);// Check each cell in order- for (const cellId of notebook.cellIds.inOrderIds) {+ for (const cellId of notebook.cellIds) {// Check if the cell is idle and has unresolved stdinconst runtime = notebook.cellRuntime[cellId];if (runtime.status === "idle") {diff --git a/frontend/src/components/slides/__tests__/minimap-actions.test.tsx b/frontend/src/components/slides/__tests__/minimap-actions.test.tsxindex 62a4da86fe..f962ec910d 100644--- a/frontend/src/components/slides/__tests__/minimap-actions.test.tsx+++ b/frontend/src/components/slides/__tests__/minimap-actions.test.tsx@@ -6,7 +6,7 @@ import { cellId } from "@/__tests__/branded";import { TooltipProvider } from "@/components/ui/tooltip";import type { CellId } from "@/core/cells/ids";import type { CellData, CellRuntimeState } from "@/core/cells/types";-import { MultiColumn } from "@/utils/id-tree";+import type { CellColumnId } from "@/core/cells/layout";import { SlidesMinimap } from "../minimap";const A = cellId("a");@@ -22,11 +22,20 @@ const { createCell, deleteCell, moveCellToIndex } = vi.hoisted(() => ({vi.mock("@/core/cells/actions", () => ({useCellActions: () => ({ moveCellToIndex, createCell }),}));-vi.mock("@/core/cells/cells", async (importOriginal) => {- const actual = await importOriginal<typeof import("@/core/cells/cells")>();+vi.mock("@/core/cells/layout", async (importOriginal) => {+ const actual = await importOriginal<typeof import("@/core/cells/layout")>();return {...actual,- useCellIds: () => MultiColumn.from([[A, B]]),+ useLayout: () => ({+ columns: [+ {+ id: "column-0" as CellColumnId,+ ids: [A, B],+ visible: [A, B],+ folds: new Map(),+ },+ ],+ }),};});diff --git a/frontend/src/components/slides/__tests__/minimap.test.ts b/frontend/src/components/slides/__tests__/minimap.test.tsindex 60d5a60947..5613fd10af 100644--- a/frontend/src/components/slides/__tests__/minimap.test.ts+++ b/frontend/src/components/slides/__tests__/minimap.test.ts@@ -4,7 +4,7 @@ import { renderHook, act } from "@testing-library/react";import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";import type { CellId } from "@/core/cells/ids";import type { DragMoveEvent } from "@dnd-kit/core";-import { MultiColumn } from "@/utils/id-tree";+import type { CellColumnId, Layout } from "@/core/cells/layout";import { exportedForTesting } from "../minimap";const { useVisibleCellIds, projectDropTarget, resolveDropTarget } =@@ -318,15 +318,23 @@ describe("resolveDropTarget", () => {const C = "c" as CellId;const D = "d" as CellId;- function makeSingleColumnIds(...ids: CellId[]) {- return MultiColumn.from([ids]);+ function makeLayout(...columns: CellId[][]): Layout {+ return {+ columns: columns.map((ids, index) => ({+ id: `column-${index}` as CellColumnId,+ ids,+ visible: ids,+ folds: new Map(),+ })),+ };}+ const makeSingleColumnIds = (...ids: CellId[]) => makeLayout(ids);it("returns null when activeId matches target overId", () => {- const cellIds = makeSingleColumnIds(A, B);+ const layout = makeSingleColumnIds(A, B);expect(resolveDropTarget({- cellIds,+ layout,activeId: A,target: { overId: A, position: "after" },}),@@ -334,68 +342,79 @@ describe("resolveDropTarget", () => {});it("returns null for multi-column layouts", () => {- const cellIds = MultiColumn.from([[A, B], [C]]);+ const layout = makeLayout([A, B], [C]);expect(resolveDropTarget({- cellIds,+ layout,activeId: A,target: { overId: B, position: "after" },}),).toBeNull();});+ it("returns null when the over cell has left the layout", () => {+ const layout = makeSingleColumnIds(A, B);+ expect(+ resolveDropTarget({+ layout,+ activeId: A,+ target: { overId: C, position: "after" },+ }),+ ).toBeNull();+ });+it('resolves "before" to the over index', () => {- const cellIds = makeSingleColumnIds(A, B, C);+ const layout = makeSingleColumnIds(A, B, C);const result = resolveDropTarget({- cellIds,+ layout,activeId: A,target: { overId: C, position: "before" },});expect(result).toEqual({cellId: A,- columnId: cellIds.atOrThrow(0).id,+ columnId: layout.columns[0].id,index: 2,});});it('resolves "after" to one past the over index', () => {- const cellIds = makeSingleColumnIds(A, B, C);+ const layout = makeSingleColumnIds(A, B, C);const result = resolveDropTarget({- cellIds,+ layout,activeId: C,target: { overId: A, position: "after" },});expect(result).toEqual({cellId: C,- columnId: cellIds.atOrThrow(0).id,+ columnId: layout.columns[0].id,index: 1,});});it('resolves "before" on first item to index 0', () => {- const cellIds = makeSingleColumnIds(A, B, C);+ const layout = makeSingleColumnIds(A, B, C);const result = resolveDropTarget({- cellIds,+ layout,activeId: C,target: { overId: A, position: "before" },});expect(result).toEqual({cellId: C,- columnId: cellIds.atOrThrow(0).id,+ columnId: layout.columns[0].id,index: 0,});});it('resolves "after" on last item to one past the end', () => {- const cellIds = makeSingleColumnIds(A, B, C, D);+ const layout = makeSingleColumnIds(A, B, C, D);const result = resolveDropTarget({- cellIds,+ layout,activeId: A,target: { overId: D, position: "after" },});expect(result).toEqual({cellId: A,- columnId: cellIds.atOrThrow(0).id,+ columnId: layout.columns[0].id,index: 4,});});diff --git a/frontend/src/components/slides/minimap.tsx b/frontend/src/components/slides/minimap.tsxindex 82fd3540da..20d817e599 100644--- a/frontend/src/components/slides/minimap.tsx+++ b/frontend/src/components/slides/minimap.tsx@@ -2,10 +2,9 @@import { useDeleteCellCallback } from "@/components/editor/cell/useDeleteCell";import { outputIsStale } from "@/core/cells/cell";-import { useCellIds } from "@/core/cells/cells";import { useCellActions } from "@/core/cells/actions";import type { CellId } from "@/core/cells/ids";-import type { CellColumnId } from "@/utils/id-tree";+import { type CellColumnId, type Layout, useLayout } from "@/core/cells/layout";import { useEffect, useRef, useState } from "react";import type { ICellRendererProps } from "../editor/renderers/types";import type {@@ -50,7 +49,6 @@ import { SLIDE_TYPE_OPTIONS_BY_VALUE } from "./slide-form";type Props = ICellRendererProps<SlidesLayout>;type SlideCell = Props["cells"][number];-type CellIdsState = ReturnType<typeof useCellIds>;type DropPosition = "before" | "after";interface ProjectedDropTarget {@@ -229,7 +227,7 @@ export const SlidesMinimap = ({slideTypes,onSlideClick,}: SlidesMinimapProps) => {- const cellIds = useCellIds();+ const layout = useLayout();const { moveCellToIndex, createCell } = useCellActions();const deleteCell = useDeleteCellCallback();const containerRef = useRef<HTMLDivElement>(null);@@ -297,7 +295,7 @@ export const SlidesMinimap = ({try {if (activeId && dropTarget) {const resolvedTarget = resolveDropTarget({- cellIds,+ layout,activeId,target: dropTarget,});@@ -751,24 +749,27 @@ function projectDropTarget(}function resolveDropTarget({- cellIds,+ layout,activeId,target,}: {- cellIds: CellIdsState;+ layout: Layout;activeId: CellId;target: ProjectedDropTarget;}): ResolvedDropTarget | null {if (activeId === target.overId) {return null;}- if (cellIds.colLength !== 1) {+ if (layout.columns.length !== 1) {Logger.warn("Multi-column mode is not supported");return null;}- const column = cellIds.findWithId(target.overId);- const overIndex = column.indexOfOrThrow(target.overId);+ const [column] = layout.columns;+ const overIndex = column.ids.indexOf(target.overId);+ if (overIndex === -1) {+ return null;+ }return {cellId: activeId,diff --git a/frontend/src/components/sort/SortableCellsProvider.tsx b/frontend/src/components/sort/SortableCellsProvider.tsxindex 9997db977a..684f17558f 100644--- a/frontend/src/components/sort/SortableCellsProvider.tsx+++ b/frontend/src/components/sort/SortableCellsProvider.tsx@@ -22,10 +22,8 @@ import React, { useCallback, useMemo, useState } from "react";import type { CellId } from "@/core/cells/ids";import { useAppConfig } from "@/core/config/config";import { Arrays } from "@/utils/arrays";-import type { CellColumnId, MultiColumn } from "@/utils/id-tree";import { invariant } from "@/utils/invariant";-import { getNotebook } from "../../core/cells/cells";-import { findColumn, layoutAtom } from "../../core/cells/layout";+import { findColumn, isColumnId, layoutAtom } from "../../core/cells/layout";import { store } from "../../core/state/jotai";import { useCellActions } from "../../core/cells/actions";import { useEvent } from "../../hooks/useEvent";@@ -50,9 +48,6 @@ const SortableCellsProviderInternal = ({useCellActions();const [activeId, setActiveId] = useState<UniqueIdentifier | null>(null);- const [clonedItems, setClonedItems] = useState<MultiColumn<CellId> | null>(- null,- );const config = useAppConfig();const modifiers = useMemo(() => {@@ -76,20 +71,10 @@ const SortableCellsProviderInternal = ({const handleDragStart = useEvent((event: DragStartEvent) => {setActiveId(event.active.id);- const notebook = getNotebook();- setClonedItems(notebook.cellIds);});const handleDragCancel = useEvent(() => {- // TODO: restore cloned items- if (clonedItems) {- // Reset items to their original state in case items have been- // Dragged across containers- // setItems(clonedItems);- }-setActiveId(null);- setClonedItems(null);});/**@@ -236,9 +221,5 @@ const SortableCellsProviderInternal = ({export const SortableCellsProvider = React.memo(SortableCellsProviderInternal);function isCellId(id: UniqueIdentifier): id is CellId {- return typeof id === "string" && !id.startsWith("tree_");-}--function isColumnId(id: UniqueIdentifier): id is CellColumnId {- return typeof id === "string" && id.startsWith("tree_");+ return typeof id === "string" && !isColumnId(id);}diff --git a/frontend/src/components/tracing/tracing.tsx b/frontend/src/components/tracing/tracing.tsxindex fb53d9664a..5764b266a8 100644--- a/frontend/src/components/tracing/tracing.tsx+++ b/frontend/src/components/tracing/tracing.tsx@@ -209,7 +209,7 @@ const TraceBlockBody: React.FC<{const elapsedTime = cellRun.elapsedTime ?? 0;return {cell: cellRun.cellId,- cellNum: cellIds.inOrderIds.indexOf(cellRun.cellId),+ cellNum: cellIds.indexOf(cellRun.cellId),startTimestamp: formatChartTime(cellRun.startTime),endTimestamp: formatChartTime(cellRun.startTime + elapsedTime),elapsedTime: formatElapsedTime(elapsedTime * 1000),diff --git a/frontend/src/core/ai/__tests__/staged-cells.test.ts b/frontend/src/core/ai/__tests__/staged-cells.test.tsindex e362226235..9f1c8d897d 100644--- a/frontend/src/core/ai/__tests__/staged-cells.test.ts+++ b/frontend/src/core/ai/__tests__/staged-cells.test.ts@@ -43,7 +43,7 @@ vi.mock("../../cells/actions", () => ({vi.mock("../../cells/cells", async () => {const { atom } = await import("jotai");return {- notebookAtom: atom({ cellData: {}, cellIds: { inOrderIds: [] } }),+ notebookAtom: atom({ cellData: {}, cellIds: [] }),cellHandleAtom: vi.fn(() => ({read: vi.fn(() => mockCellHandle),})),diff --git a/frontend/src/core/ai/context/providers/__tests__/cell-output.test.ts b/frontend/src/core/ai/context/providers/__tests__/cell-output.test.tsindex 50d4b42101..0207b45b64 100644--- a/frontend/src/core/ai/context/providers/__tests__/cell-output.test.ts+++ b/frontend/src/core/ai/context/providers/__tests__/cell-output.test.ts@@ -34,9 +34,7 @@ describe("CellOutputContextProvider", () => {beforeEach(() => {// Create a basic mock notebook statemockNotebook = {- cellIds: {- inOrderIds: ["cell1", "cell2", "cell3"],- },+ cellIds: ["cell1", "cell2", "cell3"],cellData: {cell1: {id: "cell1",@@ -207,7 +205,7 @@ describe("Cell output utility functions", () => {beforeEach(() => {const mockNotebook = {- cellIds: { inOrderIds: [] },+ cellIds: [],cellData: {},cellRuntime: {},} as unknown as NotebookState;@@ -226,7 +224,7 @@ describe("Cell output utility functions", () => {for (const testCase of testCases) {mockStore = createMockStore({- cellIds: { inOrderIds: ["test"] },+ cellIds: ["test"],cellData: {test: { id: "test", name: "", code: "" },},@@ -261,7 +259,7 @@ describe("Cell output utility functions", () => {for (const testCase of testCases) {mockStore = createMockStore({- cellIds: { inOrderIds: ["test"] },+ cellIds: ["test"],cellData: {test: { id: "test", name: "", code: "" },},@@ -295,7 +293,7 @@ describe("Cell output utility functions", () => {for (const testCase of testCases) {mockStore = createMockStore({- cellIds: { inOrderIds: ["test"] },+ cellIds: ["test"],cellData: {test: { id: "test", name: "", code: "" },},@@ -338,7 +336,7 @@ describe("Cell output utility functions", () => {} as unknown as Document;const mockNotebook = {- cellIds: { inOrderIds: [] },+ cellIds: [],cellData: {},cellRuntime: {},} as unknown as NotebookState;@@ -369,7 +367,7 @@ describe("Cell output utility functions", () => {).mockReturnValue(mockDiv);mockStore = createMockStore({- cellIds: { inOrderIds: ["test"] },+ cellIds: ["test"],cellData: {test: { id: "test", name: "", code: "" },},@@ -395,9 +393,7 @@ describe("Cell output utility functions", () => {beforeEach(() => {mockNotebook = {- cellIds: {- inOrderIds: ["cell1", "cell2"],- },+ cellIds: ["cell1", "cell2"],cellData: {cell1: {id: "cell1",diff --git a/frontend/src/core/ai/context/providers/cell-output.ts b/frontend/src/core/ai/context/providers/cell-output.tsindex 6e51c41b48..85c047ba48 100644--- a/frontend/src/core/ai/context/providers/cell-output.ts+++ b/frontend/src/core/ai/context/providers/cell-output.ts@@ -93,7 +93,7 @@ export class CellOutputContextProvider extends AIContextProvider<CellOutputConteconst notebook = this.store.get(notebookAtom);const items: CellOutputContextItem[] = [];- for (const cellId of notebook.cellIds.inOrderIds) {+ for (const cellId of notebook.cellIds) {const cellContextData = getCellContextData(cellId, notebook);const cellOutput = cellContextData.cellOutput;@@ -358,7 +358,7 @@ export function getCellContextData(const cellRuntime = notebook.cellRuntime[cellId];const cellData = notebook.cellData[cellId];- const cellIndex = notebook.cellIds.inOrderIds.indexOf(cellId);+ const cellIndex = notebook.cellIds.indexOf(cellId);const cellName = displayCellName(cellData.name, cellIndex);let consoleOutputs: BaseOutput[] | undefined;diff --git a/frontend/src/core/ai/tools/__tests__/edit-notebook-tool.test.ts b/frontend/src/core/ai/tools/__tests__/edit-notebook-tool.test.tsindex 81e9e43448..4e25d07a59 100644--- a/frontend/src/core/ai/tools/__tests__/edit-notebook-tool.test.ts+++ b/frontend/src/core/ai/tools/__tests__/edit-notebook-tool.test.ts@@ -4,12 +4,18 @@ import { EditorState } from "@codemirror/state";import { EditorView } from "@codemirror/view";import { getDefaultStore } from "jotai";import { beforeEach, describe, expect, it, vi } from "vitest";+import { mockDocumentModel } from "@/__mocks__/document";import { MockNotebook } from "@/__mocks__/notebook";import { cellId } from "@/__tests__/branded";import { notebookAtom } from "@/core/cells/cells";import type { CellId } from "@/core/cells/ids";import { OverridingHotkeyProvider } from "@/core/hotkeys/hotkeys";-import { MultiColumn } from "@/utils/id-tree";+import {+ cell,+ CONFIG,+ document as documentOf,+ withCells,+} from "@/core/notebook/document/__tests__/fixtures";import { cellConfigExtension } from "../../../codemirror/config/extension";import { adaptiveLanguageConfiguration } from "../../../codemirror/language/extension";import { stagedAICellsAtom } from "../../staged-cells";@@ -61,7 +67,6 @@ describe("EditNotebookTool", () => {let tool: EditNotebookTool;let cellId1: CellId;let cellId2: CellId;- let cellId3: CellId;let toolContext: {addStagedCell: ReturnType<typeof vi.fn>;createCell: ReturnType<typeof vi.fn>;@@ -86,7 +91,6 @@ describe("EditNotebookTool", () => {cellId1 = cellId("cell-1");cellId2 = cellId("cell-2");- cellId3 = cellId("cell-3");// Reset mocksvi.clearAllMocks();@@ -443,27 +447,37 @@ describe("EditNotebookTool", () => {[cellId2]: { code: "x = 2" },},});- // Create multi-column layout- notebook.cellIds = MultiColumn.from([[cellId1], [cellId2]]);store.set(notebookAtom, notebook);-- const newCode = "y = 2";- const result = await tool.handler(- {- edit: {- type: "add_cell",- position: { type: "column_end", columnIndex: 1 },- code: newCode,- },- },- toolContext as never,+ // The layout reads columns from the document's cells.+ const { stop } = mockDocumentModel(+ store,+ withCells(documentOf(), {+ c1: null,+ c2: null,+ [cellId1]: cell(cellId1, "x = 1"),+ [cellId2]: cell(cellId2, "x = 2", {+ config: { ...CONFIG, column: 1 },+ }),+ }),);- expect(result.status).toBe("success");+ try {+ const result = await tool.handler(+ {+ edit: {+ type: "add_cell",+ position: { type: "column_end", columnIndex: 1 },+ code: "y = 2",+ },+ },+ toolContext as never,+ );- // Check that a new cell was staged- const stagedCells = store.get(stagedAICellsAtom);- expect(stagedCells.size).toBe(1);+ expect(result.status).toBe("success");+ expect(store.get(stagedAICellsAtom).size).toBe(1);+ } finally {+ stop();+ }});it("should throw error when cell ID doesn't exist for position", async () => {@@ -698,52 +712,6 @@ describe("EditNotebookTool", () => {});});- describe("validation", () => {- it("should validate cell exists in multi-column notebook", async () => {- const notebook = MockNotebook.notebookState({- cellData: {- [cellId1]: { code: "x = 1" },- [cellId2]: { code: "x = 2" },- [cellId3]: { code: "x = 3" },- },- });- // Create multi-column layout- notebook.cellIds = MultiColumn.from([[cellId1, cellId2], [cellId3]]);- store.set(notebookAtom, notebook);-- // Should not throw for cells in different columns- const editorView = createMockEditorView("x = 1");- notebook.cellHandles[cellId1] = { current: { editorView } } as never;- notebook.cellHandles[cellId3] = { current: { editorView } } as never;-- await expect(- tool.handler(- {- edit: {- type: "update_cell",- position: { cellId: cellId1 },- code: "y = 1",- },- },- toolContext as never,- ),- ).resolves.toBeDefined();-- await expect(- tool.handler(- {- edit: {- type: "update_cell",- position: { cellId: cellId3 },- code: "y = 3",- },- },- toolContext as never,- ),- ).resolves.toBeDefined();- });- });-describe("return value", () => {it("should return success status with next steps", async () => {const notebook = MockNotebook.notebookState({diff --git a/frontend/src/core/ai/tools/edit-notebook-tool.ts b/frontend/src/core/ai/tools/edit-notebook-tool.tsindex 6beb2f7b8b..1b833d6714 100644--- a/frontend/src/core/ai/tools/edit-notebook-tool.ts+++ b/frontend/src/core/ai/tools/edit-notebook-tool.ts@@ -7,7 +7,11 @@ import { type NotebookState, notebookAtom } from "@/core/cells/cells";import type { CreateCellOptions } from "@/core/cells/actions";import { CellId } from "@/core/cells/ids";import { prepareCellCodeUpdate } from "@/core/codemirror/language/cell-code";-import type { CellColumnId } from "@/utils/id-tree";+import {+ type CellColumnId,+ type Layout,+ layoutAtom,+} from "@/core/cells/layout";import { stagedAICellsAtom } from "../staged-cells";import {type AiTool,@@ -201,7 +205,10 @@ export class EditNotebookTool implements AiTool<}placement = {- endOf: this.getColumnId(position.columnIndex, notebook),+ endOf: this.getColumnId(+ position.columnIndex,+ store.get(layoutAtom),+ ),};break;@@ -259,8 +266,7 @@ export class EditNotebookTool implements AiTool<};private validateCellIdExists(cellId: CellId, notebook: NotebookState) {- const cellIds = notebook.cellIds;- if (!cellIds.getColumns().some((column) => column.idSet.has(cellId))) {+ if (!notebook.cellIds.includes(cellId)) {throw new ToolExecutionError("Cell not found", {code: "CELL_NOT_FOUND",isRetryable: false,@@ -269,12 +275,8 @@ export class EditNotebookTool implements AiTool<}}- private getColumnId(- columnIndex: number,- notebook: NotebookState,- ): CellColumnId {- const cellIds = notebook.cellIds;- const columns = cellIds.getColumns();+ private getColumnId(columnIndex: number, layout: Layout): CellColumnId {+ const { columns } = layout;if (columnIndex < 0 || columnIndex >= columns.length) {throw new ToolExecutionError("Column index is out of range", {diff --git a/frontend/src/core/cells/__tests__/actions.test.ts b/frontend/src/core/cells/__tests__/actions.test.tsindex e2b7015795..f0ed49d94d 100644--- a/frontend/src/core/cells/__tests__/actions.test.ts+++ b/frontend/src/core/cells/__tests__/actions.test.ts@@ -5,7 +5,6 @@ import { afterEach, describe, expect, it, vi } from "vitest";import { cellId } from "@/__tests__/branded";import { createStore } from "jotai";import { cell, CONFIG } from "@/core/notebook/document/__tests__/fixtures";-import type { CellIndex } from "@/utils/id-tree";import {addSetupCellAtom,collapseAllCellsAtom,@@ -17,9 +16,11 @@ import {focusCellAtom,moveToNextCellAtom,showCellIfHiddenAtom,+ splitCellAtom,undoDeleteCellAtom,+ undoSplitCellAtom,} from "../actions";-import { moveCellsRelativeToAtom } from "../arrange";+import { moveCellAtom, moveCellsRelativeToAtom } from "../arrange";import {cellDataAtom,cellHandleAtom,@@ -95,7 +96,7 @@ describe("createCellAtom", () => {});it("names the column of a cell placed first in a later column", () => {- const { store, notebook, cellIn } = setup({+ const { store, cellIn } = setup({c2: cell("c2", "y = 2", { config: { ...CONFIG, column: 1 } }),});@@ -103,12 +104,14 @@ describe("createCellAtom", () => {expect(cellIn(id as string)?.config.column).toBe(1);expect(cellIn(c2)?.config.column).toBeNull();- expect(notebook().cellIds.atOrThrow(1).inOrderIds).toEqual([id, c2]);- expect(notebook().cellIds.atOrThrow(0).inOrderIds).toEqual([c1]);+ expect(store.get(layoutAtom).columns.map((c) => c.ids)).toEqual([+ [c1],+ [id, c2],+ ]);});it("places a cell in the column of its anchor, whatever config it carries", () => {- const { store, order, notebook, cellIn } = setup({+ const { store, order, cellIn } = setup({c2: cell("c2", "y = 2", { config: { ...CONFIG, column: 1 } }),});@@ -124,21 +127,23 @@ describe("createCellAtom", () => {hide_code: true,column: null,});- expect(notebook().cellIds.atOrThrow(1).inOrderIds).toEqual([c2, id]);+ expect(store.get(layoutAtom).columns[1].ids).toEqual([c2, id]);});- it("names the column when appending to an empty column", () => {- const { store, notebook, cellIn } = setup();- store.set(notebookAtom, (state) => ({- ...state,- cellIds: state.cellIds.addColumn(state.cellIds.atOrThrow(0).id),- }));- const empty = notebook().cellIds.atOrThrow(1).id;+ it("fills an emptied column, which keeps its id", () => {+ const { store, columns, columnIds, cellIn } = setup({+ c3: cell("c3", "z = 3", { config: { ...CONFIG, column: 1 } }),+ });+ const [first, second] = columnIds();+ // Moving its only cell out leaves the column on the page, to add to.+ store.set(moveCellAtom, { cellId: c3, direction: "left" });+ expect(columns()).toEqual([[c3, c1, c2], []]);- const id = store.set(createCellAtom, { endOf: empty });+ const id = store.set(createCellAtom, { endOf: second });expect(cellIn(id as string)?.config.column).toBe(1);- expect(notebook().cellIds.atOrThrow(1).inOrderIds).toEqual([id]);+ expect(columns()).toEqual([[c3, c1, c2], [id]]);+ expect(columnIds()).toEqual([first, second]);});it("creates nothing when a cell already has the code", () => {@@ -161,6 +166,40 @@ describe("createCellAtom", () => {});});+describe("splitCellAtom", () => {+ it("splits a cell at the cursor and undoes it", () => {+ const { store, order, notebook, cellIn, mountEditor } = setup({+ c1: cell("c1", "line1\nline2"),+ });+ const view = mountEditor(c1);+ view.dispatch({ selection: { anchor: 5 } });++ const id = store.set(splitCellAtom, { cellId: c1 }) as CellId;++ expect(order()).toEqual([c1, id, c2]);+ expect(cellIn(c1)?.code).toBe("line1");+ expect(cellIn(id)?.code).toBe("line2");+ expect(view.state.doc.toString()).toBe("line1");+ expect(notebook().scrollKey).toBe(id);++ store.set(undoSplitCellAtom, {+ cellId: c1,+ newCellId: id,+ snapshot: "line1\nline2",+ });++ expect(order()).toEqual([c1, c2]);+ expect(cellIn(c1)?.code).toBe("line1\nline2");+ expect(view.state.doc.toString()).toBe("line1\nline2");+ });++ it("does nothing for a cell without an editor", () => {+ const { store, order } = setup();+ expect(store.set(splitCellAtom, { cellId: c1 })).toBeNull();+ expect(order()).toEqual([c1, c2]);+ });+});+describe("moveToNextCellAtom", () => {it("focuses the neighbouring cell within the column", () => {const { store, order } = setup();@@ -218,14 +257,14 @@ describe("addSetupCellAtom", () => {});it("places the setup cell in an empty first column", () => {- const { store, notebook, cellIn } = setup({+ const { store, columns, notebook, cellIn } = setup({c1: cell(c1, "x = 1", { config: { ...CONFIG, column: 1 } }),});- expect(notebook().cellIds.atOrThrow(0).inOrderIds).toEqual([]);+ expect(columns()[0]).toEqual([]);store.set(addSetupCellAtom, { code: "import marimo as mo" });- expect(notebook().cellIds.atOrThrow(0).inOrderIds).toEqual([SETUP_CELL_ID]);+ expect(columns()[0]).toEqual([SETUP_CELL_ID]);expect(cellIn(SETUP_CELL_ID)?.config.column).toBe(0);expect(notebook().scrollKey).toBe(SETUP_CELL_ID);});@@ -297,16 +336,16 @@ describe("deleteCellsAtom", () => {});it("names the column of a cell that followed a deleted column start", () => {- const { store, notebook, cellIn } = setup({+ const { store, cellIn } = setup({c2: cell("c2", "y = 2", { config: { ...CONFIG, column: 1 } }),c3: cell("c3", "z = 3"),});- expect(notebook().cellIds.atOrThrow(1).inOrderIds).toEqual([c2, c3]);+ expect(store.get(layoutAtom).columns[1].ids).toEqual([c2, c3]);store.set(deleteCellsAtom, { cellIds: [c2] });expect(cellIn(c3)?.config.column).toBe(1);- expect(notebook().cellIds.atOrThrow(1).inOrderIds).toEqual([c3]);+ expect(store.get(layoutAtom).columns[1].ids).toEqual([c3]);});it("forgets a deleted cell's editor-only state and its atoms", () => {@@ -386,14 +425,14 @@ describe("undoDeleteCellAtom", () => {const { store, order, notebook } = setup({c3: cell("c3", "z = 3"),});- const column = notebook().cellIds.atOrThrow(0);+ const column = store.get(layoutAtom).columns[0];store.set(moveCellsRelativeToAtom, {cellIds: [c1, c2],targetCellId: c3,position: "after",previousPlacements: [- { columnId: column.id, index: 0 as CellIndex },- { columnId: column.id, index: 1 as CellIndex },+ { columnId: column.id, index: 0 },+ { columnId: column.id, index: 1 },],});expect(order()).toEqual([c3, c1, c2]);diff --git a/frontend/src/core/cells/__tests__/arrange.test.ts b/frontend/src/core/cells/__tests__/arrange.test.tsindex 27ca8232a9..484e11fa4e 100644--- a/frontend/src/core/cells/__tests__/arrange.test.ts+++ b/frontend/src/core/cells/__tests__/arrange.test.ts@@ -3,15 +3,25 @@import type * as Protocol from "@marimo-team/client/protocol";import { afterEach, describe, expect, it, vi } from "vitest";import { cellId } from "@/__tests__/branded";-import { cell, CONFIG } from "@/core/notebook/document/__tests__/fixtures";-import type { CellIndex } from "@/utils/id-tree";+import {+ cell,+ CONFIG,+ document as documentOf,+ withCells,+} from "@/core/notebook/document/__tests__/fixtures";import { collapseCellAtom, undoDeleteCellAtom } from "../actions";import {+ addColumnAtom,+ addColumnBreakpointAtom,+ compactColumnsAtom,+ deleteColumnAtom,dropCellOverCellAtom,dropCellOverColumnAtom,+ mergeAllColumnsAtom,moveCellAtom,moveCellsRelativeToAtom,moveCellToIndexAtom,+ moveColumnAtom,sendToBottomAtom,sendToTopAtom,} from "../arrange";@@ -220,8 +230,8 @@ describe("moveCellsRelativeToAtom", () => {targetCellId: c3,position: "after",previousPlacements: [- { columnId: column, index: 0 as CellIndex },- { columnId: column, index: 1 as CellIndex },+ { columnId: column, index: 0 },+ { columnId: column, index: 1 },],});expect(order()).toEqual([c3, c1, c2]);@@ -261,3 +271,145 @@ describe("moveCellsRelativeToAtom", () => {expect(order()).toEqual([c2, c3, c1]);});});++describe("columns", () => {+ it("adds a column after another with a new cell, and names it", () => {+ const { store, columns, columnIds, notebook, cellIn } = setup();+ const [first] = columnIds();++ store.set(addColumnAtom, { columnId: first });++ const [left, right] = store.get(layoutAtom).columns;+ expect(left.id).toBe(first);+ expect(right.id).not.toBe(first);+ expect(right.ids).toHaveLength(1);+ const id = right.ids[0];+ expect(columns()).toEqual([[c1, c2], [id]]);+ expect(cellIn(id)?.config.column).toBe(1);+ expect(notebook().scrollKey).toBe(id);+ });++ it("starts a column at a cell, but not at the first cell", () => {+ const { store, model, columns, cellIn } = setup({+ c3: cell("c3", "z = 3"),+ });+ const before = store.get(model.documentAtom);++ store.set(addColumnBreakpointAtom, { cellId: c1 });+ expect(store.get(model.documentAtom)).toBe(before);++ store.set(addColumnBreakpointAtom, { cellId: c2 });+ expect(columns()).toEqual([[c1], [c2, c3]]);+ expect(cellIn(c2)?.config.column).toBe(1);+ });++ it("starts a column at a column's first cell, keeping the id with the cells", () => {+ const { store, columns, columnIds } = twoColumns();+ const [first, second] = columnIds();++ store.set(addColumnBreakpointAtom, { cellId: c3 });++ expect(columns()).toEqual([[c1, c2], [], [c3, c4]]);+ const [left, , right] = columnIds();+ expect(left).toBe(first);+ expect(right).toBe(second);+ });++ it("deletes a column into the one before it, or after it for the first", () => {+ const { store, columns, columnIds, cellIn } = twoColumns();+ const [first, second] = columnIds();++ store.set(deleteColumnAtom, { columnId: first });+ expect(columns()).toEqual([[c1, c2, c3, c4]]);+ expect(cellIn(c3)?.config.column).toBeNull();+ expect(columnIds()).toEqual([second]);++ store.set(deleteColumnAtom, { columnId: second });+ expect(columns()).toEqual([[c1, c2, c3, c4]]);+ });++ it("merges every column into the first", () => {+ const { store, columns, columnIds, cellIn } = twoColumns();+ const [first] = columnIds();++ store.set(mergeAllColumnsAtom);++ expect(columns()).toEqual([[c1, c2, c3, c4]]);+ expect(cellIn(c3)?.config.column).toBeNull();+ expect(columnIds()).toEqual([first]);+ });++ it("keeps the column a drag emptied until a drop compacts it", () => {+ const { store, columns, columnIds, cellIn } = twoColumns();+ const [first, second] = columnIds();+ store.set(dropCellOverColumnAtom, { cellId: c3, columnId: first });+ store.set(dropCellOverColumnAtom, { cellId: c4, columnId: first });+ expect(columns()).toEqual([[c4, c3, c1, c2], []]);+ expect(columnIds()).toEqual([first, second]);+ expect(cellIn(c4)?.config.column).toBeNull();++ store.set(dropCellOverColumnAtom, { cellId: c3, columnId: second });+ expect(columns()).toEqual([[c4, c1, c2], [c3]]);+ expect(cellIn(c3)?.config.column).toBe(1);++ store.set(dropCellOverColumnAtom, { cellId: c3, columnId: first });+ store.set(compactColumnsAtom);++ expect(columns()).toEqual([[c3, c4, c1, c2]]);+ expect(columnIds()).toEqual([first]);+ });++ it("moves a column, keeping its id with it", () => {+ const { store, columns, columnIds, cellIn } = twoColumns();+ const [first, second] = columnIds();++ store.set(moveColumnAtom, { column: first, overColumn: second });+ expect(columns()).toEqual([+ [c3, c4],+ [c1, c2],+ ]);+ expect(columnIds()).toEqual([second, first]);+ expect(cellIn(c3)?.config.column).toBe(0);+ expect(cellIn(c1)?.config.column).toBe(1);++ store.set(moveColumnAtom, { column: first, overColumn: "_left_" });+ expect(columns()).toEqual([+ [c1, c2],+ [c3, c4],+ ]);+ expect(columnIds()).toEqual([first, second]);+ });+});++describe("the host's columns", () => {+ it("names a column the host adds, leaving the others as they are", () => {+ const { host, columns, columnIds } = setup();+ const [first] = columnIds();++ host(+ withCells(documentOf(), {+ c3: cell("c3", "z = 3", { config: { ...CONFIG, column: 1 } }),+ }),+ );++ expect(columns()).toEqual([[c1, c2], [c3]]);+ const [left, right] = columnIds();+ expect(left).toBe(first);+ expect(right).not.toBe(first);+ });++ it("drops a column the host removed", () => {+ const { host, columns, columnIds } = twoColumns();+ const [first] = columnIds();++ host(+ withCells(documentOf(), {+ c3: cell("c3", "z = 3"),+ c4: cell("c4", "w = 4"),+ }),+ );++ expect(columns()).toEqual([[c1, c2, c3, c4]]);+ expect(columnIds()).toEqual([first]);+ });+});diff --git a/frontend/src/core/cells/__tests__/cells.test.ts b/frontend/src/core/cells/__tests__/cells.test.tsindex da87c14655..11b6cb6595 100644--- a/frontend/src/core/cells/__tests__/cells.test.ts+++ b/frontend/src/core/cells/__tests__/cells.test.ts@@ -24,11 +24,6 @@ import { adaptiveLanguageConfiguration } from "@/core/codemirror/language/extensimport { OverridingHotkeyProvider } from "@/core/hotkeys/hotkeys";import type { OutputMessage } from "@/core/kernel/messages";import type { CellConfig } from "@/core/network/types";-import {- type CollapsibleTree,- MultiColumn,- type CellColumnId,-} from "@/utils/id-tree";import type { Seconds } from "@/utils/time";import {exportedForTesting,@@ -61,8 +56,6 @@ vi.mock("../scrollCellIntoView", async (importOriginal) => {};});-const FIRST_COLUMN = 0;-const { initialNotebookState, reducer, createActions } = exportedForTesting;function formatCells(notebook: NotebookState) {@@ -78,14 +71,7 @@ function formatCells(notebook: NotebookState) {const cells = cellsId.map((cellId) => cellData[cellId]);return cells.map((cell) => printCell(cell)).join("\n\n");};- const printColumn = (column: CollapsibleTree<CellId>, columnIdx: number) => {- return `> col ${columnIdx}\n${printCells(column.inOrderIds)}`;- };- const columns = cellIds.getColumns();- if (columns.length > 1) {- return wrap(columns.map(printColumn).join("\n\n"));- }- return wrap(printCells(cellIds.inOrderIds));+ return wrap(printCells(cellIds));}function createEditor(content: string) {@@ -152,7 +138,7 @@ describe("cell reducer", () => {hideCode,autoFocus = true,}: {- cellId: CellId | "__end__" | { type: "__end__"; columnId: CellColumnId };+ cellId: CellId | "__end__";before: boolean;code?: string;name?: string;@@ -161,26 +147,16 @@ describe("cell reducer", () => {hideCode?: boolean;autoFocus?: boolean;}): CellId {- let column: CollapsibleTree<CellId>;- let cellIndex: number;- if (cellId === "__end__") {- column = state.cellIds.atOrThrow(0);- cellIndex = column.length;- } else if (typeof cellId === "string") {- column = state.cellIds.findWithId(cellId);- cellIndex = column.topLevelIds.indexOf(cellId);- } else {- column = state.cellIds.get(cellId.columnId) ?? state.cellIds.atOrThrow(0);- cellIndex = column.length;- }+ const cellIndex =+ cellId === "__end__"+ ? state.cellIds.length+ : state.cellIds.indexOf(cellId);const id = newCellId ?? CellId.create();+ const cellIds = [...state.cellIds];+ cellIds.splice(before ? cellIndex : cellIndex + 1, 0, id);state = {...state,- cellIds: state.cellIds.insertId(- id,- column.id,- before ? cellIndex : cellIndex + 1,- ),+ cellIds,cellData: {...state.cellData,[id]: createCell({@@ -215,9 +191,8 @@ describe("cell reducer", () => {i = 0;state = initialNotebookState();- state.cellIds = MultiColumn.from([]);seedCell({ cellId: "__end__", before: false });- firstCellId = state.cellIds.inOrderIds[0];+ firstCellId = state.cellIds[0];});afterAll(() => {@@ -1156,37 +1131,6 @@ describe("cell reducer", () => {expect(state.cellLogs).toEqual([]);});- it("can split and undo split cells", () => {- seedCell({- cellId: firstCellId,- before: false,- code: "line1\nline2",- });- const nextCellId = state.cellIds.atOrThrow(FIRST_COLUMN).atOrThrow(1);-- const originalCellCount = state.cellIds.atOrThrow(FIRST_COLUMN).length;- // Move cursor to the second line- const editor = state.cellHandles[nextCellId].current?.editorView;- if (!editor) {- throw new Error("Editor not found");- }- editor.dispatch({ selection: { anchor: 5, head: 5 } });- actions.splitCell({ cellId: nextCellId });- expect(state.cellIds.atOrThrow(FIRST_COLUMN).length).toBe(- originalCellCount + 1,- );- expect(state.cellData[nextCellId].code).toBe("line1");- expect(- state.cellData[state.cellIds.atOrThrow(FIRST_COLUMN).atOrThrow(2)].code,- ).toBe("line2");-- actions.undoSplitCell({ cellId: nextCellId, snapshot: "line1\nline2" });- expect(state.cellIds.atOrThrow(FIRST_COLUMN).length).toBe(- originalCellCount,- );- expect(state.cellData[nextCellId].code).toBe("line1\nline2");- });-it("can handle multiple console outputs", () => {const STDOUT1: OutputMessage = {channel: "stdout",@@ -1223,228 +1167,6 @@ describe("cell reducer", () => {`);});- it("can add a column breakpoint", () => {- seedCell({ cellId: firstCellId, before: false });- seedCell({ cellId: cellId("1"), before: false });- seedCell({ cellId: cellId("2"), before: false });-- expect(state.cellIds.getColumns().length).toBe(1);- expect(formatCells(state)).toMatchInlineSnapshot(`- "- [0] ''-- [1] ''-- [2] ''-- [3] ''- "- `);-- actions.addColumnBreakpoint({ cellId: cellId("2") });-- expect(state.cellIds.getColumns().length).toBe(2);- expect(formatCells(state)).toMatchInlineSnapshot(`- "- > col 0- [0] ''-- [1] ''-- > col 1- [2] ''-- [3] ''- "- `);-- // Check that the cells are in the correct columns- expect(state.cellIds.getColumns()[0].topLevelIds).toEqual([- cellId("0"),- cellId("1"),- ]);- expect(state.cellIds.getColumns()[1].topLevelIds).toEqual([- cellId("2"),- cellId("3"),- ]);- });-- it("cannot add a column breakpoint before the first cell", () => {- expect(state.cellIds.getColumns().length).toBe(1);- seedCell({ cellId: firstCellId, before: false });- seedCell({ cellId: cellId("1"), before: false });- actions.addColumnBreakpoint({ cellId: firstCellId });- expect(state.cellIds.getColumns().length).toBe(1);- });-- it("can delete a column", () => {- seedCell({ cellId: firstCellId, before: false });- seedCell({ cellId: cellId("1"), before: false });- seedCell({ cellId: cellId("2"), before: false });- actions.addColumnBreakpoint({ cellId: cellId("2") });-- expect(state.cellIds.getColumns().length).toBe(2);- expect(formatCells(state)).toMatchInlineSnapshot(`- "- > col 0- [0] ''-- [1] ''-- > col 1- [2] ''-- [3] ''- "- `);-- const columnId = state.cellIds.atOrThrow(0).id;- actions.deleteColumn({ columnId: columnId });-- expect(state.cellIds.getColumns().length).toBe(1);- expect(formatCells(state)).toMatchInlineSnapshot(`- "- [2] ''-- [3] ''-- [0] ''-- [1] ''- "- `);-- // Check that all cells are now in the remaining column- expect(state.cellIds.getColumns()[0].topLevelIds).toEqual([- "2",- "3",- "0",- "1",- ]);- });-- it("deleting the last column does nothing", () => {- seedCell({ cellId: firstCellId, before: false });- seedCell({ cellId: cellId("1"), before: false });-- const initialState = { ...state };-- actions.deleteColumn({ columnId: initialState.cellIds.atOrThrow(0).id });-- // State should not change- expect(state).toEqual(initialState);- });-- it("can drop a column over another column", () => {- seedCell({ cellId: firstCellId, before: false });- seedCell({ cellId: cellId("1"), before: false });- seedCell({ cellId: cellId("2"), before: false });- actions.addColumnBreakpoint({ cellId: cellId("2") });-- expect(state.cellIds.getColumns().length).toBe(2);- expect(formatCells(state)).toMatchInlineSnapshot(`- "- > col 0- [0] ''-- [1] ''-- > col 1- [2] ''-- [3] ''- "- `);-- const columnId0 = state.cellIds.atOrThrow(0).id;- const columnId1 = state.cellIds.atOrThrow(1).id;- expect(columnId0).not.toBe(columnId1);- actions.moveColumn({ column: columnId1, overColumn: columnId0 });-- expect(state.cellIds.getColumns().length).toBe(2);- expect(formatCells(state)).toMatchInlineSnapshot(`- "- > col 0- [2] ''-- [3] ''-- > col 1- [0] ''-- [1] ''- "- `);-- // Check that the columns have swapped positions- expect(state.cellIds.getColumns()[0].topLevelIds).toEqual([- cellId("2"),- cellId("3"),- ]);- expect(state.cellIds.getColumns()[1].topLevelIds).toEqual([- cellId("0"),- cellId("1"),- ]);- });-- it("can compact columns", () => {- // Create initial state with 3 columns, including an empty one- seedCell({ cellId: firstCellId, before: false });- seedCell({ cellId: cellId("1"), before: false });- actions.addColumnBreakpoint({ cellId: cellId("1") });- actions.addColumnBreakpoint({ cellId: cellId("2") });- // A column emptied by a drag stays until the drop compacts it.- state = {- ...state,- cellIds: state.cellIds.addColumn(state.cellIds.getColumnIds()[1]),- };-- expect(state.cellIds.getColumns().length).toBe(4);- expect(formatCells(state)).toMatchInlineSnapshot(`- "- > col 0- [0] ''-- > col 1- [1] ''-- > col 2--- > col 3- [2] ''- "- `);-- // Check initial column structure- expect(state.cellIds.getColumns()[0].topLevelIds).toEqual([cellId("0")]);- expect(state.cellIds.getColumns()[1].topLevelIds).toEqual([cellId("1")]);- expect(state.cellIds.getColumns()[2].topLevelIds).toEqual([]);- expect(state.cellIds.getColumns()[3].topLevelIds).toEqual([cellId("2")]);-- // Compact columns- actions.compactColumns();-- expect(state.cellIds.getColumns().length).toBe(3);- expect(formatCells(state)).toMatchInlineSnapshot(`- "- > col 0- [0] ''-- > col 1- [1] ''-- > col 2- [2] ''- "- `);-- // Check compacted column structure- expect(state.cellIds.getColumns()[0].topLevelIds).toEqual([cellId("0")]);- expect(state.cellIds.getColumns()[1].topLevelIds).toEqual([cellId("1")]);- expect(state.cellIds.getColumns()[2].topLevelIds).toEqual([cellId("2")]);- });-it("can clear output of a single cell", () => {// Set up initial state with outputactions.handleCellMessage({@@ -1534,7 +1256,7 @@ describe("cell reducer", () => {it("can clear output of all cells", () => {// Create multiple cells with outputseedCell({ cellId: firstCellId, before: false });- const secondCellId = state.cellIds.atOrThrow(FIRST_COLUMN).atOrThrow(1);+ const secondCellId = state.cellIds[1];// Add output to both cellsconst outputMessage = {@@ -1616,7 +1338,7 @@ describe("cell reducer", () => {exportedForTesting;seedCell({ cellId: firstCellId, before: false });- const newCellId = state.cellIds.inOrderIds[1];+ const newCellId = state.cellIds[1];const dataAtom1 = cellDataAtom(newCellId);const runtimeAtom1 = cellRuntimeAtom(newCellId);diff --git a/frontend/src/core/cells/__tests__/document.test.ts b/frontend/src/core/cells/__tests__/document.test.tsindex d054806835..4480db451d 100644--- a/frontend/src/core/cells/__tests__/document.test.ts+++ b/frontend/src/core/cells/__tests__/document.test.ts@@ -46,7 +46,6 @@ const { initialNotebookState } = exportedForTesting;const a = cellId("a");const b = cellId("b");const c = cellId("c");-const d = cellId("d");function cell(id: CellId, patch: Partial<EditableCell> = {}): EditableCell {return {@@ -102,7 +101,7 @@ describe("applyDocument", () => {it("adds the document's cells to an empty notebook", () => {const state = notebookOf([cell(a), cell(b, { code: "" })]);- expect(state.cellIds.inOrderIds).toEqual([a, b]);+ expect(state.cellIds).toEqual([a, b]);expect(state.cellData[a]).toMatchObject({code: "a = 1",name: "_",@@ -199,42 +198,11 @@ describe("applyDocument", () => {const next = applyDocument(state, documentOf([cell(a), cell(c)]));- expect(next.cellIds.inOrderIds).toEqual([a, c]);+ expect(next.cellIds).toEqual([a, c]);expect(next.cellData[b]).toBeUndefined();expect(next.cellRuntime[b]).toBeUndefined();expect(next.cellHandles[b]).toBeUndefined();});-- it("regroups columns from the cells' configs, keeping column ids", () => {- const column1 = { ...cell(d).config, column: 1 };- const state = notebookOf([- cell(a),- cell(b),- cell(c),- cell(d, { config: column1 }),- ]);- const next = applyDocument(- state,- documentOf([cell(c), cell(a), cell(b), cell(d, { config: column1 })]),- );-- expect(next.cellIds.inOrderIds).toEqual([c, a, b, d]);- expect(next.cellIds.getColumnIds()).toEqual(state.cellIds.getColumnIds());- expect(next.cellIds.atOrThrow(1).inOrderIds).toEqual([d]);- });-- it("keeps a trailing empty column", () => {- const state = notebookOf([cell(a)]);- const withEmpty = {- ...state,- cellIds: state.cellIds.addColumn(state.cellIds.atOrThrow(0).id),- };-- const next = applyDocument(withEmpty, documentOf([cell(a, { code: "" })]));-- expect(next.cellIds.colLength).toBe(2);- expect(next.cellData[a].code).toBe("");- });});describe("documentEditMiddleware", () => {@@ -283,18 +251,6 @@ describe("documentEditMiddleware", () => {expect(calls).toEqual([["setCode", a, "a = 2"]]);});- it("clears columns merged into one", () => {- state = notebookOf([- cell(a),- cell(b),- cell(c, { config: { ...cell(c).config, column: 1 } }),- ]);-- actions.mergeAllColumns();-- expect(calls).toEqual([["updateConfig", c, { column: null }]]);- });-it("ignores the document and the kernel's run state", () => {actions.prepareForRun({ cellId: a });actions.setLastRun({diff --git a/frontend/src/core/cells/__tests__/harness.ts b/frontend/src/core/cells/__tests__/harness.tsindex d5de57d1b2..a55ec90bfe 100644--- a/frontend/src/core/cells/__tests__/harness.ts+++ b/frontend/src/core/cells/__tests__/harness.ts@@ -3,15 +3,20 @@/** A store whose notebook follows a document model, as the session wires it. */import { history } from "@codemirror/commands";+import { python } from "@codemirror/lang-python";import { EditorState } from "@codemirror/state";import { EditorView } from "@codemirror/view";import type * as Protocol from "@marimo-team/client/protocol";import { createStore } from "jotai";import { mockDocumentModel } from "@/__mocks__/document";+import type { CellId } from "@/core/cells/ids";+import { adaptiveLanguageConfiguration } from "@/core/codemirror/language/extension";+import { OverridingHotkeyProvider } from "@/core/hotkeys/hotkeys";import {document as documentOf,withCells,} from "@/core/notebook/document/__tests__/fixtures";+import type { Document } from "@/core/notebook/replica";import { exportedForTesting, notebookAtom, notebookReducer } from "../cells";import { followDocument } from "../document";import { layoutAtom } from "../layout";@@ -22,7 +27,7 @@ export function setupNotebook() {const store = createStore();store.set(notebookAtom, exportedForTesting.initialNotebookState());- const { model, stop } = mockDocumentModel(+ const { model, documentAtom, stop } = mockDocumentModel(store,cells ? withCells(documentOf(), cells) : documentOf(),);@@ -43,17 +48,36 @@ export function setupNotebook(return {store,model,- order: () => store.get(notebookAtom).cellIds.inOrderIds,+ /** Deliver a document from the host. Edits pending here stay on top. */+ host: (document: Document) => store.set(documentAtom, document),+ order: () => store.get(notebookAtom).cellIds,/** The cells of each column, as the page lays them out. */columns: () => store.get(layoutAtom).columns.map((column) => column.ids),+ /** The columns' ids, as the page keys them. */+ columnIds: () => store.get(layoutAtom).columns.map((column) => column.id),notebook: () => store.get(notebookAtom),cellIn: (id: string) => store.get(model.documentAtom)?.cells.get(id),- /** Give a cell an editor, as mounting it would. */+ /** Give a cell an editor, as mounting it would, with a cell's language setup. */mountEditor: (id: string) => {const view = new EditorView({state: EditorState.create({doc: store.get(model.documentAtom)?.cells.get(id)?.code ?? "",- extensions: [history()],+ extensions: [+ history(),+ python(),+ adaptiveLanguageConfiguration({+ cellId: id as CellId,+ completionConfig: {+ activate_on_typing: true,+ signature_hint_on_typing: false,+ copilot: false,+ codeium_api_key: null,+ },+ hotkeys: new OverridingHotkeyProvider({}),+ placeholderType: "marimo-import",+ lspConfig: {},+ }),+ ],}),});store.get(notebookAtom).cellHandles[id as never].current = {diff --git a/frontend/src/core/cells/__tests__/layout.test.ts b/frontend/src/core/cells/__tests__/layout.test.tsindex de285d69ba..ef3b836516 100644--- a/frontend/src/core/cells/__tests__/layout.test.ts+++ b/frontend/src/core/cells/__tests__/layout.test.ts@@ -1,12 +1,8 @@/* Copyright 2026 Marimo. All rights reserved. */-import { createStore } from "jotai";-import { describe, expect, it } from "vitest";+import { afterEach, describe, expect, it } from "vitest";import { cellId } from "@/__tests__/branded";-import type { EditableCell } from "@/core/notebook/document";-import type { CellColumnId } from "@/utils/id-tree";-import { exportedForTesting, notebookAtom, notebookReducer } from "../cells";-import { applyDocument } from "../document";+import type { CellColumnId } from "@/core/cells/layout";import type { CellId } from "../ids";import {collapsedAtom,@@ -15,10 +11,13 @@ import {foldSections,foldsAfter,foldsHiding,+ isColumnId,layoutAtom,+ newColumnId,visibleNeighbor,} from "../layout";import type { Outline } from "../outline";+import { setupNotebook } from "./harness";const [h1, a, h2, b, c, h1b, d] = ["h1", "a", "h2", "b", "c", "h1b", "d"].map(cellId,@@ -71,6 +70,13 @@ describe("foldSections", () => {});});+describe("column ids", () => {+ it("are told apart from cell ids, as drag and drop needs", () => {+ expect(isColumnId(newColumnId())).toBe(true);+ expect(isColumnId(cellId("Hbol"))).toBe(false);+ });+});+describe("deriveLayout", () => {const layout = deriveLayout([column("left", ids), column("right", [])],@@ -116,74 +122,56 @@ describe("deriveLayout", () => {});describe("layoutAtom", () => {- /** A store whose notebook holds `ids`, in that order, with nothing run. */- function notebookOf(...ids: CellId[]) {- const store = createStore();- const cells = ids.map((id): EditableCell => ({- id,- code: "",- name: "_",- config: {- column: null,- disabled: false,- hide_code: false,- expand_output: false,- },- }));- store.set(- notebookAtom,- applyDocument(exportedForTesting.initialNotebookState(), {- order: ids,- cells: new Map(cells.map((cell) => [cell.id, cell])),- }),- );- return store;- }+ const stops: Array<() => void> = [];+ afterEach(() => {+ for (const stop of stops.splice(0)) {+ stop();+ }+ });+ const [c1, c2] = ["c1", "c2"].map(cellId);/** Deliver `html` as a cell's output, as the kernel's message does. */function output(- store: ReturnType<typeof createStore>,+ dispatch: ReturnType<typeof setupNotebook>["dispatch"],id: CellId,html: string,) {- store.set(notebookAtom, (state) =>- notebookReducer(state, {- type: "handleCellMessage",- payload: {- cell_id: id,- output: {- channel: "output",- mimetype: "text/html",- data: html,- timestamp: 1,- },- console: null,- status: "idle",- stale_inputs: null,+ dispatch({+ type: "handleCellMessage",+ payload: {+ cell_id: id,+ output: {+ channel: "output",+ mimetype: "text/html",+ data: html,timestamp: 1,},- }),- );+ console: null,+ status: "idle",+ stale_inputs: null,+ timestamp: 1,+ },+ });}it("is the same layout after output that changes no heading", () => {- const store = notebookOf(h1, a);- output(store, h1, "<h1>Title</h1>");+ const { store, dispatch } = setupNotebook(stops);+ output(dispatch, c1, "<h1>Title</h1>");const layout = store.get(layoutAtom);- output(store, a, "<p>1</p>");- output(store, h1, "<h1>Title</h1>");+ output(dispatch, c2, "<p>1</p>");+ output(dispatch, c1, "<h1>Title</h1>");expect(store.get(layoutAtom)).toBe(layout);});it("follows a heading arriving in a cell's output", () => {- const store = notebookOf(h1, a);- store.set(collapsedAtom, new Set([h1]));+ const { store, dispatch } = setupNotebook(stops);+ store.set(collapsedAtom, new Set([c1]));expect(store.get(layoutAtom).columns[0].folds.size).toBe(0);- output(store, h1, "<h1>Title</h1>");+ output(dispatch, c1, "<h1>Title</h1>");- expect(store.get(layoutAtom).columns[0].folds.get(h1)).toEqual([a]);+ expect(store.get(layoutAtom).columns[0].folds.get(c1)).toEqual([c2]);});});diff --git a/frontend/src/core/cells/__tests__/session.test.ts b/frontend/src/core/cells/__tests__/session.test.tsindex 42b7cd85e0..e60bf707bb 100644--- a/frontend/src/core/cells/__tests__/session.test.ts+++ b/frontend/src/core/cells/__tests__/session.test.ts@@ -6,7 +6,6 @@ import { beforeEach, describe, expect, it, vi } from "vitest";import { Mocks } from "@/__mocks__/common";import { cellId } from "@/__tests__/branded";import { parseOutline } from "@/core/dom/outline";-import { visibleForTesting } from "@/utils/id-tree";import { invariant } from "@/utils/invariant";import { Logger } from "@/utils/Logger";import { SETUP_CELL_ID } from "../ids";@@ -35,7 +34,6 @@ function cellIn(result: ReturnType<typeof readExport>, id: string) {describe("readExport", () => {beforeEach(() => {vi.clearAllMocks();- visibleForTesting.reset();});// Test data factoriesdiff --git a/frontend/src/core/cells/__tests__/utils.test.ts b/frontend/src/core/cells/__tests__/utils.test.tsindex b0b5519209..8b49f794e0 100644--- a/frontend/src/core/cells/__tests__/utils.test.ts+++ b/frontend/src/core/cells/__tests__/utils.test.ts@@ -1,15 +1,10 @@/* Copyright 2026 Marimo. All rights reserved. *//* oxlint-disable typescript/no-explicit-any */import { describe, expect, it } from "vitest";-import {- getCellConfigs,- initialNotebookState,- type NotebookState,-} from "@/core/cells/cells";+import { initialNotebookState, type NotebookState } from "@/core/cells/cells";import { CellId } from "@/core/cells/ids";-import type { CellData, CellRuntimeState } from "@/core/cells/types";+import type { CellData } from "@/core/cells/types";import type { OutputMessage } from "@/core/kernel/messages";-import { MultiColumn } from "@/utils/id-tree";import {disabledCellIds,enabledCellIds,@@ -18,103 +13,10 @@ import {staleCellIds,} from "../utils";-describe("getCellConfigs", () => {- it("should return correct cell configs with column indices", () => {- // Create mock cell IDs- const cellId1 = CellId.create();- const cellId2 = CellId.create();- const cellId3 = CellId.create();- const cellId4 = CellId.create();-- // Create a mock NotebookState- const mockState: NotebookState = {- ...initialNotebookState(),- cellIds: MultiColumn.from([- [cellId1, cellId2],- [cellId3, cellId4],- ]),- cellData: {- [cellId1]: {- id: cellId1,- config: { hide_code: false, disabled: false },- } as CellData,- [cellId2]: {- id: cellId2,- config: { hide_code: true, disabled: false },- } as CellData,- [cellId3]: {- id: cellId3,- config: { hide_code: false, disabled: true },- } as CellData,- [cellId4]: {- id: cellId4,- config: { hide_code: true, disabled: true },- } as CellData,- },- cellRuntime: {} as Record<CellId, CellRuntimeState>,- };-- // Call the function- const result = getCellConfigs(mockState);-- // Assert the results- expect(result).toEqual([- { hide_code: false, disabled: false, column: 0 },- { hide_code: true, disabled: false, column: null },- { hide_code: false, disabled: true, column: 1 },- { hide_code: true, disabled: true, column: null },- ]);-- // Check that the original state was not modified- expect(mockState.cellData[cellId1].config).toEqual({- hide_code: false,- disabled: false,- });- expect(mockState.cellData[cellId2].config).toEqual({- hide_code: true,- disabled: false,- });- expect(mockState.cellData[cellId3].config).toEqual({- hide_code: false,- disabled: true,- });- expect(mockState.cellData[cellId4].config).toEqual({- hide_code: true,- disabled: true,- });- });-- it("should handle single column", () => {- const cellId1 = CellId.create();- const mockState: NotebookState = {- ...initialNotebookState(),- cellIds: MultiColumn.from([[cellId1]]),- cellData: {- [cellId1]: {- id: cellId1,- config: { hide_code: false, disabled: false },- } as CellData,- },- };-- const result = getCellConfigs(mockState);- expect(result).toEqual([- { hide_code: false, disabled: false, column: null },- ]);- });-- it("should handle empty notebook state", () => {- const mockState: NotebookState = initialNotebookState();- const result = getCellConfigs(mockState);-- expect(result).toEqual([]);- });-});-describe("staleCellIds", () => {it("should return cell IDs that have not been instantiated when auto-instantiate is false", () => {const state: NotebookState = {- cellIds: MultiColumn.from([["cell1", "cell2"]]),+ cellIds: ["cell1", "cell2"],cellData: {cell1: {lastExecutionTime: null,@@ -154,7 +56,7 @@ describe("staleCellIds", () => {it("should use lastExecutionTime when runElapsedTimeMs is null", () => {const state: NotebookState = {- cellIds: MultiColumn.from([["cell1", "cell2"]]),+ cellIds: ["cell1", "cell2"],cellData: {cell1: {lastExecutionTime: null,@@ -194,7 +96,7 @@ describe("staleCellIds", () => {it("should return cell IDs that are edited", () => {const state: NotebookState = {- cellIds: MultiColumn.from([["cell1", "cell2"]]),+ cellIds: ["cell1", "cell2"],cellData: {cell1: {lastExecutionTime: 123,@@ -234,7 +136,7 @@ describe("staleCellIds", () => {it("should return cell IDs that are interrupted", () => {const state: NotebookState = {- cellIds: MultiColumn.from([["cell1", "cell2"]]),+ cellIds: ["cell1", "cell2"],cellData: {cell1: {lastExecutionTime: 123,@@ -274,7 +176,7 @@ describe("staleCellIds", () => {it("should not return cell IDs that are disabled", () => {const state: NotebookState = {- cellIds: MultiColumn.from([["cell1", "cell2"]]),+ cellIds: ["cell1", "cell2"],cellData: {cell1: {lastExecutionTime: 123,@@ -394,7 +296,7 @@ describe("disabledCellIds", () => {const state: NotebookState = {...initialNotebookState(),- cellIds: MultiColumn.from([[cellId1, cellId2, cellId3]]),+ cellIds: [cellId1, cellId2, cellId3],cellData: {[cellId1]: { id: cellId1, config: { disabled: true } } as CellData,[cellId2]: { id: cellId2, config: { disabled: false } } as CellData,@@ -415,7 +317,7 @@ describe("enabledCellIds", () => {const state: NotebookState = {...initialNotebookState(),- cellIds: MultiColumn.from([[cellId1, cellId2, cellId3]]),+ cellIds: [cellId1, cellId2, cellId3],cellData: {[cellId1]: { id: cellId1, config: { disabled: true } } as CellData,[cellId2]: { id: cellId2, config: { disabled: false } } as CellData,diff --git a/frontend/src/core/cells/actions.ts b/frontend/src/core/cells/actions.tsindex 152ed754e8..2d62b45b6b 100644--- a/frontend/src/core/cells/actions.ts+++ b/frontend/src/core/cells/actions.ts@@ -3,21 +3,28 @@/** Notebook operations that edit the document and then adjust the editor. */import { historyField } from "@codemirror/commands";-import { atom, type Getter, useSetAtom } from "jotai";+import { atom, type Getter, type Setter, useSetAtom } from "jotai";import { useMemo } from "react";import { scrollAndHighlightCell } from "@/components/editor/links/cell-link";import type { EditableDocument } from "@/core/notebook/document";-import type { CellColumnId } from "@/utils/id-tree";import { Logger } from "@/utils/Logger";import { clamp } from "@/utils/math";+import { prepareCellCodeUpdate } from "../codemirror/language/cell-code";+import { splitEditor } from "../codemirror/language/utils";import type { SerializedEditorState } from "../codemirror/types";import { canCollapseOutline } from "../dom/outline";import type { CellConfig } from "../network/types";import { store } from "../state/jotai";import {+ addColumnAtom,+ addColumnBreakpointAtom,+ compactColumnsAtom,+ deleteColumnAtom,dropCellOverCellAtom,dropCellOverColumnAtom,+ mergeAllColumnsAtom,moveCellAtom,+ moveColumnAtom,moveCellsRelativeToAtom,moveCellToIndexAtom,placeCells,@@ -27,6 +34,7 @@ import {import {type DeletedCell,notebookAtom,+ notebookReducer,releaseCellAtoms,useReducerActions,} from "./cells";@@ -39,8 +47,10 @@ import {} from "./editor";import { CellId, SCRATCH_CELL_ID, SETUP_CELL_ID } from "./ids";import {+ type CellColumnId,collapsedAtom,columnOf,+ findColumn,foldOf,foldsAfter,foldsHiding,@@ -91,7 +101,7 @@ export const createCellAtom = atom(if (options.skipIfCodeExists && hasCellWithCode(document, code)) {return null;}- const { cellIds } = get(notebookAtom);+ const layout = get(layoutAtom);// The document knows cells and order. Columns come from each cell's// `column` config, which the cell before it provides unless it starts a@@ -100,31 +110,31 @@ export const createCellAtom = atom(let column: number | null = null;// The cell the new one comes right after, whose folds would hide it.let follows: CellId | undefined;- const layout = get(layoutAtom);if (options.after) {// A folded header hides the cells after it, so a cell placed after// the header goes after its fold.follows = foldOf(layout, options.after)?.at(-1) ?? options.after;placement = { after: follows };} else if (options.before) {- const tree = cellIds.findWithId(options.before);- const index = cellIds.indexOf(tree);+ const target = columnOf(layout, options.before);+ const index = layout.columns.indexOf(target);placement = { before: options.before };- follows = tree.before(options.before);- if (index > 0 && tree.indexOfOrThrow(options.before) === 0) {+ follows = target.ids[target.ids.indexOf(options.before) - 1];+ if (index > 0 && target.ids[0] === options.before) {column = index;}} else {- const tree =- (options.endOf && cellIds.get(options.endOf)) ?? cellIds.atOrThrow(0);- if (tree.length > 0) {- follows = tree.last();- placement = { after: follows };- } else if (cellIds.colLength > 1) {- column = cellIds.indexOf(tree);+ const target =+ (options.endOf && findColumn(layout, options.endOf)) ??+ layout.columns[0];+ const last = target.ids.at(-1);+ if (last !== undefined) {+ follows = last;+ placement = { after: last };+ } else if (layout.columns.length > 1) {+ column = layout.columns.indexOf(target);}}-const id = options.id ?? CellId.create();const config = { ...options.config, column };set(modelOf(get).editAtom, (tx) => {@@ -157,6 +167,70 @@ export const createCellAtom = atom(},);+/**+ * Split a cell at its cursor. The text before stays, the text after+ * becomes a new cell below, which is focused. Returns the new cell's id,+ * or null for a cell without an editor.+ */+export const splitCellAtom = atom(+ null,+ (get, set, { cellId }: { cellId: CellId }): CellId | null => {+ const view = get(notebookAtom).cellHandles[cellId]?.current?.editorView;+ if (!view) {+ return null;+ }+ const { beforeCursorCode, afterCursorCode } = splitEditor(view);+ const update = prepareCellCodeUpdate(view.state, beforeCursorCode);+ if (update) {+ view.dispatch(update);+ }+ const id = CellId.create();+ set(modelOf(get).editAtom, (tx) => {+ tx.setCode(cellId, beforeCursorCode);+ tx.createCell({ id, code: afterCursorCode }, { after: cellId });+ });+ clearOutput(set, cellId);+ focusWhenMounted(set, id);+ return id;+ },+);++/** Undo a split: the cell takes its code back and the cell split off goes. */+export const undoSplitCellAtom = atom(+ null,+ (+ get,+ set,+ {+ cellId,+ newCellId,+ snapshot,+ }: { cellId: CellId; newCellId: CellId; snapshot: string },+ ): void => {+ const view = get(notebookAtom).cellHandles[cellId]?.current?.editorView;+ const update = view && prepareCellCodeUpdate(view.state, snapshot);+ if (view && update) {+ view.dispatch(update);+ }+ const document = documentOf(get);+ set(modelOf(get).editAtom, (tx) => {+ tx.setCode(cellId, snapshot);+ if (document.cells.has(newCellId)) {+ tx.deleteCell(newCellId);+ }+ });+ releaseCellAtoms(newCellId);+ clearOutput(set, cellId);+ },+);++/** The output belonged to the code before the change; neither version has run. */+function clearOutput(set: Setter, cellId: CellId): void {+ set(notebookAtom, (state) =>+ notebookReducer(state, { type: "clearCellOutput", payload: { cellId } }),+ );+}+/*** Focus the cell before or after `cellId`. Past the edge of the column,* create one there, unless `create` is false.@@ -283,9 +357,7 @@ export const collapseAllCellsAtom = atom(null, (get, set): void => {set(collapsedAtom,new Set(- cellIds.inOrderIds.filter((id) =>- canCollapseOutline(cellRuntime[id]?.outline),- ),+ cellIds.filter((id) => canCollapseOutline(cellRuntime[id]?.outline)),),);});@@ -318,18 +390,18 @@ export const addSetupCellAtom = atom(null,(get, set, { code }: { code?: string } = {}): void => {const { cellIds } = get(notebookAtom);- if (cellIds.setupCellExists()) {+ if (cellIds.includes(SETUP_CELL_ID)) {focusWhenMounted(set, SETUP_CELL_ID);return;}// The setup cell leads the first column, which may be empty.- const first = cellIds.atOrThrow(0);+ const first = get(layoutAtom).columns[0];set(createCellAtom, {id: SETUP_CELL_ID,name: SETUP_CELL_ID,code: code ?? "",- ...(first.length > 0- ? { before: first.atOrThrow(0) }+ ...(first.ids.length > 0+ ? { before: first.ids[0] }: { endOf: first.id }),});},@@ -528,9 +600,25 @@ export function useCellActions() {const dropCellOverColumn = useSetAtom(dropCellOverColumnAtom);const sendToTop = useSetAtom(sendToTopAtom);const sendToBottom = useSetAtom(sendToBottomAtom);+ const addColumn = useSetAtom(addColumnAtom);+ const addColumnBreakpoint = useSetAtom(addColumnBreakpointAtom);+ const deleteColumn = useSetAtom(deleteColumnAtom);+ const mergeAllColumns = useSetAtom(mergeAllColumnsAtom);+ const compactColumns = useSetAtom(compactColumnsAtom);+ const moveColumn = useSetAtom(moveColumnAtom);+ const splitCell = useSetAtom(splitCellAtom);+ const undoSplitCell = useSetAtom(undoSplitCellAtom);return useMemo(() => ({...operations,+ splitCell,+ undoSplitCell,+ addColumn,+ addColumnBreakpoint,+ deleteColumn,+ mergeAllColumns,+ compactColumns,+ moveColumn,moveCell,moveCellToIndex,moveCellsRelativeTo,@@ -563,9 +651,7 @@ export type CellActions = ReturnType<typeof useCellActions>;/** Scroll to the first cell that is currently running. */export function notebookScrollToRunning() {const { cellIds, cellRuntime } = store.get(notebookAtom);- const cellId = cellIds.inOrderIds.find(- (id) => cellRuntime[id]?.status === "running",- );+ const cellId = cellIds.find((id) => cellRuntime[id]?.status === "running");if (!cellId) {return;}diff --git a/frontend/src/core/cells/add-missing-import.ts b/frontend/src/core/cells/add-missing-import.tsindex 27c61ebe87..c71bdfae70 100644--- a/frontend/src/core/cells/add-missing-import.ts+++ b/frontend/src/core/cells/add-missing-import.ts@@ -36,7 +36,7 @@ export function maybeAddMissingImport({`import[ \t]+${moduleName}[ \t]+as[ \t]+${variableName}`,"g",);- for (const cell of cellIds.inOrderIds) {+ for (const cell of cellIds) {if (regex.test(cellData[cell].code)) {return false;}diff --git a/frontend/src/core/cells/arrange.ts b/frontend/src/core/cells/arrange.tsindex 14a28794e1..03534c3f37 100644--- a/frontend/src/core/cells/arrange.ts+++ b/frontend/src/core/cells/arrange.ts@@ -1,26 +1,34 @@/* Copyright 2026 Marimo. All rights reserved. */-/** Notebook operations that move cells within and across columns. */+/** Notebook operations that move cells and arrange columns. */import { dequal as isEqual } from "dequal";import { atom, type Getter, type Setter } from "jotai";-import type { CellColumnId, CellIndex } from "@/utils/id-tree";+import { reorderColumnSizes } from "@/components/editor/columns/storage";+import type {+ DocumentEditor,+ EditableDocument,+} from "@/core/notebook/document";import { arrayMove } from "@/utils/arrays";import { notebookAtom, type UndoMoveEntry } from "./cells";import { documentOf, focusWhenMounted, modelOf } from "./edit";-import type { CellId } from "./ids";+import { CellId } from "./ids";import {+ type CellColumnId,+ columnIdsAtom,columnOf,+ emptyColumnIdsAtom,findColumn,type Layout,type LayoutColumn,layoutAtom,+ newColumnId,} from "./layout";/** A placement to restore a cell to: its column and its index there. */export interface Placement {columnId: CellColumnId;- index: CellIndex;+ index: number;}/** A cell with the cells its fold hides, which move with it. */@@ -66,12 +74,11 @@ function columnConfigs(return fixes;}-/**- * Make the document's cells fall into `columns`, in that order, as one edit.- * Nothing is written when they already do.- */-export function arrange(get: Getter, set: Setter, columns: CellId[][]): void {- const document = documentOf(get);+/** The steps that make the document's cells fall into `columns`, or null when they do. */+function arrangement(+ document: EditableDocument,+ columns: readonly (readonly CellId[])[],+): ((tx: DocumentEditor) => void) | null {const order = columns.flat();const fixes = columnConfigs(columns,@@ -79,16 +86,61 @@ export function arrange(get: Getter, set: Setter, columns: CellId[][]): void {);const reordered = !isEqual(order, document.order);if (!reordered && fixes.length === 0) {- return;+ return null;}- set(modelOf(get).editAtom, (tx) => {+ return (tx) => {if (reordered) {tx.setOrder(order);}for (const [id, column] of fixes) {tx.updateConfig(id, { column });}- });+ };+}++/**+ * How many of `columns` the document holds: all but the trailing empty+ * ones, which the page alone keeps.+ */+function heldByDocument(columns: readonly (readonly CellId[])[]): number {+ let count = columns.length;+ while (count > 1 && columns[count - 1].length === 0) {+ count -= 1;+ }+ return count;+}++/** Name the page's columns: `count` of `ids` for the document's, the rest kept empty. */+function nameColumns(+ set: Setter,+ ids: readonly CellColumnId[],+ count: number,+): void {+ set(columnIdsAtom, ids.slice(0, count));+ set(emptyColumnIdsAtom, ids.slice(count));+}++/**+ * Make the document's cells fall into `columns`, in that order, as one+ * edit, and name the columns `ids`, by default as they stand. Nothing is+ * written when the cells already fall that way. Trailing columns left+ * empty stay on the page, as the document cannot hold them, until a drop+ * compacts them.+ */+export function arrange(+ get: Getter,+ set: Setter,+ columns: CellId[][],+ ids: readonly CellColumnId[] = get(layoutAtom).columns.map(+ (column) => column.id,+ ),+): void {+ const count = heldByDocument(columns);+ nameColumns(set, ids, count);+ const edit = arrangement(documentOf(get), columns.slice(0, count));+ if (edit !== null) {+ set(modelOf(get).editAtom, edit);+ }}/**@@ -339,3 +391,144 @@ export function placeCells(}arrange(get, set, cleaned);}++// Columns --------------------------------------------------------------------++/** Add a column after another, with one new cell to start it, and focus the cell. */+export const addColumnAtom = atom(+ null,+ (get, set, { columnId }: { columnId: CellColumnId }): void => {+ const layout = get(layoutAtom);+ const at = layout.columns.findIndex((column) => column.id === columnId);+ if (at === -1) {+ return;+ }+ const id = CellId.create();+ const columns = columnsOf(layout);+ columns.splice(at + 1, 0, [id]);+ const ids = layout.columns.map((column) => column.id);+ ids.splice(at + 1, 0, newColumnId());+ const document = documentOf(get);+ const previous = layout.columns[at].ids.at(-1);+ const count = heldByDocument(columns);+ nameColumns(set, ids, count);+ set(modelOf(get).editAtom, (tx) => {+ tx.createCell(+ { id, code: "", config: { column: at + 1 } },+ previous === undefined ? {} : { after: previous },+ );+ arrangement(document, columns.slice(0, count))?.(tx);+ });+ focusWhenMounted(set, id);+ },+);++/** Start a new column at a cell, unless it already starts the first column. */+export const addColumnBreakpointAtom = atom(+ null,+ (get, set, { cellId }: { cellId: CellId }): void => {+ const layout = get(layoutAtom);+ const column = columnOf(layout, cellId);+ const at = layout.columns.indexOf(column);+ const index = column.ids.indexOf(cellId);+ if (at === 0 && index === 0) {+ return;+ }+ const columns = columnsOf(layout);+ columns.splice(at, 1, column.ids.slice(0, index), column.ids.slice(index));+ const ids = layout.columns.map((column) => column.id);+ // The id stays with the cells, so the half without any is the new one.+ ids.splice(index === 0 ? at : at + 1, 0, newColumnId());+ arrange(get, set, columns, ids);+ },+);++/** Remove a column, moving its cells to the column before it, or after it for the first. */+export const deleteColumnAtom = atom(+ null,+ (get, set, { columnId }: { columnId: CellColumnId }): void => {+ const layout = get(layoutAtom);+ const at = layout.columns.findIndex((column) => column.id === columnId);+ if (at === -1 || layout.columns.length <= 1) {+ return;+ }+ const columns = columnsOf(layout);+ const target = at === 0 ? 1 : at - 1;+ columns[target] =+ at === 0+ ? [...columns[at], ...columns[target]]+ : [...columns[target], ...columns[at]];+ columns.splice(at, 1);+ const ids = layout.columns.map((column) => column.id);+ ids.splice(at, 1);+ arrange(get, set, columns, ids);+ },+);++/** Bring every cell into the first column. */+export const mergeAllColumnsAtom = atom(null, (get, set): void => {+ const layout = get(layoutAtom);+ if (layout.columns.length <= 1) {+ return;+ }+ arrange(+ get,+ set,+ [layout.columns.flatMap((column) => [...column.ids])],+ [layout.columns[0].id],+ );+});++/** Drop the columns a drag left empty. */+export const compactColumnsAtom = atom(null, (get, set): void => {+ const layout = get(layoutAtom);+ const kept = layout.columns.filter((column) => column.ids.length > 0);+ if (kept.length === layout.columns.length || layout.columns.length <= 1) {+ return;+ }+ arrange(+ get,+ set,+ kept.map((column) => [...column.ids]),+ kept.map((column) => column.id),+ );+});++/** Move a column where another is, or one step left or right, with its saved width. */+export const moveColumnAtom = atom(+ null,+ (+ get,+ set,+ {+ column,+ overColumn,+ }: {+ column: CellColumnId;+ overColumn: CellColumnId | "_left_" | "_right_";+ },+ ): void => {+ if (column === overColumn) {+ return;+ }+ const layout = get(layoutAtom);+ const ids = layout.columns.map((column) => column.id);+ const from = ids.indexOf(column);+ const to =+ overColumn === "_left_"+ ? from - 1+ : overColumn === "_right_"+ ? from + 1+ : ids.indexOf(overColumn);+ if (from === -1 || to < 0 || to >= ids.length) {+ return;+ }+ arrange(+ get,+ set,+ arrayMove(columnsOf(layout), from, to),+ arrayMove(ids, from, to),+ );+ reorderColumnSizes(from, to);+ },+);diff --git a/frontend/src/core/cells/cells.ts b/frontend/src/core/cells/cells.tsindex 82400de26b..7acc85af1c 100644--- a/frontend/src/core/cells/cells.ts+++ b/frontend/src/core/cells/cells.ts@@ -6,11 +6,7 @@ import { atomFamily, selectAtom, splitAtom } from "jotai/utils";import { createRef, type ReducerWithoutAction } from "react";import type { CellHandle } from "@/components/editor/notebook-cell";import type { EditableDocument, Placement } from "@/core/notebook/document";-import {- type CellColumnId,- type CellIndex,- MultiColumn,-} from "@/utils/id-tree";+import type { CellColumnId } from "./layout";import { invariant } from "@/utils/invariant";import { Logger } from "@/utils/Logger";import { Objects } from "@/utils/objects";@@ -18,8 +14,6 @@ import { extractAllTracebackInfo, type TracebackInfo } from "@/utils/traceback";import { createReducerAndAtoms } from "../../utils/createReducer";import { foldAllBulk, unfoldAllBulk } from "../codemirror/editing/commands";import { editorMountScheduler } from "../codemirror/editor-mount-scheduler";-import { prepareCellCodeUpdate } from "../codemirror/language/cell-code";-import { splitEditor } from "../codemirror/language/utils";import type { SerializedEditorState } from "../codemirror/types";import { mergeOutlines } from "../dom/outline";import type { CellMessage } from "../kernel/messages";@@ -30,7 +24,7 @@ import { isWasm } from "../wasm/utils";import { prepareCellForExecution, transitionCell } from "./cell";import { applyDocument, documentEditMiddleware } from "./document";import { isCodeHidden, shownHiddenCodeAtom } from "./editor";-import { CellId, SCRATCH_CELL_ID } from "./ids";+import { type CellId, SCRATCH_CELL_ID } from "./ids";import { type CellLog, getCellLogsForMessage } from "./logs";import type { Outline } from "./outline";import { focusAndScrollCellIntoView } from "./scrollCellIntoView";@@ -74,7 +68,7 @@ export interface UndoDeleteEntry {export interface UndoMoveEntry {type: "move";cellIds: CellId[];- placements: Array<{ columnId: CellColumnId; index: CellIndex }>;+ placements: Array<{ columnId: CellColumnId; index: number }>;}export type HistoryEntry = UndoDeleteEntry | UndoMoveEntry;@@ -86,7 +80,8 @@ export interface NotebookState {/*** Order of cells on the page.*/- cellIds: MultiColumn<CellId>;+ /** The cells in document order. */+ cellIds: CellId[];/*** Map of cells to their view state*/@@ -144,7 +139,7 @@ function withScratchCell(notebookState: NotebookState): NotebookState {*/export function initialNotebookState(): NotebookState {return withScratchCell({- cellIds: MultiColumn.from([]),+ cellIds: [],cellData: {},cellRuntime: {},cellHandles: {},@@ -164,74 +159,6 @@ const {useActions,valueAtom: notebookAtom,} = createReducerAndAtoms(initialNotebookState, {- moveColumn: (- state,- action: {- column: CellColumnId;- overColumn: CellColumnId | "_left_" | "_right_";- },- ) => {- if (action.column === action.overColumn) {- return state;- }- return {- ...state,- cellIds: state.cellIds.moveColumn(action.column, action.overColumn),- };- },- addColumn: (state, action: { columnId: CellColumnId }) => {- // Add column and new cell- const newCellId = CellId.create();- return {- ...state,- cellIds: state.cellIds.addColumn(action.columnId, [newCellId]),- cellData: {- ...state.cellData,- [newCellId]: createCell({- id: newCellId,- }),- },- cellRuntime: {- ...state.cellRuntime,- [newCellId]: createCellRuntimeState(),- },- cellHandles: {- ...state.cellHandles,- [newCellId]: createRef(),- },- scrollKey: newCellId,- };- },- addColumnBreakpoint: (state, action: { cellId: CellId }) => {- const { cellId } = action;- if (state.cellIds.getColumns()[0].inOrderIds[0] === cellId) {- return state;- }- return {- ...state,- cellIds: state.cellIds.insertBreakpoint(cellId),- };- },- deleteColumn: (state, action: { columnId: CellColumnId }) => {- // Move all cells in the column to the previous column- const { columnId } = action;- return {- ...state,- cellIds: state.cellIds.delete(columnId),- };- },- mergeAllColumns: (state) => {- return {- ...state,- cellIds: state.cellIds.mergeAllColumns(),- };- },- compactColumns: (state) => {- return {- ...state,- cellIds: state.cellIds.compact(),- };- },/*** Put moved cells back where they were.*@@ -332,7 +259,7 @@ const {/** Follow the host document's cells and order. */reconcileDocument: (state, document: EditableDocument) => {const nextState = applyDocument(state, document);- for (const cellId of state.cellIds.inOrderIds) {+ for (const cellId of state.cellIds) {if (!(cellId in nextState.cellData)) {releaseCellAtoms(cellId);}@@ -382,7 +309,7 @@ const {},) => {const cellData = { ...state.cellData };- for (const cellId of state.cellIds.inOrderIds) {+ for (const cellId of state.cellIds) {const cell = state.cellData[cellId];const lastCodeRun = action.lastCodeRun[cellId] ?? null;cellData[cellId] = {@@ -402,7 +329,7 @@ const {resetCellRuntimes: (state) => {const preserveSnapshot = isWasm();const cellRuntime = { ...state.cellRuntime };- for (const cellId of [...state.cellIds.inOrderIds, SCRATCH_CELL_ID]) {+ for (const cellId of [...state.cellIds, SCRATCH_CELL_ID]) {const previous = state.cellRuntime[cellId];const hasSnapshot =preserveSnapshot &&@@ -419,12 +346,7 @@ const {return state;}- const column = state.cellIds.findWithId(scrollKey);- const index = column.indexOfOrThrow(scrollKey);-- const cellId =- index === column.length - 1 ? column.last() : column.atOrThrow(index);-+ const cellId = scrollKey;focusAndScrollCellIntoView({cellId: cellId,cell: state.cellHandles[cellId],@@ -462,110 +384,6 @@ const {cellLogs: [],};},- splitCell: (state, action: { cellId: CellId }) => {- const { cellId } = action;- const column = state.cellIds.findWithId(cellId);- const index = column.indexOfOrThrow(cellId);- const cell = state.cellData[cellId];- const cellHandle = state.cellHandles[cellId].current;-- if (cellHandle?.editorView == null) {- return state;- }-- const { beforeCursorCode, afterCursorCode } = splitEditor(- cellHandle.editorView,- );-- const update = prepareCellCodeUpdate(- cellHandle.editorView.state,- beforeCursorCode,- );-- if (update) {- cellHandle.editorView.dispatch(update);- }-- const newCellId = CellId.create();-- return {- ...state,- cellIds: state.cellIds.insertId(newCellId, column.id, index + 1),- cellData: {- ...state.cellData,- [cellId]: {- ...cell,- code: beforeCursorCode,- edited:- Boolean(beforeCursorCode) &&- beforeCursorCode.trim() !== cell.lastCodeRun?.trim(),- },- [newCellId]: createCell({- id: newCellId,- code: afterCursorCode,- edited: Boolean(afterCursorCode),- }),- },- cellRuntime: {- ...state.cellRuntime,- [cellId]: {- ...state.cellRuntime[cellId],- output: null,- consoleOutputs: [],- },- [newCellId]: createCellRuntimeState(),- },- cellHandles: {- ...state.cellHandles,- [newCellId]: createRef(),- },- scrollKey: newCellId,- };- },- undoSplitCell: (state, action: { cellId: CellId; snapshot: string }) => {- const { cellId, snapshot } = action;-- const cell = state.cellData[cellId];- const cellHandle = state.cellHandles[cellId].current;-- if (cellHandle?.editorView == null) {- return state;- }-- const update = prepareCellCodeUpdate(cellHandle.editorView.state, snapshot);-- if (update) {- cellHandle.editorView.dispatch(update);- }-- return {- ...state,- cellIds: state.cellIds.transformWithCellId(cellId, (column) => {- const newCellIndex = column.indexOfOrThrow(cellId) + 1;- return column.deleteAtIndex(newCellIndex);- }),- cellData: {- ...state.cellData,- [cellId]: {- ...cell,- code: snapshot,- edited:- Boolean(snapshot) && snapshot?.trim() !== cell.lastCodeRun?.trim(),- },- },- cellRuntime: {- ...state.cellRuntime,- [cellId]: {- ...state.cellRuntime[cellId],- output: null,- consoleOutputs: [],- },- },- cellHandles: {- ...state.cellHandles,- },- };- },clearCellOutput: (state, action: { cellId: CellId }) => {const { cellId } = action;return updateCellRuntimeState({@@ -594,7 +412,7 @@ const {},clearAllCellOutputs: (state) => {const newCellRuntime = { ...state.cellRuntime };- for (const cellId of state.cellIds.inOrderIds) {+ for (const cellId of state.cellIds) {newCellRuntime[cellId] = {...newCellRuntime[cellId],output: null,@@ -660,43 +478,12 @@ function updateCellData({};}+/** Each cell's config in order. The document's `column` configs are canonical. */export function getCellConfigs(state: NotebookState): CellConfig[] {- const cells = state.cellData;-- // We set to null by default to prevent inconsistencies between undefined & null- const defaultCellConfig: Partial<CellConfig> = { column: null };-- // Handle the case where there's only one column- // We don't want to set the column config- const hasMultipleColumns = state.cellIds.getColumns().length > 1;- if (!hasMultipleColumns) {- return state.cellIds.getColumns().flatMap((column) => {- return column.inOrderIds.map((cellId) => {- return {- ...cells[cellId].config,- ...defaultCellConfig,- };- });- });- }-- return state.cellIds.getColumns().flatMap((column, columnIndex) => {- return column.inOrderIds.map((cellId, cellIndex) => {- const config: Partial<CellConfig> = { ...defaultCellConfig };-- // Only set the column index for the first cell in the column- if (cellIndex === 0) {- config.column = columnIndex;- }-- const newConfig = {- ...cells[cellId].config,- ...config,- };-- return newConfig;- });- });+ return state.cellIds.map((cellId) => ({+ ...state.cellData[cellId].config,+ column: state.cellData[cellId].config.column ?? null,+ }));}export {@@ -716,9 +503,7 @@ export const addLogs = imperativeActions.addLogs;export const cellIdsAtom = atom((get) => get(notebookAtom).cellIds);-export const hasOnlyOneCellAtom = atom((get) =>- get(cellIdsAtom).hasOnlyOneId(),-);+export const hasOnlyOneCellAtom = atom((get) => get(cellIdsAtom).length === 1);export const hasDisabledCellsAtom = atom((get) => disabledCellIds(get(notebookAtom)).length > 0,@@ -736,7 +521,7 @@ export const needsRunAtom = atom((get) => notebookNeedsRun(get(notebookAtom)));export const cellErrorsAtom = atom((get) => {const { cellIds, cellRuntime, cellData } = get(notebookAtom);- const errors = cellIds.inOrderIds+ const errors = cellIds.map((cellId) => {const cell = cellRuntime[cellId];const { name } = cellData[cellId];@@ -785,9 +570,7 @@ export const cellOutlinesAtom = createDeepEqualAtom(export const notebookOutline = atom((get) => {const cellIds = get(cellIdsAtom);const cellRuntime = get(cellsRuntimeAtom);- const outlines = cellIds.inOrderIds.map(- (cellId) => cellRuntime[cellId].outline,- );+ const outlines = cellIds.map((cellId) => cellRuntime[cellId].outline);return mergeOutlines(outlines);});@@ -797,7 +580,7 @@ export const cellIdToNamesMap = createDeepEqualAtom(atom((get) => {const { cellIds, cellData } = get(notebookAtom);const names: Record<CellId, string | undefined> = Objects.fromEntries(- cellIds.inOrderIds.map((cellId) => [cellId, cellData[cellId]?.name]),+ cellIds.map((cellId) => [cellId, cellData[cellId]?.name]),);return names;}),@@ -854,12 +637,12 @@ export const getCells = () => store.get(notebookAtom).cellIds;*/export const getCellNames = () => {const { cellIds, cellData } = store.get(notebookAtom);- return cellIds.inOrderIds.map((id) => cellData[id]?.name).filter(Boolean);+ return cellIds.map((id) => cellData[id]?.name).filter(Boolean);};const cellDataAtoms = splitAtom(selectAtom(notebookAtom, (cells) =>- cells.cellIds.inOrderIds.map((id) => cells.cellData[id]),+ cells.cellIds.map((id) => cells.cellData[id]),),);export const useCellDataAtoms = () => useAtom(cellDataAtoms);@@ -879,16 +662,7 @@ export const notebookQueuedOrRunningCountAtom = atom((get) =>notebookQueueOrRunningCount(get(notebookAtom)),);-export const numColumnsAtom = atom(- (get) => get(notebookAtom).cellIds.colLength,-);-export const hasCellsAtom = atom(- (get) => get(notebookAtom).cellIds.idLength > 0,-);-export const columnIdsAtom = atom((get) =>- get(notebookAtom).cellIds.getColumnIds(),-);-+export const hasCellsAtom = atom((get) => get(notebookAtom).cellIds.length > 0);export const cellDataAtom = atomFamily((cellId: CellId) =>atom((get) => get(notebookAtom).cellData[cellId]),);@@ -929,7 +703,7 @@ export const useCellHandle = (cellId: CellId) =>*/export const getAllEditorViews = () => {const { cellIds, cellHandles } = store.get(notebookAtom);- return cellIds.inOrderIds+ return cellIds.map((cellId) => cellHandles[cellId]?.current?.editorViewOrNull).filter(Boolean);};@@ -953,12 +727,10 @@ export function flattenTopLevelNotebookCells(state: NotebookState,): (CellData & CellRuntimeState)[] {const { cellIds, cellData, cellRuntime } = state;- return cellIds.getColumns().flatMap((column) =>- column.topLevelIds.map((cellId) => ({- ...cellData[cellId],- ...cellRuntime[cellId],- })),- );+ return cellIds.map((cellId) => ({+ ...cellData[cellId],+ ...cellRuntime[cellId],+ }));}export function createTracebackInfoAtom(diff --git a/frontend/src/core/cells/document.ts b/frontend/src/core/cells/document.tsindex 7d9582b3d7..d1fb040e40 100644--- a/frontend/src/core/cells/document.ts+++ b/frontend/src/core/cells/document.ts@@ -8,7 +8,6 @@* document model and `notebookAtom` holds only runtime and editor state.*/-import type * as Protocol from "@marimo-team/client/protocol";import { dequal as isEqual } from "dequal";import type { createStore } from "jotai";import { createRef, type RefObject } from "react";@@ -21,15 +20,13 @@ import type {EditableDocument,} from "@/core/notebook/document";import type { DispatchedActionOf } from "@/utils/createReducer";-import { CollapsibleTree, MultiColumn } from "@/utils/id-tree";import { prepareCellCodeUpdate } from "../codemirror/language/cell-code";-import type { CellConfig } from "../network/types";import { canUseRtc, isRtcEnabled } from "../rtc/state";import { store } from "../state/jotai";import { activeDocumentModelAtom } from "../session/active-notebook";import type { NotebookState, ReducerCellActions } from "./cells";-import { groupByColumn } from "./columns";import type { CellId } from "./ids";+import { followColumnsAtom } from "./layout";import {type CellData,createCell,@@ -42,27 +39,6 @@ type CellAction = DispatchedActionOf<ReducerCellActions>;// Following the document -----------------------------------------------------/** Rebuild the tree for `columns`, keeping column ids. */-function reconcileColumns(- current: MultiColumn<CellId>,- columns: CellId[][],-): MultiColumn<CellId> {- const previous = current.getColumns();- // The document cannot hold an empty column, so keep the ones this client has.- while (previous[columns.length]?.length === 0) {- columns.push([]);- }- if (- columns.length === previous.length &&- columns.every((ids, index) => isEqual(ids, previous[index].inOrderIds))- ) {- return current;- }- return new MultiColumn(- columns.map((ids, index) => CollapsibleTree.from(ids, previous[index]?.id)),- );-}-/** The cell's data with the document's fields, and its editor brought along. */function followCell(current: CellData,@@ -115,9 +91,12 @@ export function applyDocument(state: NotebookState,document: EditableDocument,): NotebookState {- const cellIds = reconcileColumns(state.cellIds, groupByColumn(document));+ // The protocol's ids are plain strings; the notebook brands them.+ const cellIds = isEqual(document.order, state.cellIds)+ ? state.cellIds+ : ([...document.order] as CellId[]);let { cellData, cellRuntime, cellHandles } = state;- for (const id of cellIds.inOrderIds) {+ for (const id of cellIds) {const cell = document.cells.get(id);const current = state.cellData[id];if (cell === undefined) {@@ -149,8 +128,8 @@ export function applyDocument(cellData = { ...cellData, [id]: next };}}- const present = new Set(cellIds.inOrderIds);- const removed = state.cellIds.inOrderIds.filter((id) => !present.has(id));+ const present = new Set(cellIds);+ const removed = state.cellIds.filter((id) => !present.has(id));if (removed.length > 0) {const gone = new Set(removed);const keep = <V>(record: Record<CellId, V>): Record<CellId, V> =>@@ -172,7 +151,8 @@ export function applyDocument(}/**- * Reconcile the model's document now and on each change, until the signal aborts.+ * Reconcile the model's document and name its columns, now and on each+ * change, until the signal aborts.** TODO(MO-8028): Delete with `applyDocument`.*/@@ -191,6 +171,7 @@ export function followDocument({const document = store.get(model.documentAtom);if (document !== null) {reconcile(document);+ store.set(followColumnsAtom, document);}};const unsubscribe = store.sub(model.documentAtom, follow);@@ -200,107 +181,22 @@ export function followDocument({// Editing the document -------------------------------------------------------const CONFIG_FIELDS = ["disabled", "hide_code", "expand_output"] as const;--/** The fields of `after` that differ from `before`, leaving `column` to the tree. */-function configPatch(- before: CellConfig,- after: CellConfig,-): Partial<Protocol.CellConfig> | null {- const patch: Partial<Protocol.CellConfig> = {};- for (const field of CONFIG_FIELDS) {- if (before[field] !== after[field]) {- patch[field] = after[field] ?? false;- }- }- return Object.keys(patch).length === 0 ? null : patch;-}-/**- * The `column` configs that make the document's columns match the tree's.- *- * A cell without a column follows the cell before it, so only cells whose- * resolved column differs from their position need one.+ * The edit that writes the code a reducer action changed, or null when none+ * did. Order, creation, deletion, names and configs are edited on the model+ * directly by their atoms.*/-function columnFixes(state: NotebookState): Array<[CellId, number | null]> {- const fixes: Array<[CellId, number | null]> = [];- const columns = state.cellIds.getColumns();- if (columns.length === 1) {- for (const id of columns[0].inOrderIds) {- const column = state.cellData[id].config.column;- if (column != null && column !== 0) {- fixes.push([id, null]);- }- }- return fixes;- }- let inherited = 0;- columns.forEach((column, target) => {- for (const id of column.inOrderIds) {- const configured = state.cellData[id].config.column;- const resolved =- configured != null && configured >= 0 ? configured : inherited;- if (resolved !== target) {- fixes.push([id, target]);- }- inherited = target;- }- });- return fixes;-}--/** The edit that makes the document match `next`, or null when it already does. */function diffNotebook(prev: NotebookState,next: NotebookState,): DocumentEdit | null {const steps: Array<(tx: DocumentEditor) => void> = [];- const prevOrder = prev.cellIds.inOrderIds;- const nextOrder = next.cellIds.inOrderIds;- const prevIds = new Set(prevOrder);- const nextIds = new Set(nextOrder);- for (const id of prevOrder) {- if (!nextIds.has(id)) {- steps.push((tx) => tx.deleteCell(id));- }- }- const created: CellId[] = [];- for (const id of nextOrder) {+ for (const id of next.cellIds) {const cell = next.cellData[id];const before = prev.cellData[id];- if (!prevIds.has(id) || before === undefined) {- created.push(id);- steps.push((tx) =>- tx.createCell({- id,- code: cell.code,- name: cell.name,- config: cell.config,- }),- );- continue;- }- if (before === cell) {- continue;- }- if (before.code !== cell.code) {+ if (before !== undefined && cell !== before && before.code !== cell.code) {steps.push((tx) => tx.setCode(id, cell.code));}- if (before.name !== cell.name) {- steps.push((tx) => tx.setName(id, cell.name));- }- const patch = configPatch(before.config, cell.config);- if (patch !== null) {- steps.push((tx) => tx.updateConfig(id, patch));- }- }- for (const [id, column] of columnFixes(next)) {- steps.push((tx) => tx.updateConfig(id, { column }));- }- // Created cells go to the end, so the order is set when they belong elsewhere.- const survivors = prevOrder.filter((id) => nextIds.has(id));- if (!isEqual(nextOrder, [...survivors, ...created])) {- steps.push((tx) => tx.setOrder(nextOrder));}if (steps.length === 0) {return null;@@ -316,8 +212,8 @@ function diffNotebook(* Middleware for the notebook reducer. Writes what an action changed about* the cells and their order into the document model as one edit.*- * TODO(MO-8028): Each action family that moves onto the model removes its- * part of `diffNotebook`. The middleware goes with the last one, code.+ * TODO(MO-8028): Only code is left in `diffNotebook`. The middleware goes+ * with it, once the editors bind to the model.*/export function documentEditMiddleware(prev: NotebookState,diff --git a/frontend/src/core/cells/layout.ts b/frontend/src/core/cells/layout.tsindex 6407b1235e..417ede0316 100644--- a/frontend/src/core/cells/layout.ts+++ b/frontend/src/core/cells/layout.ts@@ -3,15 +3,83 @@/** The cells as they appear on the page: by column, with folded sections hidden. */import { atom, useAtomValue } from "jotai";-import type { CellColumnId } from "@/utils/id-tree";+import type { EditableDocument } from "@/core/notebook/document";import { findCollapseRange } from "../dom/outline";-import { cellIdsAtom, cellOutlinesAtom } from "./cells";-import type { CellId } from "./ids";+import { activeDocumentModelAtom } from "../session/active-notebook";+import { cellOutlinesAtom } from "./cells";+import { groupByColumn } from "./columns";+import { CellId } from "./ids";import type { Outline } from "./outline";+/** A column's identity on this page, as drag and drop and saved widths refer to it. */+export type CellColumnId = string & { __brand: "CellColumnId" };+/** The headers whose sections the user folded. Only this browser knows. */export const collapsedAtom = atom<ReadonlySet<CellId>>(new Set<CellId>());+/**+ * This browser's names for the document's columns, by position. The+ * document knows columns only by position, so `followColumnsAtom` names+ * a column as it appears and forgets one as it goes, and the column+ * operations carry the names along as they rearrange.+ */+export const columnIdsAtom = atom<readonly CellColumnId[]>([]);++/**+ * Trailing columns a rearrangement here emptied. The document cannot hold+ * an empty column at its end, so the page keeps them, as drop targets for+ * the cell being dragged, until a drop compacts them.+ */+export const emptyColumnIdsAtom = atom<readonly CellColumnId[]>([]);++export function newColumnId(): CellColumnId {+ return `column-${CellId.create()}` as CellColumnId;+}++export function isColumnId(id: unknown): id is CellColumnId {+ return typeof id === "string" && id.startsWith("column-");+}++/**+ * Name the document's columns as the document changes. A column that+ * appears takes over an empty column the page keeps, or gets a fresh id;+ * one that goes is forgotten.+ */+export const followColumnsAtom = atom(+ null,+ (get, set, document: EditableDocument): void => {+ const count = groupByColumn(document).length;+ const ids = get(columnIdsAtom);+ if (ids.length === count) {+ return;+ }+ const empty = get(emptyColumnIdsAtom);+ const named = [...ids, ...empty].slice(0, count);+ while (named.length < count) {+ named.push(newColumnId());+ }+ set(columnIdsAtom, named);+ set(emptyColumnIdsAtom, empty.slice(Math.max(0, count - ids.length)));+ },+);++/** The document's columns, each with its id, then the empty ones the page keeps. */+function columnsOf(+ document: EditableDocument | null,+ ids: readonly CellColumnId[],+ empty: readonly CellColumnId[],+): Array<{ id: CellColumnId; ids: CellId[] }> {+ const groups = document === null ? [[]] : groupByColumn(document);+ return [+ ...groups.map((cells, index) => ({+ // Before the first document names them, columns go by position.+ id: ids[index] ?? (`column-${index}` as CellColumnId),+ ids: cells,+ })),+ ...empty.map((id) => ({ id, ids: [] as CellId[] })),+ ];+}+export interface LayoutColumn {readonly id: CellColumnId;/** Every cell of the column, in order. */@@ -69,18 +137,17 @@ export function deriveLayout(}/**- * The layout of the notebook's cells.+ * The layout of the notebook's cells: the document's order and columns,+ * this browser's column ids and folds, and the kernel's outlines.*- * TODO(MO-8028): Order and columns come from the reducer's tree until the- * arrange family edits the document; then they derive from the document and- * the column ids this browser keeps. Outlines come from executions later.+ * TODO(MO-8028): Outlines come from executions later.*/export const layoutAtom = atom((get): Layout => {+ const model = get(activeDocumentModelAtom);+ const document = model === null ? null : get(model.documentAtom);const outlines = get(cellOutlinesAtom);return deriveLayout(- get(cellIdsAtom)- .getColumns()- .map((column) => ({ id: column.id, ids: column.inOrderIds })),+ columnsOf(document, get(columnIdsAtom), get(emptyColumnIdsAtom)),(id) => outlines[id] ?? null,get(collapsedAtom),);@@ -88,6 +155,8 @@ export const layoutAtom = atom((get): Layout => {export const useLayout = () => useAtomValue(layoutAtom);+export const numColumnsAtom = atom((get) => get(layoutAtom).columns.length);+export function findColumn(layout: Layout,columnId: CellColumnId,diff --git a/frontend/src/core/cells/pending-delete-service.ts b/frontend/src/core/cells/pending-delete-service.tsindex cd38ce087e..da6be9a547 100644--- a/frontend/src/core/cells/pending-delete-service.ts+++ b/frontend/src/core/cells/pending-delete-service.ts@@ -49,7 +49,7 @@ const { valueAtom: pendingDeleteStateAtom, useActions } = createReducerAndAtoms(const variables = store.get(variablesAtom);const emptyCells = new Set(- notebook.cellIds.inOrderIds.filter(+ notebook.cellIds.filter((id) => notebook.cellData[id].code.trim() === "",),);diff --git a/frontend/src/core/cells/utils.ts b/frontend/src/core/cells/utils.tsindex 13c36c5d62..ff4decd37a 100644--- a/frontend/src/core/cells/utils.ts+++ b/frontend/src/core/cells/utils.ts@@ -25,7 +25,7 @@ export function notebookNeedsRun(state: NotebookState) {}export function notebookCells(state: NotebookState) {- return state.cellIds.inOrderIds.map((cellId) => state.cellData[cellId]);+ return state.cellIds.map((cellId) => state.cellData[cellId]);}export function notebookCellEditorViews({ cellHandles }: NotebookState) {@@ -42,7 +42,7 @@ export function notebookCellEditorViews({ cellHandles }: NotebookState) {export function disabledCellIds(state: NotebookState) {const { cellIds, cellData } = state;const disabledCells: CellId[] = [];- for (const cellId of cellIds.inOrderIds) {+ for (const cellId of cellIds) {const cell = cellData[cellId];if (cell.config.disabled) {disabledCells.push(cellId);@@ -54,7 +54,7 @@ export function disabledCellIds(state: NotebookState) {export function enabledCellIds(state: NotebookState) {const { cellIds, cellData } = state;const enabledCells: CellId[] = [];- for (const cellId of cellIds.inOrderIds) {+ for (const cellId of cellIds) {const cell = cellData[cellId];if (!cell.config.disabled) {enabledCells.push(cellId);@@ -108,7 +108,7 @@ export function getDescendantsStatus(*/export function staleCellIds(state: NotebookState) {const { cellIds, cellData, cellRuntime } = state;- return cellIds.inOrderIds.filter(+ return cellIds.filter((cellId) =>isUninstantiated({// runElapstedTimeMs is what we've seen in this sessiondiff --git a/frontend/src/core/codemirror/copilot/__tests__/getCodes.test.ts b/frontend/src/core/codemirror/copilot/__tests__/getCodes.test.tsindex 36c3c1b64a..f284b3558f 100644--- a/frontend/src/core/codemirror/copilot/__tests__/getCodes.test.ts+++ b/frontend/src/core/codemirror/copilot/__tests__/getCodes.test.ts@@ -9,7 +9,6 @@ import { createCell } from "@/core/cells/types";import { OverridingHotkeyProvider } from "@/core/hotkeys/hotkeys";import { store } from "@/core/state/jotai";import { variablesAtom } from "@/core/variables/state";-import { MultiColumn } from "@/utils/id-tree";import { cellConfigExtension } from "../../config/extension";import { adaptiveLanguageConfiguration } from "../../language/extension";import {@@ -159,7 +158,7 @@ describe("getCodes", () => {];store.set(notebookAtom, {...initialNotebookState(),- cellIds: MultiColumn.from([[Cells.cell1, Cells.cell2]]),+ cellIds: [Cells.cell1, Cells.cell2],cellHandles: {[Cells.cell1]: { current: mockEditorViews[0] },[Cells.cell2]: { current: mockEditorViews[1] },@@ -177,7 +176,7 @@ describe("getCodes", () => {];store.set(notebookAtom, {...initialNotebookState(),- cellIds: MultiColumn.from([[Cells.cell1, Cells.cell2]]),+ cellIds: [Cells.cell1, Cells.cell2],cellHandles: {[Cells.cell1]: { current: mockEditorViews[0] },[Cells.cell2]: { current: mockEditorViews[1] },@@ -196,7 +195,7 @@ describe("getCodes", () => {];store.set(notebookAtom, {...initialNotebookState(),- cellIds: MultiColumn.from([[Cells.cell1, Cells.cell2, Cells.cell3]]),+ cellIds: [Cells.cell1, Cells.cell2, Cells.cell3],cellHandles: {[Cells.cell1]: { current: mockEditorViews[0] },[Cells.cell2]: { current: mockEditorViews[1] },@@ -218,7 +217,7 @@ describe("getCodes", () => {const otherCode = "print('Hello World')";store.set(notebookAtom, {...initialNotebookState(),- cellIds: MultiColumn.from([[Cells.cell1, Cells.cell2]]),+ cellIds: [Cells.cell1, Cells.cell2],cellData: {[Cells.cell1]: createCell({ id: Cells.cell1, code: "import os" }),[Cells.cell2]: createCell({ id: Cells.cell2, code: "x = 1" }),@@ -232,7 +231,7 @@ describe("getCodes", () => {const otherCode = "print('Hello World')";store.set(notebookAtom, {...initialNotebookState(),- cellIds: MultiColumn.from([[Cells.cell1, Cells.cell2]]),+ cellIds: [Cells.cell1, Cells.cell2],cellData: {[Cells.cell1]: createCell({ id: Cells.cell1, code: "import os" }),[Cells.cell2]: createCell({ id: Cells.cell2, code: "x = 1" }),@@ -250,7 +249,7 @@ describe("topologicalCodesAtom", () => {it("should include every cell when no editor is mounted", () => {store.set(notebookAtom, {...initialNotebookState(),- cellIds: MultiColumn.from([[Cells.cell1, Cells.cell2, Cells.cell3]]),+ cellIds: [Cells.cell1, Cells.cell2, Cells.cell3],cellData: {[Cells.cell1]: createCell({ id: Cells.cell1, code: "import os" }),[Cells.cell2]: createCell({ id: Cells.cell2, code: "x = 1" }),diff --git a/frontend/src/core/codemirror/copilot/getCodes.ts b/frontend/src/core/codemirror/copilot/getCodes.tsindex cbcb7c0a82..4e6ff16297 100644--- a/frontend/src/core/codemirror/copilot/getCodes.ts+++ b/frontend/src/core/codemirror/copilot/getCodes.ts@@ -12,7 +12,7 @@ import { getEditorCodeAsPython } from "../language/utils";const notebookCellCodes = atom((get) => {const notebook = get(notebookAtom);const codes = Objects.fromEntries(- notebook.cellIds.inOrderIds.map((id) => {+ notebook.cellIds.map((id) => {// A cell editor mounts progressively, so the notebook store is the only// complete source of code until every editor is built.const editorView = notebook.cellHandles[id]?.current?.editorViewOrNull;@@ -54,7 +54,7 @@ export function getOtherCellsCode(otherCode: string) {const inOrderCellIdsAtom = atom((get) => {const notebook = get(notebookAtom);- return notebook.cellIds.inOrderIds;+ return notebook.cellIds;});const topologicalCellIdsAtom = atom((get) => {const cellIds = get(inOrderCellIdsAtom);diff --git a/frontend/src/core/codemirror/lsp/__tests__/notebook-lsp.test.ts b/frontend/src/core/codemirror/lsp/__tests__/notebook-lsp.test.tsindex d142698ec3..df48561f74 100644--- a/frontend/src/core/codemirror/lsp/__tests__/notebook-lsp.test.ts+++ b/frontend/src/core/codemirror/lsp/__tests__/notebook-lsp.test.ts@@ -11,7 +11,6 @@ import type { CellId } from "@/core/cells/ids";import { initialNotebookState, notebookAtom } from "@/core/cells/cells";import { createCell } from "@/core/cells/types";import { store } from "@/core/state/jotai";-import { MultiColumn } from "@/utils/id-tree";import { topologicalCodesAtom } from "../../copilot/getCodes";import { lspWorkspaceAtom } from "@/core/saving/file-state";import { languageAdapterState } from "../../language/extension";@@ -69,7 +68,7 @@ describe("merged document with unmounted editors", () => {it("should include the code of cells whose editors are not mounted", () => {store.set(notebookAtom, {...initialNotebookState(),- cellIds: MultiColumn.from([[Cells.cell1, Cells.cell2]]),+ cellIds: [Cells.cell1, Cells.cell2],cellData: {[Cells.cell1]: createCell({ id: Cells.cell1, code: "import math" }),[Cells.cell2]: createCell({diff --git a/frontend/src/core/codemirror/lsp/utils.ts b/frontend/src/core/codemirror/lsp/utils.tsindex 78a123c9f3..29a7f62e81 100644--- a/frontend/src/core/codemirror/lsp/utils.ts+++ b/frontend/src/core/codemirror/lsp/utils.ts@@ -13,7 +13,7 @@ export function isKnownCellDocumentUri(uri: string): boolean {if (!CellDocumentUri.is(uri)) {return false;}- return store.get(cellIdsAtom).inOrderIds.includes(CellDocumentUri.parse(uri));+ return store.get(cellIdsAtom).includes(CellDocumentUri.parse(uri));}export function getLspRootUri() {diff --git a/frontend/src/core/documentation/doc-lookup.ts b/frontend/src/core/documentation/doc-lookup.tsindex d163f3e8cc..20a27c9d9a 100644--- a/frontend/src/core/documentation/doc-lookup.ts+++ b/frontend/src/core/documentation/doc-lookup.ts@@ -17,7 +17,7 @@ export async function requestOutputDocumentation(qualifiedName: string,): Promise<void> {// We need any valid cell_id for the completion request.- const cellId = getCells().inOrderIds.at(0);+ const cellId = getCells().at(0);if (!cellId) {return;}diff --git a/frontend/src/core/edit-app.tsx b/frontend/src/core/edit-app.tsxindex 39094cfc6d..706497b8f2 100644--- a/frontend/src/core/edit-app.tsx+++ b/frontend/src/core/edit-app.tsx@@ -27,11 +27,8 @@ import { useSetOutputsExpanded } from "../components/editor/actions/useSetOutputimport { CellArray } from "../components/editor/renderers/cell-array";import { CellsRenderer } from "../components/editor/renderers/cells-renderer";import { useHotkey } from "../hooks/useHotkey";-import {- hasCellsAtom,- notebookIsRunningAtom,- numColumnsAtom,-} from "./cells/cells";+import { hasCellsAtom, notebookIsRunningAtom } from "./cells/cells";+import { numColumnsAtom } from "./cells/layout";import { useCellActions } from "./cells/actions";import type { AppConfig, UserConfig } from "./config/config-schema";import { RuntimeState } from "./kernel/RuntimeState";diff --git a/frontend/src/core/errors/error-entries.ts b/frontend/src/core/errors/error-entries.tsindex 64f82e7cf7..3186810328 100644--- a/frontend/src/core/errors/error-entries.ts+++ b/frontend/src/core/errors/error-entries.ts@@ -80,7 +80,7 @@ export function getCellErrorEntries(store: JotaiStore): CellErrorEntry[] {const { cellIds, cellRuntime, cellData } = store.get(notebookAtom);const entries: CellErrorEntry[] = [];- for (const [cellIndex, cellId] of cellIds.inOrderIds.entries()) {+ for (const [cellIndex, cellId] of cellIds.entries()) {const cell = cellRuntime[cellId];const output = cell.output;if (!output || !isErrorMime(output.mimetype)) {diff --git a/frontend/src/core/islands/__tests__/bootstrap.test.ts b/frontend/src/core/islands/__tests__/bootstrap.test.tsindex cf4f02849b..401846166a 100644--- a/frontend/src/core/islands/__tests__/bootstrap.test.ts+++ b/frontend/src/core/islands/__tests__/bootstrap.test.ts@@ -103,15 +103,11 @@ describe("initializeIslands document", () => {}const notebook = store.get(notebookAtom);- expect(notebook.cellIds.inOrderIds).toEqual(["c1", "c3", "k7f2"]);+ expect(notebook.cellIds).toEqual(["c1", "c3", "k7f2"]);expect(notebook.cellData[cellId("c3")].code).toBe("x = 21");expect(notebook.cellRuntime[cellId("c3")]).toBeDefined();handlers?.message({ event: "reset", data: {} });- expect(store.get(notebookAtom).cellIds.inOrderIds).toEqual([- "c1",- "c3",- "k7f2",- ]);+ expect(store.get(notebookAtom).cellIds).toEqual(["c1", "c3", "k7f2"]);});});diff --git a/frontend/src/core/islands/components/web-components.tsx b/frontend/src/core/islands/components/web-components.tsxindex 26bf1ea3af..071469ab85 100644--- a/frontend/src/core/islands/components/web-components.tsx+++ b/frontend/src/core/islands/components/web-components.tsx@@ -104,7 +104,7 @@ export class MarimoIslandElement extends HTMLElement {*/private getCellIdFromIndex(idx: number): CellId | undefined {const { cellIds } = store.get(notebookAtom);- return cellIds.inOrderIds.at(idx);+ return cellIds.at(idx);}/**diff --git a/frontend/src/core/notebook/document/__tests__/codemirror.test.ts b/frontend/src/core/notebook/document/__tests__/codemirror.test.tsindex dcb5f1da83..4136b5a2ad 100644--- a/frontend/src/core/notebook/document/__tests__/codemirror.test.ts+++ b/frontend/src/core/notebook/document/__tests__/codemirror.test.ts@@ -82,6 +82,14 @@ function languages(id: CellId) {dropCellOverColumn: vi.fn(),sendToTop: vi.fn(),sendToBottom: vi.fn(),+ addColumn: vi.fn(),+ addColumnBreakpoint: vi.fn(),+ deleteColumn: vi.fn(),+ mergeAllColumns: vi.fn(),+ compactColumns: vi.fn(),+ moveColumn: vi.fn(),+ splitCell: vi.fn(),+ undoSplitCell: vi.fn(),onRun: vi.fn(),deleteCell: vi.fn(),afterToggleMarkdown: vi.fn(),diff --git a/frontend/src/core/session/__tests__/useNotebookSession.hook.test.tsx b/frontend/src/core/session/__tests__/useNotebookSession.hook.test.tsxindex ede233c036..6ac53acadb 100644--- a/frontend/src/core/session/__tests__/useNotebookSession.hook.test.tsx+++ b/frontend/src/core/session/__tests__/useNotebookSession.hook.test.tsx@@ -272,7 +272,7 @@ describe("useNotebookSession document model", () => {});let notebook = store.get(notebookAtom);- expect(notebook.cellIds.inOrderIds).toEqual(["c1", "c3", "k7f2"]);+ expect(notebook.cellIds).toEqual(["c1", "c3", "k7f2"]);expect(notebook.cellData[cellId("k7f2")]).toMatchObject({code: "y = x * 2",config: { hide_code: true },diff --git a/frontend/src/stories/cell.stories.tsx b/frontend/src/stories/cell.stories.tsxindex d7e2c04845..81430a729e 100644--- a/frontend/src/stories/cell.stories.tsx+++ b/frontend/src/stories/cell.stories.tsx@@ -15,7 +15,6 @@ import { requestClientAtom } from "@/core/network/requests";import { resolveRequestClient } from "@/core/network/resolve";import type { CellConfig } from "@/core/network/types";import { ConnectionState } from "@/core/session/types";-import { MultiColumn } from "@/utils/id-tree";import type { Milliseconds, Seconds } from "@/utils/time";import { Cell as EditorCell } from "../components/editor/notebook-cell";import { TooltipProvider } from "../components/ui/tooltip";@@ -55,7 +54,7 @@ const Cell: React.FC<{lastExecutionTime: null,},},- cellIds: MultiColumn.from([[cid]]),+ cellIds: [cid],cellRuntime: {[cid]: createCellRuntimeState({output: overrides.output ?? null,diff --git a/frontend/src/stories/layout/vertical/one-column.stories.tsx b/frontend/src/stories/layout/vertical/one-column.stories.tsxindex 1cdc87dee4..4acedb903d 100644--- a/frontend/src/stories/layout/vertical/one-column.stories.tsx+++ b/frontend/src/stories/layout/vertical/one-column.stories.tsx@@ -12,7 +12,6 @@ import { connectionAtom } from "@/core/network/connection";import { requestClientAtom } from "@/core/network/requests";import { resolveRequestClient } from "@/core/network/resolve";import { ConnectionState } from "@/core/session/types";-import { MultiColumn } from "@/utils/id-tree";import type { Milliseconds, Seconds } from "@/utils/time";import { CellArray } from "../../../components/editor/renderers/cell-array";import { CellsRenderer } from "../../../components/editor/renderers/cells-renderer";@@ -52,7 +51,7 @@ LongReprA()`,lastExecutionTime: null,},},- cellIds: MultiColumn.from([[cellId]]),+ cellIds: [cellId],cellRuntime: {[cellId]: createCellRuntimeState({output: {diff --git a/frontend/src/stories/runtime-story.tsx b/frontend/src/stories/runtime-story.tsxindex bd21bbb259..d237dde205 100644--- a/frontend/src/stories/runtime-story.tsx+++ b/frontend/src/stories/runtime-story.tsx@@ -16,7 +16,6 @@ import type { EnvironmentOperation } from "@/core/alerts/environment";import { notebookAtom } from "@/core/cells/cells";import { CellId } from "@/core/cells/ids";import { createCell, createCellRuntimeState } from "@/core/cells/types";-import { MultiColumn } from "@/utils/id-tree";import { userConfigAtom } from "@/core/config/config";import {type AppConfig,@@ -111,7 +110,7 @@ function RuntimePreview({{props.surface === "notebook" && (<div className="flex-1 min-w-0 pt-12"><ConnectionNotice appConfig={appConfig} onRetry={reconnect} />- {notebook.cellIds.inOrderIds.map((cellId) => (+ {notebook.cellIds.map((cellId) => (<VerticalLayoutWrapper key={cellId} appConfig={appConfig}><CellcellId={cellId}@@ -351,7 +350,7 @@ export function RuntimeStory(props: Props) {const cellId = CellId.create();state.set(notebookAtom, (value) => ({...value,- cellIds: MultiColumn.from([[cellId]]),+ cellIds: [cellId],cellData: {...value.cellData,[cellId]: createCell({diff --git a/frontend/src/utils/__tests__/id-tree.test.ts b/frontend/src/utils/__tests__/id-tree.test.tsdeleted file mode 100644index 38f48b35cc..0000000000--- a/frontend/src/utils/__tests__/id-tree.test.ts+++ /dev/null@@ -1,617 +0,0 @@-/* Copyright 2026 Marimo. All rights reserved. */-import { beforeEach, describe, expect, it } from "vitest";-import { SETUP_CELL_ID } from "@/core/cells/ids";-import {- type CellColumnId,- type CellIndex,- CollapsibleTree,- MultiColumn,-} from "../id-tree";--let tree: CollapsibleTree<string>;--describe("CollapsibleTree", () => {- beforeEach(() => {- tree = CollapsibleTree.from(["one", "two", "three", "four"]);- });-- it("initializes correctly", () => {- expect(tree.toString()).toMatchInlineSnapshot(`- "one- two- three- four- "- `);- });-- it("inserts nodes correctly", () => {- tree = tree.insert("newNode", 1);- expect(tree.toString()).toMatchInlineSnapshot(`- "one- newNode- two- three- four- "- `);-- tree = tree.insert("newNode2", tree.length - 1);- expect(tree.toString()).toMatchInlineSnapshot(`- "one- newNode- two- three- newNode2- four- "- `);-- tree = tree.insert("newNode3", 10_000);- expect(tree.toString()).toMatchInlineSnapshot(`- "one- newNode- two- three- newNode2- four- newNode3- "- `);- });-- it("can delete non-collapsed nodes without throwing", () => {- // Deleting a non-collapsed node should not throw- // (previously this would throw "Node is already expanded" internally)- const result = tree.deleteAtIndex(1);- expect(result.toString()).toMatchInlineSnapshot(`- "one- three- four- "- `);- });-- it("fails to delete nodes", () => {- expect(() => tree.deleteAtIndex(5)).toThrowErrorMatchingInlineSnapshot(- "[Error: Node at index 5 not found in tree]",- );- });-- it("atOrThrow throws if not found", () => {- expect(() => tree.atOrThrow(5)).toThrowErrorMatchingInlineSnapshot(- "[Error: Node at index 5 not found in tree]",- );- });-- it("first and last are correct", () => {- expect(tree.first()).toBe("one");- expect(tree.last()).toBe("four");- });-- it("handles slice correctly", () => {- // Normal slice in middle- expect(tree.slice(1, 3)).toEqual(["two", "three"]);-- // Slice from beginning- expect(tree.slice(0, 2)).toEqual(["one", "two"]);-- // Slice to end- expect(tree.slice(2, 4)).toEqual(["three", "four"]);-- // Slice entire array- expect(tree.slice(0, 4)).toEqual(["one", "two", "three", "four"]);-- // Slice with out-of-bounds end- expect(tree.slice(2, 10)).toEqual(["three", "four"]);-- // Slice with negative start (should work like normal array slice)- expect(tree.slice(-2, 4)).toEqual(["three", "four"]);-- // Slice with start > end (should return empty array)- expect(tree.slice(3, 1)).toEqual([]);-- // Slice with same start and end- expect(tree.slice(2, 2)).toEqual([]);-- // Slice single element- expect(tree.slice(1, 2)).toEqual(["two"]);- });-- it("handles after correctly", () => {- // Normal case - get next node- expect(tree.after("one")).toBe("two");- expect(tree.after("two")).toBe("three");- expect(tree.after("three")).toBe("four");-- // Edge case - last node should return undefined- expect(tree.after("four")).toBeUndefined();-- // Edge case - non-existent node should throw- expect(() => tree.after("nonexistent")).toThrow(- "Node nonexistent not found in tree. Valid ids: one,two,three,four",- );- });-- it("handles before correctly", () => {- // Normal case - get previous node- expect(tree.before("two")).toBe("one");- expect(tree.before("three")).toBe("two");- expect(tree.before("four")).toBe("three");-- // Edge case - first node should return undefined- expect(tree.before("one")).toBeUndefined();-- // Edge case - non-existent node should throw- expect(() => tree.before("nonexistent")).toThrow(- "Node nonexistent not found in tree. Valid ids: one,two,three,four",- );- });-- it("handles slice on empty tree", () => {- const emptyTree = CollapsibleTree.from([]);- expect(emptyTree.slice(0, 1)).toEqual([]);- expect(emptyTree.slice(0, 0)).toEqual([]);- });-- it("handles after/before on single element tree", () => {- const singleTree = CollapsibleTree.from(["single"]);- expect(singleTree.after("single")).toBeUndefined();- expect(singleTree.before("single")).toBeUndefined();- });-- it("handles split correctly", () => {- const [left, right] = tree.split("two");- expect(left.toString()).toMatchInlineSnapshot(`- "one- "- `);- expect(right?.toString()).toMatchInlineSnapshot(`- "two- three- four- "- `);- });-- it("handles split at the beginning", () => {- const [left, right] = tree.split("one");- expect(left.toString()).toMatchInlineSnapshot(`- ""- `);- expect(right?.toString()).toMatchInlineSnapshot(`- "one- two- three- four- "- `);- });-- it("handles split at the end", () => {- const [left, right] = tree.split("four");- expect(left.toString()).toMatchInlineSnapshot(`- "one- two- three- "- `);- expect(right?.toString()).toMatchInlineSnapshot(`- "four- "- `);- });-- it("throws when splitting on non-existent node", () => {- expect(() => tree.split("five")).toThrow("Node five not found in tree");- });-});--describe("CollapsibleTree edge cases", () => {- let tree: CollapsibleTree<string>;-- beforeEach(() => {- tree = CollapsibleTree.from(["A", "B", "C", "D"]);- });-- it("handles insert at start and end", () => {- tree = tree.insertAtStart("Z");- expect(tree.topLevelIds).toEqual(["Z", "A", "B", "C", "D"]);- tree = tree.insertAtEnd("Y");- expect(tree.topLevelIds).toEqual(["Z", "A", "B", "C", "D", "Y"]);- });-- it("handles edge cases for first and last", () => {- expect(tree.first()).toBe("A");- expect(tree.last()).toBe("D");- tree = CollapsibleTree.from([]);- expect(() => tree.first()).toThrow("Node at index 0 not found in tree");- expect(() => tree.last()).toThrow("Node at index -1 not found in tree");- });-});--describe("MultiColumn", () => {- let multiColumn: MultiColumn<string>;- let columnIds: CellColumnId[];-- beforeEach(() => {- multiColumn = MultiColumn.from([- ["A1", "A2", "A3"],- ["B1", "B2"],- ["C1", "C2", "C3", "C4"],- ]);-- columnIds = multiColumn.getColumns().map((c) => c.id);- });-- it("initializes correctly", () => {- expect(multiColumn.colLength).toBe(3);- expect(multiColumn.idLength).toBe(9);- expect(multiColumn.topLevelIds).toEqual([- ["A1", "A2", "A3"],- ["B1", "B2"],- ["C1", "C2", "C3", "C4"],- ]);- });-- it("creates from empty array", () => {- const emptyMultiColumn = MultiColumn.from([]);- expect(emptyMultiColumn.colLength).toBe(1);- expect(emptyMultiColumn.idLength).toBe(0);- expect(emptyMultiColumn.isEmpty()).toBe(true);- });-- it("creates from ids and columns", () => {- const idAndColumns: [string, number | undefined | null][] = [- ["A1", 0],- ["A2", 0],- ["B1", 1],- ["C1", 2],- ["C2", undefined],- ["D1", null],- ["E1", -1],- ];- const fromIdsAndColumns = MultiColumn.fromIdsAndColumns(idAndColumns);- expect(fromIdsAndColumns.colLength).toBe(3);- expect(fromIdsAndColumns.topLevelIds).toEqual([- ["A1", "A2"],- ["B1"],- ["C1", "C2", "D1", "E1"],- ]);- });-- it("iterates top-level ids", () => {- const ids = [...multiColumn.iterateTopLevelIds];- expect(ids).toEqual(["A1", "A2", "A3", "B1", "B2", "C1", "C2", "C3", "C4"]);- });-- it("gets in-order ids", () => {- expect(multiColumn.inOrderIds).toEqual([- "A1",- "A2",- "A3",- "B1",- "B2",- "C1",- "C2",- "C3",- "C4",- ]);- });-- it("checks if it has only one column", () => {- expect(multiColumn.hasOnlyOneColumn()).toBe(false);- const singleColumn = MultiColumn.from([["A1", "A2"]]);- expect(singleColumn.hasOnlyOneColumn()).toBe(true);- });-- it("checks if it has only one id", () => {- expect(multiColumn.hasOnlyOneId()).toBe(false);- const singleId = MultiColumn.from([["A1"], ["B1"], ["C1"]]);- expect(singleId.hasOnlyOneId()).toBe(false);- const singleIdSingleColumn = MultiColumn.from([["A1"]]);- expect(singleIdSingleColumn.hasOnlyOneId()).toBe(true);- });-- it("adds a column", () => {- const columnId = multiColumn.getColumns()[0].id;- const newMultiColumn = multiColumn.addColumn(columnId);- expect(newMultiColumn.colLength).toBe(4);- expect(newMultiColumn.topLevelIds[1]).toEqual([]);- });-- it("inserts a breakpoint", () => {- const withBreakpoint = multiColumn.insertBreakpoint("C3");- expect(withBreakpoint.colLength).toBe(4);- expect(withBreakpoint.topLevelIds).toEqual([- ["A1", "A2", "A3"],- ["B1", "B2"],- ["C1", "C2"],- ["C3", "C4"],- ]);- });-- it("deletes a column", () => {- const withoutBreakpoint = multiColumn.delete(columnIds[1]);- expect(withoutBreakpoint.colLength).toBe(2);- expect(withoutBreakpoint.topLevelIds).toEqual([- ["A1", "A2", "A3", "B1", "B2"],- ["C1", "C2", "C3", "C4"],- ]);- });-- it("deletes a column with only one column", () => {- const singleColumn = MultiColumn.from([["A1", "A2"]]);- const deleted = singleColumn.delete(columnIds[0]);- expect(deleted.colLength).toBe(1);- expect(deleted.topLevelIds).toEqual([["A1", "A2"]]);- });-- it("deletes the first column", () => {- const deleted = multiColumn.delete(columnIds[0]);- expect(deleted.colLength).toBe(2);- expect(deleted.topLevelIds).toEqual([- ["B1", "B2", "A1", "A2", "A3"],- ["C1", "C2", "C3", "C4"],- ]);- });-- it("merges all columns", () => {- const merged = multiColumn.mergeAllColumns();- expect(merged.colLength).toBe(1);- expect(merged.topLevelIds).toEqual([- ["A1", "A2", "A3", "B1", "B2", "C1", "C2", "C3", "C4"],- ]);- });-- it("moves a column", () => {- const moved = multiColumn.moveColumn(columnIds[0], columnIds[2]);- expect(moved.topLevelIds).toEqual([- ["B1", "B2"],- ["C1", "C2", "C3", "C4"],- ["A1", "A2", "A3"],- ]);- });-- it("moves a column before", () => {- const moved = multiColumn.moveColumn(columnIds[1], columnIds[0]);- expect(moved.topLevelIds).toEqual([- ["B1", "B2"],- ["A1", "A2", "A3"],- ["C1", "C2", "C3", "C4"],- ]);- });-- it("moves to the left", () => {- const moved = multiColumn.moveColumn(columnIds[1], "_left_");- expect(moved.topLevelIds).toEqual([- ["B1", "B2"],- ["A1", "A2", "A3"],- ["C1", "C2", "C3", "C4"],- ]);- });-- it("moves to the right", () => {- const moved = multiColumn.moveColumn(columnIds[1], "_right_");- expect(moved.topLevelIds).toEqual([- ["A1", "A2", "A3"],- ["C1", "C2", "C3", "C4"],- ["B1", "B2"],- ]);- });-- it("gets column with id", () => {- const column = multiColumn.findWithId("B2");- expect(multiColumn.indexOf(column)).toBe(1);- expect(column.topLevelIds).toEqual(["B1", "B2"]);- });-- it("transforms by id", () => {- const transformed = multiColumn.transformWithCellId("B1", (tree) =>- tree.delete("B1"),- );- expect(transformed.topLevelIds[1]).toEqual(["B2"]);- });-- it("transforms all columns", () => {- const transformed = multiColumn.transformAll((tree) =>- tree.insertAtStart("NEW"),- );- expect(transformed.topLevelIds).toEqual([- ["NEW", "A1", "A2", "A3"],- ["NEW", "B1", "B2"],- ["NEW", "C1", "C2", "C3", "C4"],- ]);- });-- it("inserts an id", () => {- const inserted = multiColumn.insertId("D1", columnIds[1], 1 as CellIndex);- expect(inserted.topLevelIds[1]).toEqual(["B1", "D1", "B2"]);- });-- it("compacts", () => {- const withEmpty = MultiColumn.from([["A1"], [], ["C1"], []]);- const compacted = withEmpty.compact();- expect(compacted.colLength).toBe(2);- expect(compacted.topLevelIds).toEqual([["A1"], ["C1"]]);- });-- it("handles errors", () => {- expect(() => multiColumn.findWithId("Z1")).toThrow(- "Cell Z1 not found in any column",- );- expect(multiColumn.colLength).toBeGreaterThan(2);- expect(() => multiColumn.delete("123" as CellColumnId)).toThrow(- /Column 123 not found/,- );- });-- it("checks if it's empty", () => {- expect(multiColumn.isEmpty()).toBe(false);- const emptyMultiColumn = MultiColumn.from([[]]);- expect(emptyMultiColumn.isEmpty()).toBe(true);- });-- it("gets columns", () => {- const columns = multiColumn.getColumns();- expect(columns.length).toBe(3);- expect(columns[0].topLevelIds).toEqual(["A1", "A2", "A3"]);- });-- it("gets index of a column", () => {- const column = multiColumn.atOrThrow(1);- const index = multiColumn.indexOf(column);- expect(index).toBe(1);- });-- it("handles at and atOrThrow", () => {- expect(multiColumn.at(1)?.topLevelIds).toEqual(["B1", "B2"]);- expect(multiColumn.atOrThrow(1).topLevelIds).toEqual(["B1", "B2"]);- expect(multiColumn.at(5)).toBeUndefined();- expect(() => multiColumn.atOrThrow(5)).toThrow("Column 5 not found");- });-- it("handles inserting at out-of-bounds indices", () => {- const multiColumn = MultiColumn.from([["A1"], ["B1"]]);- const columnId = multiColumn.atOrThrow(1).id;- const inserted = multiColumn.insertId("C1", columnId, 10 as CellIndex);- expect(inserted.topLevelIds).toEqual([["A1"], ["B1", "C1"]]);- });-- it("handles moving a column to its own position", () => {- const multiColumn = MultiColumn.from([["A1"], ["B1"], ["C1"]]);- const moved = multiColumn.moveColumn(columnIds[1], columnIds[1]);- expect(moved.topLevelIds).toEqual([["A1"], ["B1"], ["C1"]]);- });-- it("handles compacting when all columns are empty", () => {- const multiColumn = MultiColumn.from([[], [], []]);- const compacted = multiColumn.compact();- expect(compacted.colLength).toBe(1);- expect(compacted.isEmpty()).toBe(true);- });-- it("handles transformById when id is not found", () => {- const multiColumn = MultiColumn.from([["A1"], ["B1"]]);- const transformed = multiColumn.transformWithCellId("C1", (tree) =>- tree.insertAtEnd("C2"),- );- expect(transformed).toBe(multiColumn);- });-- it("handles multiple operations in sequence", () => {- let multiColumn = MultiColumn.from([- ["A1", "A2"],- ["B1", "B2"],- ["C1", "C2"],- ]);- const columnIds = multiColumn.getColumns().map((c) => c.id);- multiColumn = multiColumn.insertId("D1", columnIds[2], 1 as CellIndex);- expect(multiColumn.topLevelIds).toEqual([- ["A1", "A2"],- ["B1", "B2"],- ["C1", "D1", "C2"],- ]);- multiColumn = multiColumn.transformWithCellId("B1", (tree) =>- tree.delete("B1"),- );- expect(multiColumn.topLevelIds).toEqual([- ["A1", "A2"],- ["B2"],- ["C1", "D1", "C2"],- ]);- multiColumn = multiColumn.moveColumn(columnIds[0], columnIds[2]);- expect(multiColumn.topLevelIds).toEqual([- ["B2"],- ["C1", "D1", "C2"],- ["A1", "A2"],- ]);- multiColumn = multiColumn.compact();- expect(multiColumn.topLevelIds).toEqual([- ["B2"],- ["C1", "D1", "C2"],- ["A1", "A2"],- ]);- });-- it("handles setup cell existence", () => {- const multiColumn = MultiColumn.from([["A1"], ["B1"], ["C1"]]);- const columnIds = multiColumn.getColumnIds();- expect(multiColumn.setupCellExists()).toBe(false);- const multiColumn2 = multiColumn.insertId(SETUP_CELL_ID, columnIds[0], 0);- expect(multiColumn2.setupCellExists()).toBe(true);- const multiColumn3 = multiColumn2.transformWithCellId(- SETUP_CELL_ID,- (tree) => tree.delete(SETUP_CELL_ID),- );- expect(multiColumn3.setupCellExists()).toBe(false);- });-- it("handles get method", () => {- const column = multiColumn.get(columnIds[1]);- expect(column?.topLevelIds).toEqual(["B1", "B2"]);- });-- it("handles get method with non-existent column id", () => {- const column = multiColumn.get("non-existent-id" as CellColumnId);- expect(column).toBeUndefined();- });-- it("handles indexOfOrThrow method", () => {- expect(multiColumn.indexOfOrThrow(columnIds[1])).toBe(1);- });-- it("handles indexOfOrThrow method with non-existent column id", () => {- expect(() =>- multiColumn.indexOfOrThrow("non-existent-id" as CellColumnId),- ).toThrow("Column non-existent-id not found. Possible values: ");- });-- it("handles transform method", () => {- const transformed = multiColumn.transform(columnIds[1], (tree) =>- tree.delete("B1"),- );- expect(transformed.topLevelIds[1]).toEqual(["B2"]);- });-- it("handles transform method with non-existent column id", () => {- const transformed = multiColumn.transform(- "non-existent-id" as CellColumnId,- (tree) => tree.delete("B2"),- );- const columns1 = multiColumn.getColumns();- const columns2 = transformed.getColumns();- const allEqual = columns1.every((c, i) => c.equals(columns2[i]));- expect(allEqual).toBe(true);- expect(transformed).toBe(multiColumn);- });-- it("handles inserting a breakpoint at the start of a column", () => {- const withBreakpoint = multiColumn.insertBreakpoint("A1");- expect(withBreakpoint.colLength).toBe(4);- expect(withBreakpoint.topLevelIds).toEqual([- [],- ["A1", "A2", "A3"],- ["B1", "B2"],- ["C1", "C2", "C3", "C4"],- ]);- });-- it("handles inserting a breakpoint at the end of a column", () => {- const withBreakpoint = multiColumn.insertBreakpoint("A3");- expect(withBreakpoint.colLength).toBe(4);- expect(withBreakpoint.topLevelIds).toEqual([- ["A1", "A2"],- ["A3"],- ["B1", "B2"],- ["C1", "C2", "C3", "C4"],- ]);- });-- it("handles inserting a breakpoint in a single-item column", () => {- const singleItemColumn = MultiColumn.from([["A1"], ["B1"], ["C1"]]);- const withBreakpoint = singleItemColumn.insertBreakpoint("B1");- expect(withBreakpoint.colLength).toBe(4);- expect(withBreakpoint.topLevelIds).toEqual([["A1"], [], ["B1"], ["C1"]]);- });-});diff --git a/frontend/src/utils/id-tree.tsx b/frontend/src/utils/id-tree.tsxdeleted file mode 100644index 0f595d8abc..0000000000--- a/frontend/src/utils/id-tree.tsx+++ /dev/null@@ -1,609 +0,0 @@-/* Copyright 2026 Marimo. All rights reserved. */--import { Memoize } from "typescript-memoize";-import { reorderColumnSizes } from "@/components/editor/columns/storage";-import { SETUP_CELL_ID } from "@/core/cells/ids";-import { arrayDelete, arrayInsert, arrayMove } from "./arrays";-import { Logger } from "./Logger";--/**- * Branded number to help with type safety- */-export type CellColumnIndex = number & { __brand: "CellColumnIndex" };--/**- * Branded string to help with type safety- */-export type CellColumnId = string & { __brand: "CellColumnId" };-/**- * Weakly-branded number, since making `__brand` required causes type errors- */-export type CellIndex = number & { __brand?: "CellIndex" };--/**- * Tree data structure for handling ids with nested children- */-export class TreeNode<T> {- public value: T;-- public constructor(value: T) {- this.value = value;- }-- public toString(): string {- return String(this.value);- }-- public equals(other: TreeNode<T>): boolean {- return this.value === other.value;- }-}--let uniqueId = 0;--export class CollapsibleTree<T> {- public readonly nodes: TreeNode<T>[];- public readonly id: CellColumnId;-- private constructor(nodes: TreeNode<T>[], id: CellColumnId) {- this.nodes = nodes;- this.id = id;- }-- /** A column of `ids`, keeping `id` when a column is rebuilt. */- public static from<T>(ids: T[], id?: CellColumnId): CollapsibleTree<T> {- return new CollapsibleTree(- ids.map((id) => new TreeNode(id)),- id ?? (`tree_${uniqueId++}` as CellColumnId),- );- }-- public withNodes(nodes: TreeNode<T>[]): CollapsibleTree<T> {- return new CollapsibleTree(nodes, this.id);- }-- @Memoize()- public get topLevelIds(): T[] {- return this.nodes.map((n) => n.value);- }-- public get inOrderIds(): T[] {- return this.topLevelIds;- }-- @Memoize()- public get idSet(): Set<T> {- return new Set(this.inOrderIds);- }-- public get length(): number {- return this.nodes.length;- }-- @Memoize()- public get _nodeMap(): Map<T, TreeNode<T>> {- const result = new Map<T, TreeNode<T>>();- for (const node of this.nodes) {- result.set(node.value, node);- }- return result;- }-- /**- * Get the index of the given node, or throw- */- public indexOfOrThrow(id: T): CellIndex {- const index = this.nodes.findIndex((n) => n.value === id);- if (index === -1) {- throw new Error(- `Node ${id} not found in tree. Valid ids: ${this.topLevelIds}`,- );- }- return index as CellIndex;- }-- /**- * Get the top level nodes in the given range.- * This does not include descendants.- */- public slice(start: number, end: number): T[] {- return this.nodes.slice(start, end).map((n) => n.value);- }-- /**- * Get the node at the given index- */- public at(index: number): T | undefined {- return this.nodes.at(index)?.value;- }-- /**- * Get the node at the given index- */- public atOrThrow(index: number): T {- const node = this.nodes.at(index);- if (node === undefined) {- throw new Error(`Node at index ${index} not found in tree`);- }- return node.value;- }-- /**- * Get the first node, or throw- */- public first(): T {- return this.atOrThrow(0);- }-- /**- * Get the last node, or throw- */- public last(): T {- return this.atOrThrow(this.nodes.length - 1);- }-- /**- * Get the next node after the given node, does not wrap.- */- public after(id: T): T | undefined {- const index = this.indexOfOrThrow(id);- if (index === this.nodes.length - 1) {- return undefined;- }- return this.at(index + 1);- }-- /**- * Get the previous node before the given node, does not wrap.- */- public before(id: T): T | undefined {- const index = this.indexOfOrThrow(id);- if (index === 0) {- return undefined;- }- return this.at(index - 1);- }-- /**- * Insert a node at the given index- */- public insert(id: T, index: number): CollapsibleTree<T> {- return this.withNodes(arrayInsert(this.nodes, index, new TreeNode(id)));- }-- /**- * Insert a node at the end- */- public insertAtEnd(id: T): CollapsibleTree<T> {- return this.insert(id, this.nodes.length);- }-- /**- * Insert a node at the start- */- public insertAtStart(id: T): CollapsibleTree<T> {- return this.insert(id, 0);- }-- public deleteAtIndex(idx: number): CollapsibleTree<T> {- this.atOrThrow(idx);- return this.withNodes(arrayDelete(this.nodes, idx));- }-- public delete(id: T): CollapsibleTree<T> {- const index = this.indexOfOrThrow(id);- return this.deleteAtIndex(index);- }-- /**- * Split the tree into two trees- * @param id the id of the node to split at- * @returns a tuple of the left and right trees- */- public split(id: T): [CollapsibleTree<T>, CollapsibleTree<T> | undefined] {- const index = this.nodes.findIndex((n) => n.value === id);- if (index === -1) {- throw new Error(`Node ${id} not found in tree`);- }- const leftNodes = this.nodes.slice(0, index);- const rightNodes = this.nodes.slice(index);- if (leftNodes.length === 0) {- return [CollapsibleTree.from<T>([]), this.withNodes(rightNodes)];- }- const left = this.withNodes(leftNodes);- const right = CollapsibleTree.from<T>([]).withNodes(rightNodes);- return [left, right];- }-- public equals(other: CollapsibleTree<T>): boolean {- return (- this.nodes.length === other.nodes.length &&- this.nodes.every((n, i) => n.value === other.nodes[i].value)- );- }-- public toString(): string {- return this.nodes.map((node) => `${node.toString()}\n`).join("");- }-}--export class MultiColumn<T> {- private readonly columns: readonly CollapsibleTree<T>[];-- public constructor(columns: readonly CollapsibleTree<T>[]) {- this.columns = columns;-- // Ensure there is always at least one column- if (columns.length === 0) {- this.columns = [CollapsibleTree.from([])];- }- }-- public static from<T>(idsList: T[][]): MultiColumn<T> {- return new MultiColumn(idsList.map((ids) => CollapsibleTree.from(ids)));- }-- /**- * Create a new MultiColumn from idsList,- * attempting to preserve structure from previous MultiColumn if possible.- */- public static fromWithPreviousShape<T>(- idsList: T[],- previousShape: MultiColumn<T>,- ): MultiColumn<T> {- if (!previousShape) {- return MultiColumn.from([idsList]);- }-- // Get previous columns for reference- const previousColumns = previousShape.getColumns();-- // Split the ids into their respective columns- const splitIds: T[][] = Array.from(- { length: previousColumns.length },- () => [],- );- let lastUsedColumnIdx = 0;- for (const id of idsList) {- const column = previousColumns.findIndex((c) => c.idSet.has(id));- if (column === -1) {- // No column found, use the last used column- splitIds[lastUsedColumnIdx].push(id);- } else {- lastUsedColumnIdx = column;- splitIds[column].push(id);- }- }-- const newColumns = splitIds.map((ids, idx) =>- CollapsibleTree.from(ids, previousColumns[idx].id),- );-- return new MultiColumn(newColumns);- }-- @Memoize()- public isEmpty(): boolean {- if (this.columns.length === 0) {- return true;- }- return this.columns.every((c) => c.nodes.length === 0);- }-- public static fromIdsAndColumns<T>(- idAndColumns: [T, number | undefined | null][],- ): MultiColumn<T> {- // If column is undefined, use the previous column- // Ensure there is always at least one column- const numColumns = Math.max(- 1,- ...idAndColumns.map(([, column]) => (column ?? 0) + 1),- );- const idsList: T[][] = Array.from({ length: numColumns }, () => []);-- let prevColumn = 0;- for (const [id, column] of idAndColumns) {- // Avoid negative column indices- if (column === undefined || column === null || column < 0) {- idsList[prevColumn].push(id);- } else {- idsList[column].push(id);- prevColumn = column;- }- }-- return MultiColumn.from(idsList);- }-- @Memoize()- public get topLevelIds(): T[][] {- return this.columns.map((c) => c.topLevelIds);- }-- public get iterateTopLevelIds(): Iterable<T> {- const columns = this.columns;-- function* iter() {- for (const column of columns) {- for (const id of column.topLevelIds) {- yield id;- }- }- }-- return iter();- }-- @Memoize()- public get inOrderIds(): T[] {- return this.columns.flatMap((c) => c.inOrderIds);- }-- public get colLength(): number {- return this.columns.length;- }-- @Memoize()- public get idLength(): number {- return this.columns.reduce((acc, c) => acc + c.nodes.length, 0);- }-- @Memoize()- public get _columnMap(): Map<CellColumnId, CollapsibleTree<T>> {- return new Map(this.columns.map((c) => [c.id, c]));- }-- public at(idx: number): CollapsibleTree<T> | undefined {- return this.columns[idx];- }-- public get(columnId: CellColumnId): CollapsibleTree<T> | undefined {- return this._columnMap.get(columnId);- }-- public atOrThrow(idx: number): CollapsibleTree<T> {- const column = this.columns[idx];- if (!column) {- throw new Error(`Column ${idx} not found`);- }- return column;- }-- public hasOnlyOneColumn(): boolean {- return this.columns.length === 1;- }-- public getColumns(): readonly CollapsibleTree<T>[] {- return this.columns;- }-- public hasOnlyOneId(): boolean {- return this.idLength === 1;- }-- @Memoize()- public getColumnIds(): CellColumnId[] {- return this.columns.map((c) => c.id);- }-- public indexOf(column: CollapsibleTree<T>): number {- return this.columns.indexOf(column);- }-- public addColumn(- columnId: CellColumnId,- initialIds: T[] = [],- ): MultiColumn<T> {- return new MultiColumn(- this.columns.flatMap((c) => {- if (c.id === columnId) {- return [c, CollapsibleTree.from(initialIds)];- }- return [c];- }),- );- }-- public insertBreakpoint(cellId: T): MultiColumn<T> {- const column = this.findWithId(cellId);- const [left, right] = column.split(cellId);- const newColumns = this.columns.flatMap((c) => {- if (c === column) {- return [left, right].filter(Boolean);- }- return [c];- });- return new MultiColumn(newColumns);- }-- public delete(columnId: CellColumnId): MultiColumn<T> {- // Move cells to preceding column- // If its the first column, move the cells to the next column-- // Noop if there is only one column- if (this.columns.length <= 1) {- return this;- }-- const columnIndex = this.indexOfOrThrow(columnId);- const targetColumnIndex = columnIndex === 0 ? 1 : columnIndex - 1;-- const columns = [...this.columns];- const column = columns[columnIndex];- columns[targetColumnIndex] = column.withNodes([- ...columns[targetColumnIndex].nodes,- ...column.nodes,- ]);- columns.splice(columnIndex, 1);- return new MultiColumn(columns);- }-- public mergeAllColumns(): MultiColumn<T> {- if (this.columns.length <= 1) {- return this;- }-- const nodes = this.columns.flatMap((c) => c.nodes);- const firstColumn = this.columns[0];- return new MultiColumn([firstColumn.withNodes(nodes)]);- }-- public indexOfOrThrow(id: CellColumnId): number {- const index = this.columns.findIndex((c) => c.id === id);- if (index === -1) {- throw new Error(- `Column ${id} not found. Possible values: ${this.getColumnIds().join(", ")}`,- );- }- return index;- }-- public moveColumn(- fromCol: CellColumnId,- toCol: CellColumnId | "_left_" | "_right_",- ): MultiColumn<T> {- if (fromCol === toCol) {- return this;- }- const fromIdx = this.indexOfOrThrow(fromCol);- const toIdx =- toCol === "_left_"- ? fromIdx - 1- : toCol === "_right_"- ? fromIdx + 1- : this.indexOfOrThrow(toCol);-- reorderColumnSizes(fromIdx, toIdx);- return new MultiColumn(arrayMove([...this.columns], fromIdx, toIdx));- }-- public findWithId(id: T): CollapsibleTree<T> {- const found = this.columns.find((c) => {- return c.inOrderIds.includes(id);- });- if (!found) {- Logger.log(- `Possible values: ${this.columns.map((c) => c.inOrderIds).join(", ")}`,- );- throw new Error(`Cell ${id} not found in any column`);- }- return found;- }-- /**- * Transform the column containing the given cell id- * @param id the id of the cell to transform- * @param fn the function to transform the column- * @returns new MultiColumn with the transformed column- * If the column was not updated, we return the object.- */- public transformWithCellId(- id: T,- fn: (tree: CollapsibleTree<T>) => CollapsibleTree<T>,- ): MultiColumn<T> {- let didChange = false;-- const columns = this.columns.map((c) => {- if (c.inOrderIds.includes(id)) {- const newColumn = fn(c);- if (c !== newColumn) {- didChange = true;- }- return newColumn;- }- return c;- });-- // Avoid unnecessary re-renders- if (!didChange) {- return this;- }-- return new MultiColumn(columns);- }-- public setupCellExists(): boolean {- return this.columns.some((c) => c.topLevelIds.includes(SETUP_CELL_ID as T));- }-- public insertId(id: T, col: CellColumnId, index: CellIndex): MultiColumn<T> {- return this.transform(col, (c) => c.insert(id, index));- }-- public compact(): MultiColumn<T> {- // Don't compact if there's only one column- if (this.columns.length === 1) {- return this;- }- // If no need to compact, return the same tree- // to avoid unnecessary re-renders- const someEmpty = this.columns.some((c) => c.nodes.length === 0);- if (!someEmpty) {- return this;- }-- return new MultiColumn(this.columns.filter((c) => c.nodes.length > 0));- }-- /**- * Transform the column with the given column id- */- public transform(- columnId: CellColumnId,- fn: (tree: CollapsibleTree<T>) => CollapsibleTree<T>,- ): MultiColumn<T> {- // Use columnMap for faster lookup- const column = this._columnMap.get(columnId);- if (!column) {- Logger.warn(`Column ${columnId} not found`);- return this;- }-- const newColumn = fn(column);- if (column === newColumn) {- return this;- }-- const newColumns = this.columns.map((c) => {- if (c.id === columnId) {- return newColumn;- }- return c;- });-- return new MultiColumn(newColumns);- }-- /**- * Apply a transformation function to all columns- * @param fn The function to transform each column- * @returns A new MultiColumn if any changes were made, otherwise this- */- public transformAll(- fn: (tree: CollapsibleTree<T>) => CollapsibleTree<T>,- ): MultiColumn<T> {- let didChange = false;-- // Apply the transformation to all columns- const newColumns = this.columns.map((column) => {- const newColumn = fn(column);- if (!column.equals(newColumn)) {- didChange = true;- }- return newColumn;- });-- // Avoid unnecessary re-renders if nothing changed- if (!didChange) {- return this;- }-- return new MultiColumn(newColumns);- }-}--export const visibleForTesting = {- reset: () => {- uniqueId = 0;- },-};