Repository navigation
feat: build completion on a cursor context API of the parser - #267
Merged
Merged
Conversation
sql-language-server completed by parsing the SQL truncated at the cursor and reading the `expected` list of the resulting syntax error, plus a separate FROM clause parser, a recursive completion for broken subqueries and the EXPECTED COLUMN NAME message. It is replaced by getCompletionContext(sql, offset) in @joe-re/sql-parser. Parser: - Record roles (table, expression, column name, alias, new name) along with the expected keywords; they are not exposed in syntax errors - getCompletionContext() returns the expected keywords and roles at the cursor, the word being typed and its qualifier, the statement type, the tables visible from the innermost query (FROM / JOIN, or the target of INSERT / UPDATE / DELETE / ALTER TABLE) and the CTEs - When SQL before the cursor is broken, parse again from the clause that contains the cursor (SELECT, FROM, JOIN, ON, WHERE, HAVING, SET, BY or an opening parenthesis) Server: - Offer tables where a table can appear, columns / functions / aliases where an expression can appear, and target table columns for INSERT, UPDATE SET and ALTER TABLE column names - Offer CTE names and columns - Fix createFunctionCandidates mutating the shared schema - Remove helpers that located AST nodes by position
joe-re
force-pushed
the
feat/completion-context
branch
from
September 27, 2026 23:18
bc04f3f to
6bfe91b
Compare
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
Moves sql-language-server completion off the syntax-error workaround onto a new cursor context API of the parser.
Before
complete()parsed the SQL truncated at the cursor and relied on the failure: keywords came from the error'sexpected, tables from a secondparseFromClauseover the whole text, broken FROM subqueries were completed by recursing on their text, and column-name positions were detected by anEXPECTED COLUMN NAMEerror message. When the truncated SQL happened to parse, a separate path produced a different set of candidates.After
getCompletionContext(sql, offset)in@joe-re/sql-parserreturns:keywordsexpected at the cursor, and roles:table,column(expression),columnName(INSERT column list,UPDATE ... SET,DROP COLUMN, ...),alias,newNamewordbeing typed and itsqualifier(uinu.na|, including quoted names and subscripts)tablesvisible from the innermost query at the cursor (FROM / JOIN, subqueries with their columns) or the target of INSERT / UPDATE / DELETE / ALTER TABLE, andcteswith their columnsinCommentOrString,statementType,errorBeforeCursorThe parser records the roles next to its expectations (they never appear in syntax errors). When the SQL before the cursor is broken, the context is computed again from the clause that contains the cursor (
SELECT,FROM,JOIN,ON,WHERE,HAVING,SET,BY, or an opening parenthesis), e.g.SELECT jo. FROM employees jo JOIN jobs j ON jo.|still completes columns.complete()now only maps roles to the existing candidate builders: tables where a table can appear, functions / aliases / scoped columns where an expression can appear, and target-table columns for column-name positions.Behavior changes
FROM t);is not offered as a candidateEvaluation
complete()calls made by the server tests on feat(sql-parser)!: reimplement the parser by hand in TypeScript #266 were recorded and replayed: 103/106 identical; 2 differ only in order and 1 is an artifact of the recording (the oldcreateFunctionCandidatesmutated the shared schema, fixed here)release(old parser + old completion): 41.4%releasehit are artifacts (e.g. the old code used the first statement's FROM for a second statement) or suggesting an alias the user is currently definingTest plan