@marimo-team/islands 0.25.3-dev102 → 0.25.3-dev104

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (23) hide show
  1. package/dist/{ErrorBoundary-CTqzlCgA.js → ErrorBoundary-BSX5FnuK.js} +1 -1
  2. package/dist/{chat-ui-BIgPxfPP.js → chat-ui-BjFJNtik.js} +3 -3
  3. package/dist/{code-visibility-BXe6iFL5.js → code-visibility-azRFwO8n.js} +2 -2
  4. package/dist/{common-DKpWKeru.js → common-D7dvNADC.js} +3 -3
  5. package/dist/{data-grid-overlay-editor-D-lYQf1c.js → data-grid-overlay-editor-BvWRCxAv.js} +1 -1
  6. package/dist/{error-entries-TAO6kEhF.js → error-entries-m7wjcSR-.js} +1 -1
  7. package/dist/{feedback-button-CoLi84rn.js → feedback-button-CfEYDdvF.js} +3 -3
  8. package/dist/{glide-data-editor-bMB-b4l2.js → glide-data-editor-CuC6Qmmd.js} +3 -3
  9. package/dist/{globals-CmP7eFM1.js → globals-D7jFtSFG.js} +1 -1
  10. package/dist/{html-to-image-CWdayGZU.js → html-to-image-DQn1HTpP.js} +1 -1
  11. package/dist/main.js +10 -10
  12. package/dist/{number-overlay-editor-JFjUfbs3.js → number-overlay-editor-CCzf5gxX.js} +1 -1
  13. package/dist/{process-output-DD-oSvZk.js → process-output-DIMk7xHI.js} +2 -2
  14. package/dist/{reveal-component-BT1D05hU.js → reveal-component-ZUn41jSB.js} +3 -3
  15. package/package.json +1 -1
  16. package/src/core/codemirror/cells/__tests__/extensions.test.ts +15 -0
  17. package/src/core/codemirror/cells/extensions.ts +7 -3
  18. package/src/core/document/__tests__/codemirror.test.ts +483 -0
  19. package/src/core/document/__tests__/hooks.test.tsx +386 -0
  20. package/src/core/document/codemirror.ts +311 -0
  21. package/src/core/document/hooks.tsx +118 -0
  22. package/src/core/document/index.ts +10 -0
  23. package/src/core/document/select.ts +9 -0
