You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
## Summary
Adds the classic LSP trio for statement-local SQL identifiers —
go-to-definition, document highlights, and rename — and rewrites CTE
completion on top of `QueryContext` (#164). All analyses are purely
local; no schema required.
### CTE completion (`cteCompletionSource` rewrite)
- Statement-scoped: CTEs from other statements in the doc no longer leak
into completions.
- Handles nested-paren bodies, 3+ comma-separated CTEs, `RECURSIVE`, and
quoted names (quoted names complete with a quoted `apply`).
- New: CTE **column** completions after `<cte>.` and in column positions
when the statement selects `FROM` that CTE (explicit column list or
select-list inference; skipped on `SELECT *`).
- Regex scan remains the fallback for statements that don't parse
mid-edit. Exported name/signature unchanged;
`createCteCompletionSource(config)` added for sharing parser/analyzer
instances.
### `findReferences` / `SqlReferenceResolver` (new)
Resolves three kinds: CTE names (definition = name token in `WITH x
AS`), table aliases (definition = alias token in `FROM t x`), and
select-list aliases (references limited to `GROUP BY`/`ORDER
BY`/`HAVING`/`QUALIFY`). Occurrence scanning runs on masked statement
text, so string literals and comments are never matched. It refuses
rather than guesses: ambiguous aliases (same alias on two tables),
dotted qualifiers, and cursors inside strings all return null.
### Navigation extensions (new)
- `sqlHighlightReferences()` — debounced cursor-driven `Decoration.mark`
on all references, definition styled distinctly.
- `sqlGotoDefinition()` — Mod-hover underline affordance + Mod-click
jump; unresolvable Mod-clicks fall through to CodeMirror's default
(multi-cursor).
- `renameSqlIdentifier(view, config)` — rewrites definition + all
references in one transaction (one undo step); new name comes from a
`prompt` callback so hosts (marimo) can supply their own UI; refuses
when not confidently resolvable — never text search-and-replace.
- `sqlNavigationKeymap()` — opt-in `F12`/`Mod-b` go-to-definition, `F2`
rename.
Wired into `sqlExtension` behind `enableNavigation` (default on,
highlights + Mod-click) and `navigationConfig` (keybindings opt-in via
`keymap: true`).
### Supporting fixes (`query-context.ts`)
- Quoted CTE names in the regex extractor and `findCteSpan`.
- Span search on masked text so `AS (` inside a string can't fake a
declaration.
- `SELECT *` no longer infers `*` as a CTE output column.
### Behavior change
The old test expecting CTEs from *other* statements to appear in
completions was inverted — that leaking is what this PR fixes; new tests
assert scoping in both directions.
## Demo
The demo opens with a two-CTE query exercising every navigation kind and
enables the keymap; a new `demo-doc.test.ts` guards that the demo
document parses and all navigation targets resolve. Verified end-to-end
in a live browser: highlights, Mod-hover underline, Mod-click jump, and
F2 rename (comment/string occurrences untouched).
## Testing
- 439 unit tests pass (61 new across `references.test.ts`,
`navigation-extension.test.ts`, expanded
`cte-completion-source.test.ts`), browser test passes, typecheck +
oxlint clean, demo builds.
<!-- This is an auto-generated description by cubic. -->
---
## Summary by cubic
Adds AST-based, statement-scoped CTE navigation (go-to-definition,
reference highlights, safe rename) and rewrites CTE completion to
include CTE columns. Improves accuracy, quoted-name handling, and
edit-safety — no schema required.
- New Features
- Rewrote CTE completion on `QueryContext`: statement-scoped, supports
quoted names, nested parens, multiple CTEs, and `RECURSIVE`. Adds CTE
column completion after `<cte>.` and in column positions when selected
from that CTE. Regex fallback for mid-edit. New
`createCteCompletionSource({ parser })`.
- Added `findReferences` / `SqlReferenceResolver`: resolves CTE names,
table aliases, and select aliases; scans masked text (ignores
strings/comments); refuses ambiguous cases.
- Added navigation: `sqlHighlightReferences` (cursor-driven highlights),
`sqlGotoDefinition` (Mod-hover underline + Mod-click jump),
`renameSqlIdentifier` (single-transaction rename via host `prompt`), and
`sqlNavigationKeymap` (F12/Mod-b/F2). Wired into `sqlExtension` via
`enableNavigation` (default on) and `navigationConfig.keymap`.
- Exported new APIs: `createCteCompletionSource`, `findReferences`,
`gotoSqlDefinition`, `renameSqlIdentifier`, `sqlNavigation`,
`sqlNavigationKeymap`, and related types. Updated README, demo, and
tests.
- Bug Fixes
- CTE references limited to relation positions and qualifiers; bare
same-named columns are not matched. Refuse collisions with select
aliases; alias definition search anchored to relation positions;
resolves quoted identifiers under the cursor.
- Navigation drops stale results if the doc changes while resolving;
hover state resets on edits; rename rejects reserved words and cancels
if the doc changed during prompting.
- Completion quotes non-bare CTE column labels on apply and treats `$`
as part of identifier tokens.
- `sqlExtension` navigation inherits
`linterConfig.parser`/`structureAnalyzer` by default for consistent
dialect behavior.
<sup>Written for commit 5bb4820.
Summary will update on new commits.</sup>
<a
href="https://cubic.dev/pr/marimo-team/codemirror-sql/pull/166?utm_source=github"
target="_blank" rel="noopener noreferrer"
data-no-image-dialog="true"><picture><source
media="(prefers-color-scheme: dark)"
srcset="https://www.cubic.dev/buttons/review-in-cubic-dark.svg"><source
media="(prefers-color-scheme: light)"
srcset="https://www.cubic.dev/buttons/review-in-cubic-light.svg"><img
alt="Review in cubic"
src="https://www.cubic.dev/buttons/review-in-cubic-dark.svg"></picture></a>
<!-- End of auto-generated description by cubic. -->
0 commit comments