Файли
openidea/e2e/notebookRunCell.spec.ts
T
ntbang0901 d61ffa5430 feat(notebook): run cells from the keyboard with Ctrl/Cmd+Enter and Shift+Enter (#7845)
Ctrl+Enter (also Cmd+Enter on macOS) runs the notebook cell and keeps the caret in it, and Shift+Enter runs it and selects the next cell, adding a code cell at the end of the notebook. Run Cell, Run Cell and Select Below and Run Cell and Insert Below are actions in Find Action and the keymap settings; they take these keys only while the notebook has focus (notebookFocus), ahead of the editor's Split Line / Start New Line, and the notebook's own key handler is gone. Markdown cells render instead of running; while a cell runs, the keys do nothing on a code cell. Alt+Enter stays Show Intentions, so Insert Below has no default key except in the VS Code keymaps.

Closes #7624

Co-authored-by: ntbang0901 <[email protected]>
2026-12-04 01:27:05 +08:00

116 рядки
4.4 KiB
TypeScript

import type { Page } from "@playwright/test";
import { expect, openFromTree, stubDesktop, test } from "./fixtures";
const NOTEBOOK_PATH = "/dev/acme-api/run-cells.ipynb";
/** Opens a notebook whose kernel answers a cell with its source as output. */
async function openNotebook(page: Page) {
await openFromTree(page, "README.md");
await stubDesktop(page);
await page.evaluate(async (path) => {
const cell = (id: string, cellType: string, source: string) => ({
id,
cellType,
source,
metadata: {},
attachments: {},
executionCount: null as number | null,
outputs: [] as unknown[],
});
let runs = 0;
let added = 0;
const snapshot = {
id: "run-cells",
path,
version: 1,
dirty: false,
nbformat: 4,
nbformatMinor: 5,
metadata: {},
canUndo: false,
canRedo: false,
cells: [
cell("load", "code", "orders = [12, 30, 8]"),
cell("notes", "markdown", "## Totals"),
cell("total", "code", "sum(orders)"),
],
};
const answer = () => {
snapshot.version++;
return structuredClone(snapshot);
};
Object.assign(window.__ipc.handlers, {
notebook_open: () => structuredClone(snapshot),
notebook_get: () => structuredClone(snapshot),
notebook_cell_docs: () => [],
sdk_detect: () => [],
sdk_python_effective: () => null,
jupyter_servers: () => [],
notebook_edit: ({ edit }: { edit: Record<string, string> }) => {
const at = snapshot.cells.findIndex((c) => c.id === (edit.cell ?? edit.after));
if (edit.type === "setSource") snapshot.cells[at]!.source = edit.source!;
else if (edit.type === "insertCell")
snapshot.cells.splice(at + 1, 0, cell(`added-${++added}`, edit.cellType!, ""));
return answer();
},
notebook_execute: ({ cells }: { cells: string[] }) => {
const run = snapshot.cells.find((c) => c.id === cells[0])!;
run.executionCount = ++runs;
run.outputs = [{ output_type: "stream", name: "stdout", text: `ran: ${run.source}\n` }];
return answer();
},
});
const module = "/src/workbench/contributions.ts";
const { editorService } = await import(/* @vite-ignore */ module);
await editorService().openFile(path, { preview: false });
}, NOTEBOOK_PATH);
await expect(region(page, 1)).toBeVisible();
}
const region = (page: Page, n: number) => page.getByRole("region", { name: new RegExp(`^Cell ${n}: `) });
const text = (page: Page, n: number) => region(page, n).locator(".cm-content");
const runs = (page: Page) =>
page.evaluate(() =>
window.__ipc.calls.filter((c) => c.cmd === "notebook_execute").flatMap((c) => c.args.cells as string[]),
);
test("Run Cell keeps the caret in the cell; Shift+Enter steps through and adds a cell at the end", async ({
workbench: page,
}) => {
await openNotebook(page);
await text(page, 1).click();
await page.keyboard.press("Meta+Enter");
await expect(region(page, 1)).toContainText("ran: orders = [12, 30, 8]");
await expect(text(page, 1)).toBeFocused();
await page.keyboard.press("Control+Enter");
await expect.poll(() => runs(page)).toEqual(["load", "load"]);
await page.keyboard.press("Shift+Enter");
await expect(region(page, 2)).toBeFocused();
await expect(region(page, 2)).toHaveClass(/selected/);
await page.keyboard.press("Enter");
await expect(text(page, 2)).toBeFocused();
await page.keyboard.press("Shift+Enter");
await expect(text(page, 2)).toHaveCount(0);
await expect(region(page, 3)).toBeFocused();
await page.keyboard.press("Shift+Enter");
await expect(text(page, 4)).toBeFocused();
await page.keyboard.type("1");
await expect(text(page, 4)).toHaveText("1");
expect(await runs(page)).toEqual(["load", "load", "load", "total"]);
await expect(text(page, 1)).toHaveText("orders = [12, 30, 8]");
await expect(text(page, 3)).toHaveText("sum(orders)");
});
test("the run actions are in Find Action and act on the notebook behind it", async ({ workbench: page }) => {
await openNotebook(page);
await text(page, 1).click();
await page.keyboard.press("Meta+Shift+A");
await page.keyboard.type("Run Cell and Insert Below");
await page.getByRole("option", { name: /Run Cell and Insert Below/ }).waitFor();
await page.keyboard.press("Enter");
await expect(text(page, 2)).toBeFocused();
await expect(region(page, 3)).toContainText("Totals");
expect(await runs(page)).toEqual(["load"]);
});