|
| 1 | +# vNext CodeMirror Adapter |
| 2 | + |
| 3 | +Status: experimental |
| 4 | + |
| 5 | +Import: `@marimo-team/codemirror-sql/vnext/codemirror` |
| 6 | + |
| 7 | +`sqlEditor` connects one caller-owned vNext language service to one or more |
| 8 | +CodeMirror 6 views. Each view owns a document session, cancellation state, |
| 9 | +catalog-refresh intent, and revision subscription. Destroying a view disposes |
| 10 | +that view's session but never disposes the shared service. |
| 11 | + |
| 12 | +```ts |
| 13 | +import { EditorView } from "@codemirror/view"; |
| 14 | +import { |
| 15 | + createSqlLanguageService, |
| 16 | + duckdbDialect, |
| 17 | +} from "@marimo-team/codemirror-sql/vnext"; |
| 18 | +import { |
| 19 | + sqlEditor, |
| 20 | +} from "@marimo-team/codemirror-sql/vnext/codemirror"; |
| 21 | + |
| 22 | +const service = createSqlLanguageService({ |
| 23 | + dialects: [duckdbDialect()], |
| 24 | +}); |
| 25 | +const support = sqlEditor({ |
| 26 | + initialContext: { |
| 27 | + catalog: { scope: "connection-incarnation:1" }, |
| 28 | + dialect: "duckdb", |
| 29 | + }, |
| 30 | + service, |
| 31 | +}); |
| 32 | +const view = new EditorView({ |
| 33 | + doc: "SELECT * FROM ", |
| 34 | + extensions: [support.extension], |
| 35 | +}); |
| 36 | + |
| 37 | +support.setContext(view, { |
| 38 | + catalog: { scope: "connection-incarnation:2" }, |
| 39 | + dialect: "duckdb", |
| 40 | +}); |
| 41 | + |
| 42 | +view.destroy(); |
| 43 | +service.dispose(); |
| 44 | +``` |
| 45 | + |
| 46 | +The adapter installs one coherent `autocompletion` configuration. Additional |
| 47 | +sources belong in `autocomplete.externalSources`; consumers should not install |
| 48 | +a second independently configured `autocompletion` extension. |
| 49 | + |
| 50 | +## Atomic inputs |
| 51 | + |
| 52 | +Context and document changes can share one CodeMirror transaction: |
| 53 | + |
| 54 | +```ts |
| 55 | +view.dispatch({ |
| 56 | + changes, |
| 57 | + effects: support.contextEffect.of(nextContext), |
| 58 | +}); |
| 59 | +``` |
| 60 | + |
| 61 | +CodeMirror's composite `ChangeSet` is converted to sorted, non-overlapping |
| 62 | +pre-update UTF-16 changes and sent to the session exactly once. |
| 63 | + |
| 64 | +Embedded-language ranges are explicit. Their effect payload is the complete |
| 65 | +region set in the resulting document's coordinates: |
| 66 | + |
| 67 | +```ts |
| 68 | +view.dispatch({ |
| 69 | + changes, |
| 70 | + effects: support.embeddedRegionsEffect.of(nextRegions), |
| 71 | +}); |
| 72 | +``` |
| 73 | + |
| 74 | +When the current document contains embedded regions, every document-changing |
| 75 | +transaction must include the complete resulting region set. The adapter fails |
| 76 | +closed instead of guessing how host-language delimiters map through edits. |
| 77 | +Region-only updates may use `setEmbeddedRegions`. |
| 78 | + |
| 79 | +The adapter's own completion edit is the narrow exception: it maps unrelated |
| 80 | +regions through the exact edit and includes the complete mapped set in the same |
| 81 | +transaction. A completion edit that overlaps an embedded region is ignored. |
| 82 | + |
| 83 | +## Completion currency |
| 84 | + |
| 85 | +The adapter does not use `validFor` or result mapping in this first slice. |
| 86 | +Every result and completion application rechecks the session revision, |
| 87 | +document identity, selection, and context generation. |
| 88 | + |
| 89 | +Loading completion results retain one bounded, token-correlated refresh intent, |
| 90 | +including empty results that open no menu. Only a session event carrying the |
| 91 | +exact task token can schedule a refresh. A newer request, document/context/ |
| 92 | +region/selection change, Escape, configured blur, visibility loss, lease |
| 93 | +expiry, or view destruction cancels the intent. Refresh and close operations |
| 94 | +are always queued; provider and session callbacks never synchronously dispatch |
| 95 | +CodeMirror transactions. |
0 commit comments