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.

Bind CodeMirror editors to document cell code (#11446)

Trevor Manz2871725f7591
diff --git a/frontend/src/core/codemirror/cells/__tests__/extensions.test.ts b/frontend/src/core/codemirror/cells/__tests__/extensions.test.ts
index 8dd18b3d62..e1bd55578b 100644
--- a/frontend/src/core/codemirror/cells/__tests__/extensions.test.ts
+++ b/frontend/src/core/codemirror/cells/__tests__/extensions.test.ts
@@ -68,6 +68,21 @@ describe("shouldAutorunMarkdownUpdate", () => {
).toBe(false);
});
+ it("ignores remote transactions while focused", () => {
+ const transaction = createTransaction({
+ changes: { from: 0, insert: "#" },
+ annotations: [Transaction.remote.of(true)],
+ });
+
+ expect(
+ shouldAutorunMarkdownUpdate({
+ docChanged: transaction.docChanged,
+ transactions: [transaction],
+ hasFocus: true,
+ }),
+ ).toBe(false);
+ });
+
it("ignores programmatic doc changes without a user event", () => {
const transaction = createTransaction({
changes: { from: 0, insert: "#" },
diff --git a/frontend/src/core/codemirror/cells/extensions.ts b/frontend/src/core/codemirror/cells/extensions.ts
index e6e77f3262..966dc8a5a0 100644
--- a/frontend/src/core/codemirror/cells/extensions.ts
+++ b/frontend/src/core/codemirror/cells/extensions.ts
@@ -1,7 +1,7 @@
/* Copyright 2026 Marimo. All rights reserved. */
import { closeCompletion, completionStatus } from "@codemirror/autocomplete";
-import { type Extension, Prec } from "@codemirror/state";
+import { type Extension, Prec, Transaction } from "@codemirror/state";
import {
EditorView,
type KeyBinding,
@@ -382,8 +382,12 @@ function shouldAutorunMarkdownUpdate({
}
return transactions.some((tr) => {
- // Ignore RTC sync changes to avoid duplicate runs from remote edits.
- if (tr.annotation(loroSyncAnnotation) !== undefined) {
+ // Ignore remote changes to avoid duplicate runs. The client that made the
+ // edit runs it.
+ if (
+ tr.annotation(Transaction.remote) ||
+ tr.annotation(loroSyncAnnotation) !== undefined
+ ) {
return false;
}
diff --git a/frontend/src/core/document/__tests__/codemirror.test.ts b/frontend/src/core/document/__tests__/codemirror.test.ts
new file mode 100644
index 0000000000..12b418c36e
--- /dev/null
+++ b/frontend/src/core/document/__tests__/codemirror.test.ts
@@ -0,0 +1,483 @@
+/* Copyright 2026 Marimo. All rights reserved. */
+
+import { history, undo } from "@codemirror/commands";
+import { EditorSelection, EditorState } from "@codemirror/state";
+import { EditorView } from "@codemirror/view";
+import type { CellId } from "@marimo-team/client/protocol";
+import { atom, createStore } from "jotai/vanilla";
+import { afterEach, describe, expect, it, vi } from "vitest";
+import { cellId as brandedCellId } from "@/__tests__/branded";
+import { createNotebookActions } from "@/core/cells/cells";
+import { OverridingHotkeyProvider } from "@/core/hotkeys/hotkeys";
+import { cellActionsState } from "../../codemirror/cells/state";
+import { cellConfigExtension } from "../../codemirror/config/extension";
+import {
+ adaptiveLanguageConfiguration,
+ languageAdapterState,
+ switchLanguage,
+} from "../../codemirror/language/extension";
+import {
+ languageMetadataField,
+ setLanguageMetadata,
+} from "../../codemirror/language/metadata";
+import { SCHEMA_CACHE } from "../../codemirror/language/languages/sql/completion-store";
+import type { Document } from "../../host/resources/document";
+import { cellCodeBinding } from "../codemirror";
+import { createDocumentModel } from "../model";
+import { cell, document, withCells } from "./fixtures";
+
+const views: EditorView[] = [];
+const lifetimes: AbortController[] = [];
+
+afterEach(() => {
+ for (const view of views.splice(0)) {
+ view.destroy();
+ }
+ lifetimes.splice(0).forEach((controller) => controller.abort());
+});
+
+const completionConfig = {
+ activate_on_typing: true,
+ signature_hint_on_typing: false,
+ copilot: false,
+ codeium_api_key: null,
+} as const;
+
+/** The language configuration a marimo cell editor installs. */
+function languages(id: CellId) {
+ const options = {
+ cellId: brandedCellId(id),
+ completionConfig,
+ hotkeys: new OverridingHotkeyProvider({}),
+ placeholderType: "marimo-import",
+ lspConfig: {},
+ } as const;
+ return [
+ cellActionsState.of({
+ ...createNotebookActions(vi.fn()),
+ toggleHideCode: () => false,
+ aiCellCompletion: () => false,
+ createManyBelow: vi.fn(),
+ addOrAppendSetupCell: vi.fn(),
+ onRun: vi.fn(),
+ deleteCell: vi.fn(),
+ afterToggleMarkdown: vi.fn(),
+ afterToggleSQL: vi.fn(),
+ saveNotebook: vi.fn(),
+ }),
+ adaptiveLanguageConfiguration(options),
+ cellConfigExtension({ ...options, diagnosticsConfig: {} }),
+ ];
+}
+
+function setup({
+ initial = document(),
+ cellId = "c1",
+ doc = "",
+ adapters = false,
+}: {
+ initial?: Document | null;
+ cellId?: CellId;
+ doc?: string;
+ adapters?: boolean;
+} = {}) {
+ const store = createStore();
+ const source = atom(initial);
+ const controller = new AbortController();
+ lifetimes.push(controller);
+ const model = createDocumentModel({
+ store,
+ notebook: { documentAtom: source, edit: () => new Promise(() => {}) },
+ signal: controller.signal,
+ });
+ const editor = (text = doc) => {
+ const view = new EditorView({
+ state: EditorState.create({
+ doc: text,
+ extensions: [
+ history(),
+ adapters ? languages(cellId) : [],
+ cellCodeBinding({ store, model, cellId }),
+ ],
+ }),
+ });
+ views.push(view);
+ return view;
+ };
+ const view = editor();
+ return {
+ store,
+ source,
+ model,
+ view,
+ editor,
+ code: () => store.get(model.documentAtom)?.cells.get(cellId)?.code,
+ status: () => store.get(model.syncStatusAtom),
+ type: (insert: string) =>
+ view.dispatch({
+ changes: { from: view.state.doc.length, insert },
+ userEvent: "input.type",
+ }),
+ };
+}
+
+// Inbound changes apply in a microtask.
+const settle = () => new Promise<void>((resolve) => setTimeout(resolve, 0));
+
+describe("cellCodeBinding", () => {
+ it("seeds the editor from the cell outside undo history", async () => {
+ const { view, status } = setup();
+ await settle();
+ expect(view.state.doc.toString()).toBe("x = 1");
+ expect(undo(view)).toBe(false);
+ expect(status()).toEqual({ status: "idle" });
+ });
+
+ it("writes local changes to the draft before the update returns", async () => {
+ const { code, status, type } = setup();
+ await settle();
+ type(" + 1");
+ expect(code()).toBe("x = 1 + 1");
+ expect(status()).toEqual({ status: "pending" });
+ });
+
+ it("shows a change another subscriber makes while a push notifies", async () => {
+ const { store, model, view, type } = setup();
+ await settle();
+ // Another subscriber normalizes code synchronously, inside the push.
+ const unsubscribe = store.sub(model.documentAtom, () => {
+ const code = store.get(model.documentAtom)?.cells.get("c1")?.code;
+ if (code?.includes(" + ")) {
+ const normalized = code.replaceAll(" + ", "+");
+ store.set(model.editAtom, (tx) => tx.setCode("c1", normalized));
+ }
+ });
+ type(" + 1");
+ await settle();
+ expect(view.state.doc.toString()).toBe("x = 1+1");
+ unsubscribe();
+ });
+
+ it("keeps the selection between separate host edits", async () => {
+ const { store, source, view } = setup({
+ initial: withCells(document(), { c1: cell("c1", "a = 1\nb = 2\nc = 3") }),
+ });
+ await settle();
+ view.dispatch({ selection: EditorSelection.cursor(8) });
+
+ store.set(
+ source,
+ withCells(document(), { c1: cell("c1", "A = 1\nb = 2\nC = 3") }),
+ );
+ await settle();
+
+ expect(view.state.doc.toString()).toBe("A = 1\nb = 2\nC = 3");
+ expect(view.state.selection.main.head).toBe(8);
+ });
+
+ it("waits for composition to end before applying host changes", async () => {
+ const { store, source, view } = setup();
+ await settle();
+ let composing = true;
+ Object.defineProperty(view, "composing", { get: () => composing });
+
+ store.set(source, withCells(document(), { c1: cell("c1", "x = 2") }));
+ await settle();
+ expect(view.state.doc.toString()).toBe("x = 1");
+
+ // A composition can end without an update, so the binding watches for it.
+ composing = false;
+ view.contentDOM.dispatchEvent(new Event("compositionend"));
+ await settle();
+ expect(view.state.doc.toString()).toBe("x = 2");
+ });
+
+ it("applies a waiting host change on the first update after composition", async () => {
+ const { store, source, view } = setup();
+ await settle();
+ let composing = true;
+ Object.defineProperty(view, "composing", { get: () => composing });
+ store.set(source, withCells(document(), { c1: cell("c1", "x = 2") }));
+ await settle();
+
+ composing = false;
+ view.dispatch({ selection: EditorSelection.cursor(0) });
+ await settle();
+ expect(view.state.doc.toString()).toBe("x = 2");
+ });
+
+ it("keeps two editors on one cell in step", async () => {
+ const { editor, type } = setup();
+ const other = editor();
+ await settle();
+
+ type("!");
+ await settle();
+
+ expect(other.state.doc.toString()).toBe("x = 1!");
+ });
+
+ it("shows host code that returns to what this editor wrote", async () => {
+ const { store, source, view, type } = setup();
+ await settle();
+ type("!");
+ // The host confirms the edit, which releases the draft.
+ store.set(source, withCells(document(), { c1: cell("c1", "x = 1!") }));
+ await settle();
+
+ store.set(source, withCells(document(), { c1: cell("c1", "x = 2") }));
+ await settle();
+ expect(view.state.doc.toString()).toBe("x = 2");
+
+ store.set(source, withCells(document(), { c1: cell("c1", "x = 1!") }));
+ await settle();
+ expect(view.state.doc.toString()).toBe("x = 1!");
+ });
+
+ it("follows a cell once its code is available", async () => {
+ const absent = setup({ cellId: "c3" });
+ const withheld = setup({
+ initial: withCells(document(), { c1: cell("c1", "", { code: null }) }),
+ });
+ await settle();
+
+ absent.store.set(
+ absent.source,
+ withCells(document(), { c3: cell("c3", "new") }),
+ );
+ withheld.store.set(
+ withheld.source,
+ withCells(document(), { c1: cell("c1", "revealed") }),
+ );
+ await settle();
+
+ expect(absent.view.state.doc.toString()).toBe("new");
+ expect(withheld.view.state.doc.toString()).toBe("revealed");
+ });
+
+ it("shows the host's code after typing while the document was unavailable", async () => {
+ const { store, source, view, type, code } = setup();
+ await settle();
+
+ // All in one task, so no pull runs while the document is away.
+ store.set(source, null);
+ type(" + 1");
+ store.set(source, document());
+ await settle();
+
+ expect(view.state.doc.toString()).toBe(code());
+ });
+
+ it("leaves the editor alone for an absent cell or withheld source", async () => {
+ const absent = setup({ cellId: "c9", doc: "local" });
+ const withheld = setup({
+ initial: withCells(document(), { c1: cell("c1", "", { code: null }) }),
+ doc: "local",
+ });
+ await settle();
+
+ for (const { view, type, status } of [absent, withheld]) {
+ type("!");
+ expect(view.state.doc.toString()).toBe("local!");
+ expect(status()).toEqual({ status: "idle" });
+ }
+ });
+
+ it("stops following the cell when the view is destroyed", async () => {
+ const { store, source, view } = setup();
+ await settle();
+
+ // The change arrives before the view goes, and would apply just after.
+ store.set(source, withCells(document(), { c1: cell("c1", "x = 2") }));
+ view.destroy();
+ await settle();
+
+ expect(view.state.doc.toString()).toBe("x = 1");
+ });
+
+ describe("with language adapters", () => {
+ const language = (view: EditorView) =>
+ view.state.field(languageAdapterState).type;
+ /** What the editor's adapter reads back from cell code. */
+ const readBack = (view: EditorView, code: string | null | undefined) => {
+ const [text, , metadata] = view.state
+ .field(languageAdapterState)
+ .transformIn(code ?? "");
+ return { text, metadata };
+ };
+
+ it("edits markdown as markdown and writes Python with its metadata", async () => {
+ const { view, code, type } = setup({
+ adapters: true,
+ initial: withCells(document(), {
+ c1: cell("c1", 'mo.md(f"""hi {x}""")'),
+ }),
+ });
+ await settle();
+ expect(language(view)).toBe("markdown");
+ expect(view.state.doc.toString()).toBe("hi {x}");
+
+ type("!");
+ expect(readBack(view, code())).toMatchObject({
+ text: "hi {x}!",
+ metadata: { quotePrefix: "f" },
+ });
+ });
+
+ it("switches language for host code without rewriting it", async () => {
+ const { store, source, view, code, status } = setup({ adapters: true });
+ await settle();
+ expect(language(view)).toBe("python");
+
+ store.set(
+ source,
+ withCells(document(), { c1: cell("c1", 'mo.md("hi")') }),
+ );
+ await settle();
+
+ expect(language(view)).toBe("markdown");
+ expect(view.state.doc.toString()).toBe("hi");
+ expect(code()).toBe('mo.md("hi")');
+ expect(status()).toEqual({ status: "idle" });
+ });
+
+ it("writes changes to language metadata alone", async () => {
+ const { view, code } = setup({
+ adapters: true,
+ initial: withCells(document(), {
+ c1: cell("c1", 'mo.md("""\n# Title\n""")'),
+ }),
+ });
+ await settle();
+
+ view.dispatch({
+ effects: setLanguageMetadata.of({
+ ...view.state.field(languageMetadataField),
+ quotePrefix: "r",
+ }),
+ });
+ expect(readBack(view, code())).toMatchObject({
+ text: "# Title",
+ metadata: { quotePrefix: "r" },
+ });
+ });
+
+ it("applies a host change to language metadata alone", async () => {
+ const { store, source, view } = setup({
+ adapters: true,
+ initial: withCells(document(), {
+ c1: cell("c1", 'mo.md(r"""hi""")'),
+ }),
+ });
+ await settle();
+
+ store.set(
+ source,
+ withCells(document(), { c1: cell("c1", 'mo.md(f"""hi""")') }),
+ );
+ await settle();
+
+ expect(view.state.doc.toString()).toBe("hi");
+ expect(view.state.field(languageMetadataField)).toMatchObject({
+ quotePrefix: "f",
+ });
+ });
+
+ it("does not rewrite code when the editor switches language as it opens", async () => {
+ const original = 'mo.md("hello")';
+ const { view, code, status } = setup({
+ adapters: true,
+ doc: original,
+ initial: withCells(document(), { c1: cell("c1", original) }),
+ });
+ // The cell editor switches to the language its code implies on mount.
+ switchLanguage(view, { language: "markdown" });
+ await settle();
+
+ expect(code()).toBe(original);
+ expect(status()).toEqual({ status: "idle" });
+ });
+
+ it("keeps typed text that the adapter would normalize", async () => {
+ const { view, type } = setup({
+ adapters: true,
+ initial: withCells(document(), {
+ c1: cell("c1", 'mo.md("""\nhi\n""")'),
+ }),
+ });
+ await settle();
+
+ type("\n");
+ await settle();
+ type(" ");
+ await settle();
+
+ expect(view.state.doc.toString()).toBe("hi\n ");
+ });
+
+ it("keeps the language a person chose when the cell changes otherwise", async () => {
+ const { store, model, view } = setup({
+ adapters: true,
+ initial: withCells(document(), {
+ c1: cell("c1", 'mo.md("""\nhi\n""")'),
+ }),
+ });
+ await settle();
+ switchLanguage(view, { language: "python" });
+ await settle();
+
+ store.set(model.editAtom, (tx) => tx.setName("c1", "renamed"));
+ await settle();
+
+ expect(language(view)).toBe("python");
+ });
+
+ it("edits SQL as SQL and keeps its output name", async () => {
+ const { store, source, view, code, type } = setup({ adapters: true });
+ await settle();
+
+ store.set(
+ source,
+ withCells(document(), {
+ c1: cell("c1", 'df = mo.sql(f"""SELECT 1""")'),
+ }),
+ );
+ await settle();
+ expect(language(view)).toBe("sql");
+ expect(view.state.doc.toString()).toBe("SELECT 1");
+
+ type(" LIMIT 1");
+ expect(readBack(view, code())).toMatchObject({
+ text: "SELECT 1 LIMIT 1",
+ metadata: { dataframeName: "df" },
+ });
+ });
+
+ it("sets the SQL dialect from the host's engine", async () => {
+ const { store, source } = setup({ adapters: true });
+ await settle();
+ const dialect = vi.spyOn(SCHEMA_CACHE, "getDialect");
+ try {
+ store.set(
+ source,
+ withCells(document(), {
+ c1: cell("c1", '_df = mo.sql(f"""SELECT 1""", engine=warehouse)'),
+ }),
+ );
+ await settle();
+ expect(dialect).toHaveBeenLastCalledWith("warehouse");
+
+ store.set(
+ source,
+ withCells(document(), {
+ c1: cell("c1", '_df = mo.sql(f"""SELECT 1""", engine=lake)'),
+ }),
+ );
+ await settle();
+ expect(dialect).toHaveBeenLastCalledWith("lake");
+ } finally {
+ dialect.mockRestore();
+ }
+ });
+ });
+});
diff --git a/frontend/src/core/document/codemirror.ts b/frontend/src/core/document/codemirror.ts
new file mode 100644
index 0000000000..f08f42f384
--- /dev/null
+++ b/frontend/src/core/document/codemirror.ts
@@ -0,0 +1,311 @@
+/* Copyright 2026 Marimo. All rights reserved. */
+
+/**
+ * A CodeMirror binding between an editor and one cell's code.
+ *
+ * The editor and the document model each hold the code, and the binding keeps
+ * them equal in both directions.
+ *
+ * - Push. An update that could change the code writes it to the model before
+ * the update returns, so the model keeps pace with typing.
+ * - Pull. A model change reaches the editor in a microtask, because a view
+ * cannot dispatch while it is constructing or updating. The binding keeps
+ * the code both sides last agreed on, so the model's echo of a push is
+ * skipped while a different value, even one written during that push, is
+ * pulled. A real change is marked remote, stays out of undo history, and
+ * waits for IME composition to end.
+ *
+ * The editor shows code through marimo's language adapters, so a markdown
+ * cell edits the text inside `mo.md`. Both directions compare the cell's
+ * Python code rather than the editor text, so an adapter that normalizes what
+ * it reads back leaves typing alone.
+ *
+ * A pulled change replaces only what differs, using `diff` from
+ * `@codemirror/merge`. Separate edits stay separate, so the selection, folds,
+ * and diagnostics between them keep their place. The diff uses a scan limit of
+ * 500, as the merge view does. Past it, the search trades precision for speed,
+ * so a rewrite of a large cell costs microseconds and maps the selection
+ * coarsely instead of blocking the editor.
+ */
+
+import { diff } from "@codemirror/merge";
+import {
+ type ChangeSpec,
+ type EditorState,
+ type Extension,
+ type StateEffect,
+ Transaction,
+} from "@codemirror/state";
+import { type EditorView, ViewPlugin, type ViewUpdate } from "@codemirror/view";
+import type { CellId } from "@marimo-team/client/protocol";
+import { dequal } from "dequal";
+import type { Atom, createStore } from "jotai/vanilla";
+import {
+ languageAdapterFromCode,
+ languageAdapterState,
+ switchLanguage,
+} from "../codemirror/language/extension";
+import { initializeSQLDialect } from "../codemirror/language/languages/sql/sql";
+import {
+ languageMetadataField,
+ setLanguageMetadata,
+} from "../codemirror/language/metadata";
+import type { DocumentModel } from "./model";
+import { selectCellField } from "./select";
+
+/** The diff scan limit, matching the default of the merge view. */
+const SCAN_LIMIT = 500;
+
+interface Binding {
+ readonly store: ReturnType<typeof createStore>;
+ readonly model: DocumentModel;
+ readonly cellId: CellId;
+ /** Undefined while the cell is absent, null while its source is withheld. */
+ readonly code: Atom<string | null | undefined>;
+}
+
+/**
+ * Bind an editor to one cell's code.
+ *
+ * Editors without marimo's language configuration bind their text as is.
+ */
+export function cellCodeBinding({
+ store,
+ model,
+ cellId,
+}: {
+ store: ReturnType<typeof createStore>;
+ model: DocumentModel;
+ cellId: CellId;
+}): Extension {
+ const binding: Binding = {
+ store,
+ model,
+ cellId,
+ code: selectCellField(model.documentAtom, cellId, "code"),
+ };
+ return ViewPlugin.define((view) => new CellCodeSync(view, binding), {
+ eventObservers: {
+ compositionend() {
+ this.compositionEnded();
+ },
+ },
+ });
+}
+
+class CellCodeSync {
+ #scheduled = false;
+ /** A pull arrived during IME composition and runs once it ends. */
+ #deferred = false;
+ /**
+ * A pull is dispatching to the editor.
+ *
+ * A language switch dispatches transactions of its own before the text, and
+ * their in-between states represent the wrong code. Until the switch can be
+ * part of the pull's one remote transaction, this keeps them from pushing.
+ */
+ #pulling = false;
+ /**
+ * The code the editor and the model last agreed on.
+ *
+ * A change the model cannot take clears it, so once the cell is available
+ * again its code is pulled even if it matches what was agreed before.
+ */
+ #synced: string | undefined;
+ #destroyed = false;
+ readonly #view: EditorView;
+ readonly #binding: Binding;
+ readonly #unsubscribe: () => void;
+
+ public constructor(view: EditorView, binding: Binding) {
+ this.#view = view;
+ this.#binding = binding;
+ // A change to the agreed code is the echo of a push and needs no pull.
+ this.#unsubscribe = binding.store.sub(binding.code, () => {
+ if (binding.store.get(binding.code) !== this.#synced) {
+ this.#schedule();
+ }
+ });
+ this.#schedule();
+ }
+
+ public update(update: ViewUpdate) {
+ if (!this.#pulling && changesCode(update)) {
+ this.#push(update.state);
+ }
+ if (this.#deferred && !update.view.composing) {
+ this.#schedule();
+ }
+ }
+
+ /**
+ * Run a pull that waited for the composition.
+ *
+ * A composition that ends without changing the text need not produce an
+ * update. CodeMirror finishes handling the end in a microtask, so the pull
+ * waits for the next task.
+ */
+ public compositionEnded() {
+ if (this.#deferred) {
+ setTimeout(() => this.#schedule(), 0);
+ }
+ }
+
+ public destroy() {
+ this.#destroyed = true;
+ this.#unsubscribe();
+ }
+
+ /** Write the editor's code to the model unless the editor already shows it. */
+ #push(state: EditorState) {
+ const { store, model, cellId, code } = this.#binding;
+ const current = store.get(code);
+ if (typeof current !== "string") {
+ this.#synced = undefined;
+ return;
+ }
+ if (shows(state, current)) {
+ this.#synced = current;
+ return;
+ }
+ const next = codeOf(state);
+ this.#synced = next;
+ store.set(model.editAtom, (tx) => tx.setCode(cellId, next));
+ }
+
+ /** Show the model's code unless the editor already shows it. */
+ #pull() {
+ this.#scheduled = false;
+ const next = this.#binding.store.get(this.#binding.code);
+ if (this.#destroyed || typeof next !== "string") {
+ return;
+ }
+ if (this.#view.composing) {
+ this.#deferred = true;
+ return;
+ }
+ this.#deferred = false;
+ if (next !== this.#synced && !shows(this.#view.state, next)) {
+ this.#pulling = true;
+ try {
+ replaceCode(this.#view, next);
+ } finally {
+ this.#pulling = false;
+ }
+ }
+ this.#synced = next;
+ }
+
+ /** Pull once after the current update, however many changes arrive. */
+ #schedule() {
+ if (!this.#scheduled) {
+ this.#scheduled = true;
+ queueMicrotask(() => this.#pull());
+ }
+ }
+}
+
+/**
+ * The cell code an editor state represents.
+ *
+ * With a language adapter, this is the adapter's Python for the editor text.
+ */
+function codeOf(state: EditorState): string {
+ const text = state.doc.toString();
+ const adapter = state.field(languageAdapterState, false);
+ return adapter
+ ? adapter.transformOut(
+ text,
+ state.field(languageMetadataField, false) ?? {},
+ )[0]
+ : text;
+}
+
+/**
+ * Whether an editor state shows the given cell code.
+ *
+ * An adapter can spell the same code more than one way, such as `mo.md("hi")`
+ * and its canonical form, so a state shows code that reads into its text and
+ * metadata as well as the code its own spelling produces.
+ */
+function shows(state: EditorState, code: string): boolean {
+ if (codeOf(state) === code) {
+ return true;
+ }
+ const adapter = state.field(languageAdapterState, false);
+ if (!adapter) {
+ return false;
+ }
+ const [text, , metadata] = adapter.transformIn(code);
+ return (
+ text === state.doc.toString() &&
+ dequal(metadata, state.field(languageMetadataField, false))
+ );
+}
+
+/**
+ * Whether an update could change the code an editor represents.
+ *
+ * Language metadata, such as an SQL cell's output name, changes the code
+ * without changing the text.
+ */
+function changesCode(update: ViewUpdate): boolean {
+ return (
+ update.docChanged ||
+ update.state.field(languageAdapterState, false) !==
+ update.startState.field(languageAdapterState, false) ||
+ update.state.field(languageMetadataField, false) !==
+ update.startState.field(languageMetadataField, false)
+ );
+}
+
+/**
+ * Show cell code in the language it implies.
+ *
+ * The language switch happens first, in its own transactions, and clears undo
+ * history because its entries edit the previous language's text. The text and
+ * metadata follow in one remote transaction that undo history leaves out. An
+ * SQL dialect depends on the engine in the metadata, so it is set again after.
+ */
+function replaceCode(view: EditorView, code: string) {
+ let text = code;
+ const effects: StateEffect<unknown>[] = [];
+ let sql = false;
+ const adapter = view.state.field(languageAdapterState, false);
+ if (adapter) {
+ const target = languageAdapterFromCode(code);
+ sql = target.type === "sql";
+ if (target.type !== adapter.type) {
+ switchLanguage(view, { language: target.type, keepCodeAsIs: true });
+ }
+ const [inner, , metadata] = target.transformIn(code);
+ text = inner;
+ if (!dequal(metadata, view.state.field(languageMetadataField, false))) {
+ effects.push(setLanguageMetadata.of(metadata));
+ }
+ }
+ const current = view.state.doc.toString();
+ if (current === text && effects.length === 0) {
+ return;
+ }
+ view.dispatch({
+ changes: changesBetween(current, text),
+ effects,
+ annotations: [
+ Transaction.remote.of(true),
+ Transaction.addToHistory.of(false),
+ ],
+ });
+ if (sql && effects.length > 0) {
+ initializeSQLDialect(view);
+ }
+}
+
+/** The changes that turn one text into another, one per edited range. */
+function changesBetween(from: string, to: string): ChangeSpec {
+ return diff(from, to, { scanLimit: SCAN_LIMIT }).map((change) => ({
+ from: change.fromA,
+ to: change.toA,
+ insert: to.slice(change.fromB, change.toB),
+ }));
+}
diff --git a/frontend/src/core/document/select.ts b/frontend/src/core/document/select.ts
index 05a609c8fd..fc2b6bd033 100644
--- a/frontend/src/core/document/select.ts
+++ b/frontend/src/core/document/select.ts
@@ -30,3 +30,12 @@ export function selectOrder(
): Atom<readonly Protocol.CellId[] | null> {
return selectAtom(document, (value) => value?.order ?? null, dequal);
}
+
+/** Select one field of a cell, unchanged until that field's value changes. */
+export function selectCellField<K extends keyof EditableCell>(
+ document: Atom<EditableDocument | null>,
+ id: Protocol.CellId,
+ field: K,
+): Atom<EditableCell[K] | undefined> {
+ return selectAtom(document, (value) => value?.cells.get(id)?.[field], dequal);
+}