Skip to content

feat: SQL IntelliSense in SqlView + VS Code (#26) - #40

Merged
melonamin merged 7 commits into
masterfrom
feat/issue-26-intellisense-completion
Apr 20, 2026
Merged

melonamin merged 7 commits into
masterfrom
feat/issue-26-intellisense-completion

Conversation

@melonamin

Copy link
Copy Markdown
Member

Summary

Adds SQL IntelliSense to both the React SqlView (CodeMirror 6) and the VS Code extension, backed by flowscope's existing completion_items_json WASM entry point. Typing in an editable SQL surface now surfaces dialect keywords, built-in functions, operators, and any table / CTE / column names recovered from the current parse.

Closes #26.

What's in the box

React package — packages/react/src/completion/

  • sqlCompletionSource.ts — CompletionSource factory. Reads full document + UTF-16 cursor offset from the CompletionContext, calls completionItems({ encoding: 'utf16', … }), maps the result to a CodeMirror CompletionResult using the engine's token span for the replace range and validFor: /^[\w$]*$/ so CM filters locally while the user keeps typing the same identifier.
  • mapCompletionItem.ts — pure CompletionItem → Completion mapper. Kind → CM type (table → class, column → property, …), score rescaled by 100 for CM's boost, apply populated only when insertText diverges from label.
  • Stale-request handling via a monotonic counter so a slow engine call never overwrites the result of a newer keystroke.
  • Engine errors go to an optional onError callback; the source never throws into CM.

SqlView props

  • dialect?: Dialect, completionSchema?: SchemaMetadata, disableCompletion?: boolean.
  • Dialect / schema are read through refs so changing them doesn't force a CodeMirror reconfigure.
  • Tab binding added at Prec.highest — acceptCompletion wins over basicSetup's indentWithTab when the popup is open and falls through to indent otherwise.

App wiring

  • EditorArea passes currentProject.dialect through to SqlView.

VS Code extension — vscode/src/providers/completion.ts

  • FlowScopeCompletionProvider implementing vscode.CompletionItemProvider, registered for language: 'sql' with . as an extra trigger character.
  • document.offsetAt(position) for UTF-16 cursor offsets; same encoding: 'utf16' pattern as the React side, so no byte conversion.
  • Completion kinds → vscode.CompletionItemKind (table → Struct, schemaTable → Module, column → Field, …). Engine score is inverted into a zero-padded sortText bucket so higher scores sort first.
  • Gated by new flowscope.enableCompletion setting (default on).

Types

  • The extension currently duplicates core types; added CompletionRequest, CompletionItemsResult, CompletionItem, CompletionClause, CompletionToken, and related enums to vscode/src/types.ts to match @pondpilot/flowscope-core.

What's intentionally not here

Test plan

  • yarn workspace @pondpilot/flowscope-react test — 182 passing (13 new: 6 for mapCompletionItem, 7 for sqlCompletionSource covering UTF-16 offset forwarding, token → replace-range, shouldShow gating, default-dialect fallback, stale-request dropping, and error isolation).
  • just typecheck — clean.
  • just lint-ts — clean.
  • Manual: typed WITH user_cohort AS (SELECT user_id FROM users) SELECT * FROM u in the demo app, confirmed popup surfaces user_cohort, users, user_id, dialect keywords (UNION, USING, UNNEST, UPDATE), and matching functions.
  • Manual: Tab accepts the highlighted suggestion with the popup open; Tab still indents when it's closed.
  • VS Code side exercised in Extension Development Host (reviewer please verify; test infra in vscode/ is thin so this wasn't automated).

Wire @codemirror/autocomplete to the existing flowscope `completionItems`
WASM API so typing in an editable `SqlView` pops a context-aware suggestion
list: dialect keywords, built-in functions, operators, and any table/CTE/
column names recovered from the current parse. Passes `encoding: 'utf16'`
to the engine so CodeMirror character offsets cross the boundary without
conversion.

Adds `dialect`, `completionSchema`, and `disableCompletion` props to
`SqlView` and plumbs the project dialect through from `EditorArea`.

Ships reusable `createSqlCompletionSource` + `mapCompletionItem` helpers
so a later VS Code `CompletionItemProvider` can consume the same mapping.
Register a `vscode.CompletionItemProvider` for `.sql` documents that
forwards the cursor position to the flowscope `completion_items_json`
WASM binding and maps each returned item to a VS Code completion (kind
icons, detail text, replacement range, score-derived `sortText`).

`flowscope.enableCompletion` (default true) lets users disable it.

The provider shares semantics with the React-side helper (`.` trigger,
UTF-16 cursor offsets, `shouldShow` short-circuit) so the two surfaces
behave consistently.
Bind Tab to `acceptCompletion` alongside the default Enter binding.
`acceptCompletion` returns false when no popup is open, so default Tab
handling (indentation / focus traversal) stays intact outside an active
completion session.
The previous Tab binding sat at default precedence, which put it after
basicSetup's `indentWithTab` in the keymap chain. Since `indentMore`
always returns true, Tab always indented and `acceptCompletion` never ran.

Wrap the binding in `Prec.highest` so it's tried first. `acceptCompletion`
still returns false when no popup is open, so indent behavior is
preserved outside an active completion session.
- Clamp token spans to document length and only replace identifier/keyword
  tokens, so trigger punctuation (`.`, `(`, `,`) stays in place.
- Honor VS Code's CancellationToken at entry, pre-WASM, and post-WASM.
- Validate the `flowscope.dialect` setting against the Dialect union and
  fall back to `generic` with a warning on unknown values.
- Sanitize completion error logs to emit only `error.message` (prefixed
  with `[FlowScope]`) and expose an optional `onCompletionError` on
  `SqlView` / `LineageExplorer` for hosts that want structured reporting.
- Correct the misleading `triggerCharacters` comment and clarify
  `BOOST_SCALE`'s expected input/output range.
Treat completion results with an error field as failures instead of silent empty results.

Report the error through the React completion hook and warn in the VS Code provider, with a regression test for the React source.
@melonamin
melonamin merged commit f114208 into master Apr 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

IntelliSense: node-name + dialect builtin completion

1 participant