Skip to content

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

fix: moveToNextCell at the end

Myles Scolnick48985bc111dd
diff --git a/frontend/src/core/cells/__tests__/cells.test.ts b/frontend/src/core/cells/__tests__/cells.test.ts
index 9f42cb0166..b3a570e3c0 100644
--- a/frontend/src/core/cells/__tests__/cells.test.ts
+++ b/frontend/src/core/cells/__tests__/cells.test.ts
@@ -2141,4 +2141,149 @@ describe("cell reducer", () => {
expect(state.cellData[SETUP_CELL_ID].edited).toBe(true);
expect(state.cellIds.inOrderIds).toContain(SETUP_CELL_ID);
});
+
+ it("can clear all outputs", () => {
+ // Add a cell and give it output
+ actions.createNewCell({
+ cellId: firstCellId,
+ before: false,
+ });
+
+ const cell1Id = cells[0].id;
+ const cell2Id = cells[1].id;
+
+ // Manually set output for the cells
+ state.cellRuntime[cell1Id].output = {
+ mimetype: "text/plain",
+ data: "output1",
+ timestamp: 0 as Seconds,
+ };
+ state.cellRuntime[cell2Id].output = {
+ mimetype: "text/plain",
+ data: "output2",
+ timestamp: 0 as Seconds,
+ };
+
+ actions.clearAllCellOutputs();
+
+ expect(state.cellRuntime[cell1Id].output).toBeNull();
+ expect(state.cellRuntime[cell2Id].output).toBeNull();
+ });
+
+ describe("moveToNextCell", () => {
+ let cell1Id: CellId;
+ let cell2Id: CellId;
+ let cell3Id: CellId;
+
+ beforeEach(() => {
+ // Create a few cells to work with
+ actions.createNewCell({ cellId: "__end__", before: false });
+ actions.createNewCell({ cellId: "__end__", before: false });
+
+ cell1Id = state.cellIds.inOrderIds[0];
+ cell2Id = state.cellIds.inOrderIds[1];
+ cell3Id = state.cellIds.inOrderIds[2];
+ });
+
+ it("creates new cell when moving after last cell with noCreate=false", () => {
+ const initialCellCount = state.cellIds.inOrderIds.length;
+
+ actions.moveToNextCell({
+ cellId: cell3Id,
+ before: false,
+ noCreate: false,
+ });
+
+ expect(state.cellIds.inOrderIds.length).toBe(initialCellCount + 1);
+ });
+
+ it("creates new cell when moving before first cell with noCreate=false", () => {
+ const initialCellCount = state.cellIds.inOrderIds.length;
+
+ actions.moveToNextCell({
+ cellId: cell1Id,
+ before: true,
+ noCreate: false,
+ });
+
+ expect(state.cellIds.inOrderIds.length).toBe(initialCellCount + 1);
+ });
+
+ it("does not create new cell when moving after last cell with noCreate=true", () => {
+ const initialCellCount = state.cellIds.inOrderIds.length;
+ const initialState = { ...state };
+
+ actions.moveToNextCell({
+ cellId: cell3Id,
+ before: false,
+ noCreate: true,
+ });
+
+ // Should not create a new cell
+ expect(state.cellIds.inOrderIds.length).toBe(initialCellCount);
+ // Should not crash or throw an error
+ expect(state.cellIds.inOrderIds).toEqual(initialState.cellIds.inOrderIds);
+ });
+
+ it("does not create new cell when moving before first cell with noCreate=true", () => {
+ const initialCellCount = state.cellIds.inOrderIds.length;
+ const initialState = { ...state };
+
+ actions.moveToNextCell({ cellId: cell1Id, before: true, noCreate: true });
+
+ // Should not create a new cell
+ expect(state.cellIds.inOrderIds.length).toBe(initialCellCount);
+ // Should not crash or throw an error
+ expect(state.cellIds.inOrderIds).toEqual(initialState.cellIds.inOrderIds);
+ });
+
+ it("focuses next cell when moving within bounds", () => {
+ const focusSpy = vi.mocked(focusAndScrollCellIntoView);
+ focusSpy.mockClear();
+
+ actions.moveToNextCell({
+ cellId: cell1Id,
+ before: false,
+ noCreate: true,
+ });
+
+ expect(focusSpy).toHaveBeenCalledWith({
+ cellId: cell2Id,
+ cell: state.cellHandles[cell2Id],
+ config: state.cellData[cell2Id].config,
+ codeFocus: "top",
+ variableName: undefined,
+ });
+ });
+
+ it("focuses previous cell when moving backward within bounds", () => {
+ const focusSpy = vi.mocked(focusAndScrollCellIntoView);
+ focusSpy.mockClear();
+
+ actions.moveToNextCell({ cellId: cell2Id, before: true, noCreate: true });
+
+ expect(focusSpy).toHaveBeenCalledWith({
+ cellId: cell1Id,
+ cell: state.cellHandles[cell1Id],
+ config: state.cellData[cell1Id].config,
+ codeFocus: "bottom",
+ variableName: undefined,
+ });
+ });
+
+ it("does not move focus from scratch cell", () => {
+ const focusSpy = vi.mocked(focusAndScrollCellIntoView);
+ focusSpy.mockClear();
+ const initialState = { ...state };
+
+ actions.moveToNextCell({
+ cellId: "__scratch__" as CellId,
+ before: false,
+ noCreate: false,
+ });
+
+ expect(focusSpy).not.toHaveBeenCalled();
+ expect(state.cellIds.inOrderIds).toEqual(initialState.cellIds.inOrderIds);
+ });
+ });
});
diff --git a/frontend/src/core/cells/cells.ts b/frontend/src/core/cells/cells.ts
index c36e1f8847..dec7bbea4f 100644
--- a/frontend/src/core/cells/cells.ts
+++ b/frontend/src/core/cells/cells.ts
@@ -881,9 +881,13 @@ const {
const column = state.cellIds.findWithId(cellId);
const index = column.indexOfOrThrow(cellId);
const nextCellIndex = before ? index - 1 : index + 1;
+
+ const isPastLastCell = nextCellIndex === column.length;
+ const isBeforeFirstCell = nextCellIndex === -1;
+
// Create a new cell at the end; no need to update scrollKey,
// because cell will be created with autoScrollIntoView
- if (nextCellIndex === column.length && !noCreate) {
+ if (isPastLastCell && !noCreate) {
const newCellId = CellId.create();
return {
...state,
@@ -905,7 +909,7 @@ const {
// scrollKey
}
- if (nextCellIndex === -1 && !noCreate) {
+ if (isBeforeFirstCell && !noCreate) {
const newCellId = CellId.create();
return {
...state,
@@ -925,8 +929,14 @@ const {
};
}
- if (nextCellIndex !== -1) {
- // Don't wrap around from top to bottom
+ if ((isPastLastCell || isBeforeFirstCell) && noCreate) {
+ // Trying to move past the last cell or before the first cell
+ // with noCreate=true - do nothing
+ return state;
+ }
+
+ if (nextCellIndex >= 0 && nextCellIndex < column.length) {
+ // Move to valid cell within bounds
const nextCellId = column.atOrThrow(nextCellIndex);
// Just focus, no state change
focusAndScrollCellIntoView({
@@ -937,6 +947,7 @@ const {
variableName: undefined,
});
}
+
return state;
},
scrollToTarget: (state) => {