fix(editor): bound SQL completion to active statement

This commit is contained in:
Melody Ruch authored and GitHub committed 2026-08-18 11:51:07 +08:00
1 parent 8271ba2a71
commit 41ffc969a1
12 files changed
+917 -210

No files matched your search

+11 -2
View File
@@ -1,5 +1,6 @@
import { sqlSemanticDialectFor, type SqlSemanticDialectAdapter } from "@/lib/sql/semantic/dialect";
import { findActiveSqlStatementSpan, isSuppressedSqlSemanticContext, tokenIsIdentifier, tokenizeSqlSemantic, unquoteSqlSemanticIdentifier } from "@/lib/sql/semantic/tokens";
import { resolveSqlStatementWindow } from "@/lib/sql/sqlSyntaxTreeWindow";
import type {
SqlSemanticBuildOptions,
SqlSemanticClauseSpans,
@@ -785,8 +786,16 @@ function buildScope(statement: SqlSemanticStatement, rowSources: SqlSemanticRowS
export function buildSqlSemanticModel(sql: string, cursor: number, options: SqlSemanticBuildOptions = {}): SqlSemanticModel {
const safeCursor = Math.max(0, Math.min(cursor, sql.length));
const dialect = sqlSemanticDialectFor(options);
const allTokens = tokenizeSqlSemantic(sql, dialect.id);
const statementSpan = findActiveSqlStatementSpan(sql, allTokens, safeCursor);
const window = resolveSqlStatementWindow(sql, safeCursor, options.editorState, dialect.id);
const windowSql = sql.slice(window.from, window.to);
const windowCursor = safeCursor - window.from;
const localTokens = tokenizeSqlSemantic(windowSql, dialect.id);
const localStatementSpan = findActiveSqlStatementSpan(windowSql, localTokens, windowCursor);
const allTokens = localTokens.map((token) => ({
...token,
span: { start: token.span.start + window.from, end: token.span.end + window.from },
}));
const statementSpan = { start: localStatementSpan.start + window.from, end: localStatementSpan.end + window.from };
const tokens = significantTokens(allTokens.filter((item) => item.span.end > statementSpan.start && item.span.start < statementSpan.end));
const kind = statementKind(tokens);
const statement: SqlSemanticStatement = {