improve: 优化 SQL 语义诊断范围
This commit is contained in:
3 files changed
+165
-31
No files matched your search
@@ -10,7 +10,7 @@ import SqlExecutionTargetPicker from "./SqlExecutionTargetPicker.vue";
|
||||
import CustomContextMenu, { type ContextMenuItem } from "@/components/ui/CustomContextMenu.vue";
|
||||
import { copyToClipboard } from "@/lib/clipboard";
|
||||
import { resolveExecutableSql, type SqlExecutionSnapshot, type SqlExecutionOverride, type SqlExecutionCandidate } from "@/lib/sqlExecutionTarget";
|
||||
import { buildExecutionCandidates, executableStatementRanges, hasMultipleExecutionTargets, supportsExecutionTargetPicker } from "@/lib/sqlStatementRanges";
|
||||
import { buildExecutionCandidates, executableStatementRanges, hasMultipleExecutionTargets, supportsExecutionTargetPicker, type SqlTextRange } from "@/lib/sqlStatementRanges";
|
||||
import { formatSqlText, type SqlFormatDialect } from "@/lib/sqlFormatter";
|
||||
import { formatMongoShellText } from "@/lib/mongoFormatter";
|
||||
import { useConnectionStore } from "@/stores/connectionStore";
|
||||
@@ -43,7 +43,7 @@ import { isSchemaAware, isSingleDatabase } from "@/lib/databaseFeatureSupport";
|
||||
import { usesLocalOnlyEditorCompletionMetadata, usesOnDemandOnlyEditorColumnMetadata } from "@/lib/completionMetadataPolicy";
|
||||
import { qualifiedTableNameAtSqlPosition } from "@/lib/queryCursorTableTarget";
|
||||
import * as api from "@/lib/api";
|
||||
import { areSqlSemanticDiagnosticsEqual, buildSqlParserErrorDiagnostic, buildSqlSemanticDiagnostics, isSqlSemanticDiagnosticInputContext, shouldRunSqlSemanticDiagnostics, tableReferenceKey, type SqlSemanticDiagnostic } from "@/lib/sqlSemanticDiagnostics";
|
||||
import { areSqlSemanticDiagnosticsEqual, buildSqlParserErrorDiagnostic, buildSqlSemanticDiagnostics, isSqlSemanticDiagnosticInputContext, shouldRunSqlSemanticDiagnostics, sqlSemanticDiagnosticRangesForViewport, tableReferenceKey, type SqlSemanticDiagnostic } from "@/lib/sqlSemanticDiagnostics";
|
||||
import { buildRedisSyntaxDiagnostics, shouldRunRedisDiagnostics } from "@/lib/redisSyntaxDiagnostics";
|
||||
import { buildRedisCompletionItemsFromContext, getRedisCompletionContext, getRedisCompletionResultValidFor, shouldAutoOpenRedisCompletion, takesKeyArgument, type RedisCompletionItem } from "@/lib/redisCompletion";
|
||||
import type { SqlCompletionColumn, SqlCompletionForeignKey, SqlCompletionItem, SqlCompletionObject, SqlCompletionTable } from "@/lib/sqlCompletion";
|
||||
@@ -208,6 +208,7 @@ let codeMirrorIndentUnit: typeof import("@codemirror/language").indentUnit | nul
|
||||
let semanticDiagnostics: SqlSemanticDiagnostic[] = [];
|
||||
let semanticDiagnosticTimer: ReturnType<typeof setTimeout> | null = null;
|
||||
let semanticDiagnosticRunId = 0;
|
||||
let pendingSemanticDiagnosticPreserveOutsideRanges = false;
|
||||
let editorIsActive = true;
|
||||
let tableReferenceDropListenerRegistered = false;
|
||||
let imeCompositionActive = false;
|
||||
@@ -922,6 +923,54 @@ function setSemanticDiagnostics(next: SqlSemanticDiagnostic[]) {
|
||||
reconfigureDiagnostics();
|
||||
}
|
||||
|
||||
function rangesOverlap(left: { from: number; to: number }, right: { from: number; to: number }): boolean {
|
||||
return left.from < right.to && right.from < left.to;
|
||||
}
|
||||
|
||||
function sqlLineColumnAtOffset(sql: string, offset: number): { line: number; column: number } {
|
||||
const safeOffset = Math.max(0, Math.min(offset, sql.length));
|
||||
let line = 1;
|
||||
let lineStart = 0;
|
||||
for (let index = 0; index < safeOffset; index += 1) {
|
||||
if (sql[index] === "\n") {
|
||||
line += 1;
|
||||
lineStart = index + 1;
|
||||
}
|
||||
}
|
||||
return { line, column: safeOffset - lineStart + 1 };
|
||||
}
|
||||
|
||||
function offsetSqlTextSpan(span: SqlTextSpan, rangeStart: { line: number; column: number }): SqlTextSpan {
|
||||
const offsetLine = (line: number) => rangeStart.line + line - 1;
|
||||
const offsetColumn = (line: number, column: number) => (line === 1 ? rangeStart.column + column - 1 : column);
|
||||
return {
|
||||
start_line: offsetLine(span.start_line),
|
||||
start_column: offsetColumn(span.start_line, span.start_column),
|
||||
end_line: offsetLine(span.end_line),
|
||||
end_column: offsetColumn(span.end_line, span.end_column),
|
||||
};
|
||||
}
|
||||
|
||||
function offsetSqlSemanticDiagnostics(diagnostics: readonly SqlSemanticDiagnostic[], range: SqlTextRange, fullSql: string): SqlSemanticDiagnostic[] {
|
||||
const rangeStart = sqlLineColumnAtOffset(fullSql, range.from);
|
||||
return diagnostics.map((diagnostic) => ({
|
||||
...diagnostic,
|
||||
span: offsetSqlTextSpan(diagnostic.span, rangeStart),
|
||||
}));
|
||||
}
|
||||
|
||||
function replaceSemanticDiagnosticsInRanges(next: SqlSemanticDiagnostic[], ranges: readonly SqlTextRange[], fullSql: string) {
|
||||
const retained = semanticDiagnostics.filter((diagnostic) => {
|
||||
const diagnosticRange = sqlTextSpanToRange(fullSql, diagnostic.span);
|
||||
return !diagnosticRange || !ranges.some((range) => rangesOverlap(diagnosticRange, range));
|
||||
});
|
||||
setSemanticDiagnostics([...retained, ...next].sort(compareSqlSemanticDiagnostics));
|
||||
}
|
||||
|
||||
function compareSqlSemanticDiagnostics(left: SqlSemanticDiagnostic, right: SqlSemanticDiagnostic): number {
|
||||
return left.span.start_line - right.span.start_line || left.span.start_column - right.span.start_column || left.span.end_line - right.span.end_line || left.span.end_column - right.span.end_column || left.message.localeCompare(right.message);
|
||||
}
|
||||
|
||||
async function enrichSemanticDiagnosticTables(tables: SqlTableReference[]): Promise<{ tables: SqlTableReference[]; missingTables: Set<string> }> {
|
||||
if (!props.connectionId || props.database == null) return { tables, missingTables: new Set() };
|
||||
|
||||
@@ -968,7 +1017,7 @@ async function ensureColumnsForSemanticDiagnostics(tables: SqlTableReference[]):
|
||||
return missingTables;
|
||||
}
|
||||
|
||||
async function refreshSemanticDiagnostics() {
|
||||
async function refreshSemanticDiagnostics(options: { preserveOutsideRanges?: boolean } = {}) {
|
||||
const currentView = view.value;
|
||||
const runId = ++semanticDiagnosticRunId;
|
||||
if (!currentView || !props.connectionId || props.database == null) {
|
||||
@@ -988,54 +1037,75 @@ async function refreshSemanticDiagnostics() {
|
||||
if (props.databaseType === "redis") {
|
||||
// Redis has no SQL semantics; run command-name / arity / quote / danger checks instead.
|
||||
if (!shouldRunRedisDiagnostics(sql, currentView.state.selection.main.head)) {
|
||||
scheduleSemanticDiagnostics(900);
|
||||
scheduleSemanticDiagnostics(900, { preserveOutsideRanges: options.preserveOutsideRanges });
|
||||
return;
|
||||
}
|
||||
setSemanticDiagnostics(buildRedisSyntaxDiagnostics(sql));
|
||||
return;
|
||||
}
|
||||
if (!shouldRunSqlSemanticDiagnostics(sql, currentView.state.selection.main.head, { databaseType: props.databaseType })) {
|
||||
scheduleSemanticDiagnostics(1200);
|
||||
scheduleSemanticDiagnostics(1200, { preserveOutsideRanges: options.preserveOutsideRanges });
|
||||
return;
|
||||
}
|
||||
if (codeMirrorCompletionStatus?.(currentView.state) && isSqlSemanticDiagnosticInputContext(sql, currentView.state.selection.main.head, { databaseType: props.databaseType })) {
|
||||
scheduleSemanticDiagnostics(900);
|
||||
scheduleSemanticDiagnostics(900, { preserveOutsideRanges: options.preserveOutsideRanges });
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const analysis = await api.analyzeSqlReferences(sql, props.formatDialect ?? props.dialect ?? "generic");
|
||||
if (runId !== semanticDiagnosticRunId) return;
|
||||
const visibleRanges = currentView.visibleRanges.length > 0 ? currentView.visibleRanges : [currentView.viewport];
|
||||
const diagnosticRanges = sqlSemanticDiagnosticRangesForViewport(sql, visibleRanges, props.databaseType);
|
||||
if (diagnosticRanges.length === 0) {
|
||||
if (!options.preserveOutsideRanges) setSemanticDiagnostics([]);
|
||||
return;
|
||||
}
|
||||
|
||||
const { tables, missingTables } = await enrichSemanticDiagnosticTables(analysis.tables);
|
||||
const columnMetadataMissingTables = await ensureColumnsForSemanticDiagnostics(tables);
|
||||
for (const tableKey of columnMetadataMissingTables) missingTables.add(tableKey);
|
||||
if (runId !== semanticDiagnosticRunId) return;
|
||||
const nextDiagnostics: SqlSemanticDiagnostic[] = [];
|
||||
for (const range of diagnosticRanges) {
|
||||
try {
|
||||
const analysis = await api.analyzeSqlReferences(range.sql, props.formatDialect ?? props.dialect ?? "generic");
|
||||
if (runId !== semanticDiagnosticRunId) return;
|
||||
|
||||
const enrichedAnalysis: SqlReferenceAnalysis = { ...analysis, tables };
|
||||
setSemanticDiagnostics(
|
||||
buildSqlSemanticDiagnostics(enrichedAnalysis, {
|
||||
tables: cachedTables,
|
||||
columnsByTable: cachedColumnsByTable,
|
||||
missingTables,
|
||||
loadedColumnTables: loadedColumnsByTable,
|
||||
sql,
|
||||
}),
|
||||
);
|
||||
} catch (error) {
|
||||
if (runId === semanticDiagnosticRunId) {
|
||||
const diagnostic = buildSqlParserErrorDiagnostic(error, sql);
|
||||
setSemanticDiagnostics(diagnostic ? [diagnostic] : []);
|
||||
const { tables, missingTables } = await enrichSemanticDiagnosticTables(analysis.tables);
|
||||
const columnMetadataMissingTables = await ensureColumnsForSemanticDiagnostics(tables);
|
||||
for (const tableKey of columnMetadataMissingTables) missingTables.add(tableKey);
|
||||
if (runId !== semanticDiagnosticRunId) return;
|
||||
|
||||
const enrichedAnalysis: SqlReferenceAnalysis = { ...analysis, tables };
|
||||
nextDiagnostics.push(
|
||||
...offsetSqlSemanticDiagnostics(
|
||||
buildSqlSemanticDiagnostics(enrichedAnalysis, {
|
||||
tables: cachedTables,
|
||||
columnsByTable: cachedColumnsByTable,
|
||||
missingTables,
|
||||
loadedColumnTables: loadedColumnsByTable,
|
||||
sql: range.sql,
|
||||
}),
|
||||
range,
|
||||
sql,
|
||||
),
|
||||
);
|
||||
} catch (error) {
|
||||
if (runId !== semanticDiagnosticRunId) return;
|
||||
const diagnostic = buildSqlParserErrorDiagnostic(error, range.sql);
|
||||
if (diagnostic) nextDiagnostics.push(...offsetSqlSemanticDiagnostics([diagnostic], range, sql));
|
||||
}
|
||||
}
|
||||
if (options.preserveOutsideRanges) {
|
||||
replaceSemanticDiagnosticsInRanges(nextDiagnostics, diagnosticRanges, sql);
|
||||
} else {
|
||||
setSemanticDiagnostics(nextDiagnostics.sort(compareSqlSemanticDiagnostics));
|
||||
}
|
||||
}
|
||||
|
||||
function scheduleSemanticDiagnostics(delay = 500) {
|
||||
function scheduleSemanticDiagnostics(delay = 500, options: { preserveOutsideRanges?: boolean } = {}) {
|
||||
if (!editorIsActive) return;
|
||||
pendingSemanticDiagnosticPreserveOutsideRanges = !!options.preserveOutsideRanges;
|
||||
if (semanticDiagnosticTimer) clearTimeout(semanticDiagnosticTimer);
|
||||
semanticDiagnosticTimer = setTimeout(() => {
|
||||
const preserveOutsideRanges = pendingSemanticDiagnosticPreserveOutsideRanges;
|
||||
pendingSemanticDiagnosticPreserveOutsideRanges = false;
|
||||
semanticDiagnosticTimer = null;
|
||||
void refreshSemanticDiagnostics();
|
||||
void refreshSemanticDiagnostics({ preserveOutsideRanges });
|
||||
}, delay);
|
||||
}
|
||||
|
||||
@@ -2545,6 +2615,7 @@ function emitEditorViewport(viewport: { scrollTop: number; scrollLeft: number })
|
||||
function scheduleEditorViewportEmit() {
|
||||
if (!view.value || !editorIsActive) return;
|
||||
latestViewport = readEditorViewport(view.value);
|
||||
scheduleSemanticDiagnostics(700, { preserveOutsideRanges: true });
|
||||
if (viewportEmitFrame !== null) return;
|
||||
viewportEmitFrame = requestAnimationFrame(() => {
|
||||
viewportEmitFrame = null;
|
||||
|
||||
Reference in new issue
Block a user