fix: scope column completions to current query context#98
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
completionSourcenow checkscontext.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)innerSubqueryContexthelper tracks paren depth (stack-based) inbeforeWord; when the cursor is inside a subquery, only the inner subquery text is passed todetectSqlContext, so outer-query tables don't bleed into inner-query column suggestionsfindStatementBoundsreplaces the rawlastIndexOf/indexOfsemicolon scan with a quote-aware forward scan, fixing the edge case whereWHERE comment = 'end; of story' AND |would split the statement at the wrong placeTest plan
findStatementBounds(including semicolon-in-string case) andinnerSubqueryContext(including nested parens, closed inner parens, and combined-with-detectSqlContextassertions)npm run testinfrontend/)npx playwright test e2e/autocomplete.spec.ts)SELECT * FROM users WHERE id IN (SELECT— verify only inner-query columns appear (none, since no FROM yet), notuserscolumns