@@ -0,0 +1,483 @@
1
+ /* Copyright 2026 Marimo. All rights reserved. */
2
+
3
+ import { history, undo } from "@codemirror/commands";
4
+ import { EditorSelection, EditorState } from "@codemirror/state";
5
+ import { EditorView } from "@codemirror/view";
6
+ import type { CellId } from "@marimo-team/client/protocol";
7
+ import { atom, createStore } from "jotai/vanilla";
8
+ import { afterEach, describe, expect, it, vi } from "vitest";
9
+ import { cellId as brandedCellId } from "@/__tests__/branded";
10
+ import { createNotebookActions } from "@/core/cells/cells";
11
+ import { OverridingHotkeyProvider } from "@/core/hotkeys/hotkeys";
12
+ import { cellActionsState } from "../../codemirror/cells/state";
13
+ import { cellConfigExtension } from "../../codemirror/config/extension";
14
+ import {
15
+ adaptiveLanguageConfiguration,
16
+ languageAdapterState,
17
+ switchLanguage,
18
+ } from "../../codemirror/language/extension";
19
+ import {
20
+ languageMetadataField,
21
+ setLanguageMetadata,
22
+ } from "../../codemirror/language/metadata";
23
+ import { SCHEMA_CACHE } from "../../codemirror/language/languages/sql/completion-store";
24
+ import type { Document } from "../../host/resources/document";
25
+ import { cellCodeBinding } from "../codemirror";
26
+ import { createDocumentModel } from "../model";
27
+ import { cell, document, withCells } from "./fixtures";
28
+
29
+ const views: EditorView[] = [];
30
+ const lifetimes: AbortController[] = [];
31
+
32
+ afterEach(() => {
33
+ for (const view of views.splice(0)) {
34
+ view.destroy();
35
+ }
36
+ lifetimes.splice(0).forEach((controller) => controller.abort());
37
+ });
38
+
39
+ const completionConfig = {
40
+ activate_on_typing: true,
41
+ signature_hint_on_typing: false,
42
+ copilot: false,
43
+ codeium_api_key: null,
44
+ } as const;
45
+
46
+ /** The language configuration a marimo cell editor installs. */
47
+ function languages(id: CellId) {
48
+ const options = {
49
+ cellId: brandedCellId(id),
50
+ completionConfig,
51
+ hotkeys: new OverridingHotkeyProvider({}),
52
+ placeholderType: "marimo-import",
53
+ lspConfig: {},
54
+ } as const;
55
+ return [
56
+ cellActionsState.of({
57
+ ...createNotebookActions(vi.fn()),
58
+ toggleHideCode: () => false,
59
+ aiCellCompletion: () => false,
60
+ createManyBelow: vi.fn(),
61
+ addOrAppendSetupCell: vi.fn(),
62
+ onRun: vi.fn(),
63
+ deleteCell: vi.fn(),
64
+ afterToggleMarkdown: vi.fn(),
65
+ afterToggleSQL: vi.fn(),
66
+ saveNotebook: vi.fn(),
67
+ }),
68
+ adaptiveLanguageConfiguration(options),
69
+ cellConfigExtension({ ...options, diagnosticsConfig: {} }),
70
+ ];
71
+ }
72
+
73
+ function setup({
74
+ initial = document(),
75
+ cellId = "c1",
76
+ doc = "",
77
+ adapters = false,
78
+ }: {
79
+ initial?: Document | null;
80
+ cellId?: CellId;
81
+ doc?: string;
82
+ adapters?: boolean;
83
+ } = {}) {
84
+ const store = createStore();
85
+ const source = atom(initial);
86
+ const controller = new AbortController();
87
+ lifetimes.push(controller);
88
+ const model = createDocumentModel({
89
+ store,
90
+ notebook: { documentAtom: source, edit: () => new Promise(() => {}) },
91
+ signal: controller.signal,
92
+ });
93
+ const editor = (text = doc) => {
94
+ const view = new EditorView({
95
+ state: EditorState.create({
96
+ doc: text,
97
+ extensions: [
98
+ history(),
99
+ adapters ? languages(cellId) : [],
100
+ cellCodeBinding({ store, model, cellId }),
101
+ ],
102
+ }),
103
+ });
104
+ views.push(view);
105
+ return view;
106
+ };
107
+ const view = editor();
108
+ return {
109
+ store,
110
+ source,
111
+ model,
112
+ view,
113
+ editor,
114
+ code: () => store.get(model.documentAtom)?.cells.get(cellId)?.code,
115
+ status: () => store.get(model.syncStatusAtom),
116
+ type: (insert: string) =>
117
+ view.dispatch({
118
+ changes: { from: view.state.doc.length, insert },
119
+ userEvent: "input.type",
120
+ }),
121
+ };
122
+ }
123
+
124
+ // Inbound changes apply in a microtask.
125
+ const settle = () => new Promise<void>((resolve) => setTimeout(resolve, 0));
126
+
127
+ describe("cellCodeBinding", () => {
128
+ it("seeds the editor from the cell outside undo history", async () => {
129
+ const { view, status } = setup();
130
+ await settle();
131
+ expect(view.state.doc.toString()).toBe("x = 1");
132
+ expect(undo(view)).toBe(false);
133
+ expect(status()).toEqual({ status: "idle" });
134
+ });
135
+
136
+ it("writes local changes to the draft before the update returns", async () => {
137
+ const { code, status, type } = setup();
138
+ await settle();
139
+ type(" + 1");
140
+ expect(code()).toBe("x = 1 + 1");
141
+ expect(status()).toEqual({ status: "pending" });
142
+ });
143
+
144
+ it("shows a change another subscriber makes while a push notifies", async () => {
145
+ const { store, model, view, type } = setup();
146
+ await settle();
147
+ // Another subscriber normalizes code synchronously, inside the push.
148
+ const unsubscribe = store.sub(model.documentAtom, () => {
149
+ const code = store.get(model.documentAtom)?.cells.get("c1")?.code;
150
+ if (code?.includes(" + ")) {
151
+ const normalized = code.replaceAll(" + ", "+");
152
+ store.set(model.editAtom, (tx) => tx.setCode("c1", normalized));
153
+ }
154
+ });
155
+ type(" + 1");
156
+ await settle();
157
+ expect(view.state.doc.toString()).toBe("x = 1+1");
158
+ unsubscribe();
159
+ });
160
+
161
+ it("keeps the selection between separate host edits", async () => {
162
+ const { store, source, view } = setup({
163
+ initial: withCells(document(), { c1: cell("c1", "a = 1\nb = 2\nc = 3") }),
164
+ });
165
+ await settle();
166
+ view.dispatch({ selection: EditorSelection.cursor(8) });
167
+
168
+ store.set(
169
+ source,
170
+ withCells(document(), { c1: cell("c1", "A = 1\nb = 2\nC = 3") }),
171
+ );
172
+ await settle();
173
+
174
+ expect(view.state.doc.toString()).toBe("A = 1\nb = 2\nC = 3");
175
+ expect(view.state.selection.main.head).toBe(8);
176
+ });
177
+
178
+ it("waits for composition to end before applying host changes", async () => {
179
+ const { store, source, view } = setup();
180
+ await settle();
181
+ let composing = true;
182
+ Object.defineProperty(view, "composing", { get: () => composing });
183
+
184
+ store.set(source, withCells(document(), { c1: cell("c1", "x = 2") }));
185
+ await settle();
186
+ expect(view.state.doc.toString()).toBe("x = 1");
187
+
188
+ // A composition can end without an update, so the binding watches for it.
189
+ composing = false;
190
+ view.contentDOM.dispatchEvent(new Event("compositionend"));
191
+ await settle();
192
+ expect(view.state.doc.toString()).toBe("x = 2");
193
+ });
194
+
195
+ it("applies a waiting host change on the first update after composition", async () => {
196
+ const { store, source, view } = setup();
197
+ await settle();
198
+ let composing = true;
199
+ Object.defineProperty(view, "composing", { get: () => composing });
200
+ store.set(source, withCells(document(), { c1: cell("c1", "x = 2") }));
201
+ await settle();
202
+
203
+ composing = false;
204
+ view.dispatch({ selection: EditorSelection.cursor(0) });
205
+ await settle();
206
+ expect(view.state.doc.toString()).toBe("x = 2");
207
+ });
208
+
209
+ it("keeps two editors on one cell in step", async () => {
210
+ const { editor, type } = setup();
211
+ const other = editor();
212
+ await settle();
213
+
214
+ type("!");
215
+ await settle();
216
+
217
+ expect(other.state.doc.toString()).toBe("x = 1!");
218
+ });
219
+
220
+ it("shows host code that returns to what this editor wrote", async () => {
221
+ const { store, source, view, type } = setup();
222
+ await settle();
223
+ type("!");
224
+ // The host confirms the edit, which releases the draft.
225
+ store.set(source, withCells(document(), { c1: cell("c1", "x = 1!") }));
226
+ await settle();
227
+
228
+ store.set(source, withCells(document(), { c1: cell("c1", "x = 2") }));
229
+ await settle();
230
+ expect(view.state.doc.toString()).toBe("x = 2");
231
+
232
+ store.set(source, withCells(document(), { c1: cell("c1", "x = 1!") }));
233
+ await settle();
234
+ expect(view.state.doc.toString()).toBe("x = 1!");
235
+ });
236
+
237
+ it("follows a cell once its code is available", async () => {
238
+ const absent = setup({ cellId: "c3" });
239
+ const withheld = setup({
240
+ initial: withCells(document(), { c1: cell("c1", "", { code: null }) }),
241
+ });
242
+ await settle();
243
+
244
+ absent.store.set(
245
+ absent.source,
246
+ withCells(document(), { c3: cell("c3", "new") }),
247
+ );
248
+ withheld.store.set(
249
+ withheld.source,
250
+ withCells(document(), { c1: cell("c1", "revealed") }),
251
+ );
252
+ await settle();
253
+
254
+ expect(absent.view.state.doc.toString()).toBe("new");
255
+ expect(withheld.view.state.doc.toString()).toBe("revealed");
256
+ });
257
+
258
+ it("shows the host's code after typing while the document was unavailable", async () => {
259
+ const { store, source, view, type, code } = setup();
260
+ await settle();
261
+
262
+ // All in one task, so no pull runs while the document is away.
263
+ store.set(source, null);
264
+ type(" + 1");
265
+ store.set(source, document());
266
+ await settle();
267
+
268
+ expect(view.state.doc.toString()).toBe(code());
269
+ });
270
+
271
+ it("leaves the editor alone for an absent cell or withheld source", async () => {
272
+ const absent = setup({ cellId: "c9", doc: "local" });
273
+ const withheld = setup({
274
+ initial: withCells(document(), { c1: cell("c1", "", { code: null }) }),
275
+ doc: "local",
276
+ });
277
+ await settle();
278
+
279
+ for (const { view, type, status } of [absent, withheld]) {
280
+ type("!");
281
+ expect(view.state.doc.toString()).toBe("local!");
282
+ expect(status()).toEqual({ status: "idle" });
283
+ }
284
+ });
285
+
286
+ it("stops following the cell when the view is destroyed", async () => {
287
+ const { store, source, view } = setup();
288
+ await settle();
289
+
290
+ // The change arrives before the view goes, and would apply just after.
291
+ store.set(source, withCells(document(), { c1: cell("c1", "x = 2") }));
292
+ view.destroy();
293
+ await settle();
294
+
295
+ expect(view.state.doc.toString()).toBe("x = 1");
296
+ });
297
+
298
+ describe("with language adapters", () => {
299
+ const language = (view: EditorView) =>
300
+ view.state.field(languageAdapterState).type;
301
+ /** What the editor's adapter reads back from cell code. */
302
+ const readBack = (view: EditorView, code: string | null | undefined) => {
303
+ const [text, , metadata] = view.state
304
+ .field(languageAdapterState)
305
+ .transformIn(code ?? "");
306
+ return { text, metadata };
307
+ };
308
+
309
+ it("edits markdown as markdown and writes Python with its metadata", async () => {
310
+ const { view, code, type } = setup({
311
+ adapters: true,
312
+ initial: withCells(document(), {
313
+ c1: cell("c1", 'mo.md(f"""hi {x}""")'),
314
+ }),
315
+ });
316
+ await settle();
317
+ expect(language(view)).toBe("markdown");
318
+ expect(view.state.doc.toString()).toBe("hi {x}");
319
+
320
+ type("!");
321
+ expect(readBack(view, code())).toMatchObject({
322
+ text: "hi {x}!",
323
+ metadata: { quotePrefix: "f" },
324
+ });
325
+ });
326
+
327
+ it("switches language for host code without rewriting it", async () => {
328
+ const { store, source, view, code, status } = setup({ adapters: true });
329
+ await settle();
330
+ expect(language(view)).toBe("python");
331
+
332
+ store.set(
333
+ source,
334
+ withCells(document(), { c1: cell("c1", 'mo.md("hi")') }),
335
+ );
336
+ await settle();
337
+
338
+ expect(language(view)).toBe("markdown");
339
+ expect(view.state.doc.toString()).toBe("hi");
340
+ expect(code()).toBe('mo.md("hi")');
341
+ expect(status()).toEqual({ status: "idle" });
342
+ });
343
+
344
+ it("writes changes to language metadata alone", async () => {
345
+ const { view, code } = setup({
346
+ adapters: true,
347
+ initial: withCells(document(), {
348
+ c1: cell("c1", 'mo.md("""\n# Title\n""")'),
349
+ }),
350
+ });
351
+ await settle();
352
+
353
+ view.dispatch({
354
+ effects: setLanguageMetadata.of({
355
+ ...view.state.field(languageMetadataField),
356
+ quotePrefix: "r",
357
+ }),
358
+ });
359
+ expect(readBack(view, code())).toMatchObject({
360
+ text: "# Title",
361
+ metadata: { quotePrefix: "r" },
362
+ });
363
+ });
364
+
365
+ it("applies a host change to language metadata alone", async () => {
366
+ const { store, source, view } = setup({
367
+ adapters: true,
368
+ initial: withCells(document(), {
369
+ c1: cell("c1", 'mo.md(r"""hi""")'),
370
+ }),
371
+ });
372
+ await settle();
373
+
374
+ store.set(
375
+ source,
376
+ withCells(document(), { c1: cell("c1", 'mo.md(f"""hi""")') }),
377
+ );
378
+ await settle();
379
+
380
+ expect(view.state.doc.toString()).toBe("hi");
381
+ expect(view.state.field(languageMetadataField)).toMatchObject({
382
+ quotePrefix: "f",
383
+ });
384
+ });
385
+
386
+ it("does not rewrite code when the editor switches language as it opens", async () => {
387
+ const original = 'mo.md("hello")';
388
+ const { view, code, status } = setup({
389
+ adapters: true,
390
+ doc: original,
391
+ initial: withCells(document(), { c1: cell("c1", original) }),
392
+ });
393
+ // The cell editor switches to the language its code implies on mount.
394
+ switchLanguage(view, { language: "markdown" });
395
+ await settle();
396
+
397
+ expect(code()).toBe(original);
398
+ expect(status()).toEqual({ status: "idle" });
399
+ });
400
+
401
+ it("keeps typed text that the adapter would normalize", async () => {
402
+ const { view, type } = setup({
403
+ adapters: true,
404
+ initial: withCells(document(), {
405
+ c1: cell("c1", 'mo.md("""\nhi\n""")'),
406
+ }),
407
+ });
408
+ await settle();
409
+
410
+ type("\n");
411
+ await settle();
412
+ type(" ");
413
+ await settle();
414
+
415
+ expect(view.state.doc.toString()).toBe("hi\n ");
416
+ });
417
+
418
+ it("keeps the language a person chose when the cell changes otherwise", async () => {
419
+ const { store, model, view } = setup({
420
+ adapters: true,
421
+ initial: withCells(document(), {
422
+ c1: cell("c1", 'mo.md("""\nhi\n""")'),
423
+ }),
424
+ });
425
+ await settle();
426
+ switchLanguage(view, { language: "python" });
427
+ await settle();
428
+
429
+ store.set(model.editAtom, (tx) => tx.setName("c1", "renamed"));
430
+ await settle();
431
+
432
+ expect(language(view)).toBe("python");
433
+ });
434
+
435
+ it("edits SQL as SQL and keeps its output name", async () => {
436
+ const { store, source, view, code, type } = setup({ adapters: true });
437
+ await settle();
438
+
439
+ store.set(
440
+ source,
441
+ withCells(document(), {
442
+ c1: cell("c1", 'df = mo.sql(f"""SELECT 1""")'),
443
+ }),
444
+ );
445
+ await settle();
446
+ expect(language(view)).toBe("sql");
447
+ expect(view.state.doc.toString()).toBe("SELECT 1");
448
+
449
+ type(" LIMIT 1");
450
+ expect(readBack(view, code())).toMatchObject({
451
+ text: "SELECT 1 LIMIT 1",
452
+ metadata: { dataframeName: "df" },
453
+ });
454
+ });
455
+
456
+ it("sets the SQL dialect from the host's engine", async () => {
457
+ const { store, source } = setup({ adapters: true });
458
+ await settle();
459
+ const dialect = vi.spyOn(SCHEMA_CACHE, "getDialect");
460
+ try {
461
+ store.set(
462
+ source,
463
+ withCells(document(), {
464
+ c1: cell("c1", '_df = mo.sql(f"""SELECT 1""", engine=warehouse)'),
465
+ }),
466
+ );
467
+ await settle();
468
+ expect(dialect).toHaveBeenLastCalledWith("warehouse");
469
+
470
+ store.set(
471
+ source,
472
+ withCells(document(), {
473
+ c1: cell("c1", '_df = mo.sql(f"""SELECT 1""", engine=lake)'),
474
+ }),
475
+ );
476
+ await settle();
477
+ expect(dialect).toHaveBeenLastCalledWith("lake");
478
+ } finally {
479
+ dialect.mockRestore();
480
+ }
481
+ });
482
+ });
483
+ });