Skip to content

fix: scope column completions to current query context#98

Merged
nkcoder merged 2 commits into
mainfrom
feature/completion-context-scoping
Jun 15, 2026
Merged

fix: scope column completions to current query context#98
nkcoder merged 2 commits into
mainfrom
feature/completion-context-scoping

Conversation

@nkcoder

@nkcoder nkcoder commented Jun 15, 2026

Copy link
Copy Markdown
Owner

Summary

  • Selected-text boundary: completionSource now checks context.state.selection.main; when the user has text selected, that selection range is used as the statement boundary instead of the semicolon scan — matching the same behaviour as Cmd+Enter (run selected SQL)
  • Subquery isolation: new innerSubqueryContext helper tracks paren depth (stack-based) in beforeWord; when the cursor is inside a subquery, only the inner subquery text is passed to detectSqlContext, so outer-query tables don't bleed into inner-query column suggestions
  • Semicolons inside strings: findStatementBounds replaces the raw lastIndexOf/indexOf semicolon scan with a quote-aware forward scan, fixing the edge case where WHERE comment = 'end; of story' AND | would split the statement at the wrong place

Test plan

  • 13 new unit tests covering findStatementBounds (including semicolon-in-string case) and innerSubqueryContext (including nested parens, closed inner parens, and combined-with-detectSqlContext assertions)
  • All 413 unit tests pass (npm run test in frontend/)
  • All 36 E2E autocomplete tests pass (npx playwright test e2e/autocomplete.spec.ts)
  • Manual: open demo DB, type SELECT * FROM users WHERE id IN (SELECT — verify only inner-query columns appear (none, since no FROM yet), not users columns
  • Manual: select a single statement from a multi-statement editor, trigger autocomplete — verify FROM tables from the unselected statement don't appear

nkcoder added 2 commits June 15, 2026 19:48
Fixes three gaps in completionSource so columns are only suggested from
tables actually referenced in the active query:

- findStatementBounds: replaces raw lastIndexOf/indexOf with a
  quote-aware scan, so semicolons inside string literals (e.g.
  WHERE comment = 'end; of story') no longer break statement boundaries

- innerSubqueryContext: detects when the cursor is inside a subquery by
  tracking paren depth (stack-based, so balanced inner parens are
  handled). Passes the innermost subquery text to detectSqlContext so
  outer-query tables don't bleed into subquery completions.

- completionSource: respects an active selection as the context boundary
  (user spec: context is bound by ; OR selected text), and applies
  innerSubqueryContext before dispatching to detectSqlContext.

13 new unit tests; all 413 unit tests and 36 E2E autocomplete tests pass.
…clip, comments, dedupe

Addresses the four highest-priority findings from the code review:

1. Selection now COMPOSES with `;` boundaries instead of bypassing them.
   `completionSource` always calls `findStatementBounds`, then clamps
   `[stmtStart, stmtEnd]` to the selection if any. Multi-statement
   selections now correctly isolate to the statement containing the cursor.

2. `innerSubqueryContext` clips `innerFull` at the matching `)` instead of
   extending to end-of-stmtFull. Outer-query content past the subquery
   (especially trailing JOINs) no longer leaks into inner completions.

3. SQL line comments (`-- …`) and block comments (`/* … */`) are now
   recognised by the scanner. Semicolons / parens inside comments no
   longer break statement boundaries or false-trigger subquery detection.

4. The inSingle/inDouble quote-state machine is now centralised in a
   single `scanSql(text, cb)` helper. `isInsideString`,
   `findStatementBounds`, and `innerSubqueryContext` all delegate to it.
   `findStatementBounds` also short-circuits when stmtEnd is found,
   recovering most of the lost native-`indexOf` fast path.

Deferred: dollar-quoted strings (`$$…$$`) and full-doc-scan perf — both
niche for an interactive GUI query editor.

11 new unit tests; 424 unit tests + 36 E2E autocomplete tests pass.
@nkcoder nkcoder merged commit 39b9811 into main Jun 15, 2026
2 checks passed
@nkcoder nkcoder deleted the feature/completion-context-scoping branch June 15, 2026 10:17
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.

1 participant