feat(editor): highlight unknown SQL objects

This commit is contained in:
大峰 authored and GitHub committed 2026-09-24 22:34:15 +08:00
1 parent cd24f69daa
commit 46cc9be7d9
4 files changed
+490 -56

No files matched your search

@@ -111,7 +111,7 @@ import { selectionMatchOccurrences } from "@/lib/editor/codemirrorSelectionMatch
import { createInsertValueHintsExtension, requestInsertValueHintsRefresh, supportsInsertValueHints } from "@/lib/editor/codemirrorInsertValueHints";
import { sqlBlockFoldService } from "@/lib/editor/codemirrorSqlBlockFolding";
import { focusEditorView } from "@/lib/editor/queryEditorFocus";
import { createSqlUnknownObjectHighlights, refreshSqlUnknownObjectHighlights } from "@/lib/editor/codemirrorSqlUnknownObjectHighlights";
import { startsQueryEditorRectangularSelection } from "@/lib/editor/queryEditorPointerSelection";
import { LARGE_PASTE_HISTORY_USER_EVENT, normalizeQueryEditorPasteText, recoverableNativePasteSuffix, shouldRecoverLargeTauriPaste } from "@/lib/editor/queryEditorLargePaste";
@@ -122,18 +122,12 @@ import { addNextQueryEditorSelectionOccurrence, selectAllQueryEditorSelectionOcc
import { createQueryEditorStringMouseSelection } from "@/lib/editor/queryEditorStringMouseSelection";
import { createQueryEditorCompletionShortcutBindings } from "@/lib/editor/queryEditorCompletionShortcut";
import { createQueryEditorSelectionCaseShortcutBindings } from "@/lib/editor/queryEditorSelectionCaseShortcut";
import { createQueryEditorExecutionShortcutBindings, createQueryEditorPostCompositionKeyGuard } from "@/lib/editor/queryEditorExecutionShortcut";
import { supportsQueryEditorBlockComments, supportsSqlInListPaste } from "@/lib/database/databaseFeatureSupport";
import { queryContextObjectRoute, queryTableCandidateAtSqlPosition, resolveQueryContextCandidateDatabase, resolveQueryContextObjectTarget, type QueryContextObjectAction } from "@/lib/sql/queryCursorTableTarget";
import * as api from "@/lib/backend/api";
import { isTauriRuntime } from "@/lib/backend/tauriRuntime";
import { resolveSqlDialectId } from "@/lib/sql/semantic/dialect";
import type { SqlCompletionColumn, SqlCompletionContext, SqlCompletionReferencedTable } from "@/lib/sql/sqlCompletion";
const props = defineProps<QueryEditorProps>();
@@ -160,6 +154,9 @@ const COMPLETION_ENTER_MAX_WAIT_MS = 125;
const SQL_SIGNATURE_HELP_WINDOW_CHARS = 10_000;
// Internal rollback switch: flip to false to route completion, diagnostics, and navigation through the legacy SQL context path.
const SEMANTIC_SQL_COMPLETION_ENABLED = true;
// Master switch for colouring table/column names that the connected database does not have.
// There is no settings entry yet: flip this to false to turn the whole feature off.
const SQL_UNKNOWN_OBJECT_HIGHLIGHT_ENABLED = true;
const emit = defineEmits<{
"update:modelValue": [value: string];
@@ -216,7 +213,8 @@ const sqlDriverProfile = computed(() => (props.connectionId ? connectionStore.ge
const MAX_COMPLETION_TABLES = 200;
const PRESTO_ON_DEMAND_TABLE_COMPLETION_MIN_PREFIX = 2;
const PRESTO_ON_DEMAND_TABLE_COMPLETION_LIMIT = 20;
const SQL_UNKNOWN_OBJECT_INITIAL_DELAY_MS = 900;
const SQL_UNKNOWN_OBJECT_DEBOUNCE_MS = 600;
const liveFontSize = ref(settingsStore.editorSettings.fontSize);
const gestureStartFontSize = ref(settingsStore.editorSettings.fontSize);
const isGestureZooming = ref(false);
@@ -1499,52 +1497,55 @@ async function expandSelectStar(target = selectStarExpansionTarget.value) {
currentView.focus();
}
const { sqlErrorDecorationRange, sqlSemanticDecorationRanges, reconfigureDiagnostics, setSemanticDiagnostics, clearScheduledSemanticDiagnostics, invalidateSemanticDiagnosticsForDocumentChange, shouldSkipSqlSemanticDiagnostics, scheduleSemanticDiagnostics } = useQueryEditorDiagnostics({
props,
view,
settingsStore,
connectionStore,
sqlDriverProfile,
sqlStatementParameterOptions,
sqlBehaviorDialect,
semanticCompletionEnabled: SEMANTIC_SQL_COMPLETION_ENABLED,
maxCompletionTables: MAX_COMPLETION_TABLES,
runtime: {
get setSqlDiagnosticsEffect() {
return codeMirrorRuntime.setSqlDiagnosticsEffect;
const { sqlErrorDecorationRange, sqlSemanticDecorationRanges, reconfigureDiagnostics, setSemanticDiagnostics, clearScheduledSemanticDiagnostics, invalidateSemanticDiagnosticsForDocumentChange, shouldSkipSqlSemanticDiagnostics, scheduleSemanticDiagnostics, loadSqlUnknownObjectSpans } =
useQueryEditorDiagnostics({
props,
view,
settingsStore,
connectionStore,
sqlDriverProfile,
sqlStatementParameterOptions,
sqlBehaviorDialect,
queryEditorSelectionLanguage,
semanticCompletionEnabled: SEMANTIC_SQL_COMPLETION_ENABLED,
maxCompletionTables: MAX_COMPLETION_TABLES,
unknownObjectHighlightEnabled: SQL_UNKNOWN_OBJECT_HIGHLIGHT_ENABLED,
runtime: {
get setSqlDiagnosticsEffect() {
return codeMirrorRuntime.setSqlDiagnosticsEffect;
},
get diagnosticComp() {
return codeMirrorRuntime.diagnosticComp;
},
get buildSqlDiagnosticExtension() {
return codeMirrorRuntime.buildSqlDiagnosticExtension;
},
get codeMirrorCompletionStatus() {
return codeMirrorRuntime.codeMirrorCompletionStatus;
},
get executableStatementRangeCache() {
return executableStatementRangeCache;
},
set executableStatementRangeCache(value) {
executableStatementRangeCache = value;
},
get editorIsActive() {
return editorIsActive;
},
},
get diagnosticComp() {
return codeMirrorRuntime.diagnosticComp;
metadata: {
get cachedTables() {
return completionMetadata.cachedTables;
},
cachedColumnsByTable,
loadedColumnsByTable,
usesOracleSessionCompletionColumns,
findExactSemanticDiagnosticTable,
completionCacheKey,
ensureColumnsForTable,
isMissingTableMetadataError,
},
get buildSqlDiagnosticExtension() {
return codeMirrorRuntime.buildSqlDiagnosticExtension;
},
get codeMirrorCompletionStatus() {
return codeMirrorRuntime.codeMirrorCompletionStatus;
},
get executableStatementRangeCache() {
return executableStatementRangeCache;
},
set executableStatementRangeCache(value) {
executableStatementRangeCache = value;
},
get editorIsActive() {
return editorIsActive;
},
},
metadata: {
get cachedTables() {
return completionMetadata.cachedTables;
},
cachedColumnsByTable,
loadedColumnsByTable,
usesOracleSessionCompletionColumns,
findExactSemanticDiagnosticTable,
completionCacheKey,
ensureColumnsForTable,
isMissingTableMetadataError,
},
});
});
async function formatCurrentSql() {
if (props.readOnly) return;
@@ -1872,6 +1873,12 @@ const codeMirrorLifecycle = useQueryEditorCodeMirror({
Prec.highest(keymap.of([{ key: "Space", run: acceptSqlServerCompletionOnSpace }])),
initializedRuntime.sqlLanguageComp.of(sqlExtensions.buildSqlLanguageExtension()),
initializedRuntime.sqlSemanticHighlightComp.of(sqlExtensions.buildSqlSemanticHighlightExtension()),
createSqlUnknownObjectHighlights({
enabled: SQL_UNKNOWN_OBJECT_HIGHLIGHT_ENABLED,
load: loadSqlUnknownObjectSpans,
initialDelayMs: SQL_UNKNOWN_OBJECT_INITIAL_DELAY_MS,
debounceMs: SQL_UNKNOWN_OBJECT_DEBOUNCE_MS,
}),
tooltips({ parent: tooltipParent }),
initializedRuntime.completionComp.of(sqlExtensions.buildSqlCompletionExtension()),
sqlCompletionTheme(EditorView),
@@ -2495,6 +2502,7 @@ function resumeQueryEditorBackgroundWork() {
editorIsActive = true;
registerTableReferenceDropListener();
scheduleSemanticDiagnostics();
view.value?.dispatch({ effects: [refreshSqlUnknownObjectHighlights.of(null)] });
// Warm the database driver/pool while the user is still reading or typing, so
// the first Run does not pay pool creation or external-driver startup.
warmActiveTabConnection();
@@ -17,6 +17,8 @@ import { buildOracleSyntaxDiagnostics } from "@/lib/sql/oracleSyntaxDiagnostics"
import { buildSqlServerRoutineSyntaxDiagnostics } from "@/lib/sql/sqlServerRoutineSyntaxDiagnostics";
import { needsDiagnosticCaretReanchor } from "@/lib/editor/queryEditorDiagnosticCaretAnchor";
import { metadataSchemaForConnection } from "@/lib/database/jdbcDialect";
import { sqlTextFingerprint } from "@/lib/sql/sqlTextFingerprint";
import type { SqlUnknownObjectSpan } from "@/lib/editor/codemirrorSqlUnknownObjectHighlights";
import {
areSqlSemanticDiagnosticsEqual,
buildSqlParserErrorDiagnostic,
@@ -29,11 +31,11 @@ import {
tableReferenceKey,
type SqlSemanticDiagnostic,
} from "@/lib/sql/semantic/diagnostics";
import { sqlReferenceAnalysisDialectFor } from "@/lib/sql/semantic/dialect";
import { resolveSqlDialectId, sqlReferenceAnalysisDialectFor } from "@/lib/sql/semantic/dialect";
import { buildRedisSyntaxDiagnostics, shouldRunRedisDiagnostics } from "@/lib/redis/redisSyntaxDiagnostics";
import { buildMongoSyntaxDiagnostics } from "@/lib/mongo/mongoSyntaxDiagnostics";
import type { SqlCompletionColumn, SqlCompletionTable } from "@/lib/sql/sqlCompletion";
import type { SqlReferenceAnalysis, SqlTableReference, SqlTextSpan } from "@/types/database";
import type { DatabaseType, SqlReferenceAnalysis, SqlTableReference, SqlTextSpan } from "@/types/database";
interface QueryEditorDiagnosticsRuntime {
setSqlDiagnosticsEffect: import("@codemirror/state").StateEffectType<SqlSemanticDiagnostic[]> | null;
@@ -63,8 +65,10 @@ interface QueryEditorDiagnosticsOptions {
sqlDriverProfile: ComputedRef<string | undefined>;
sqlStatementParameterOptions: () => SqlParameterOptions;
sqlBehaviorDialect: () => QueryEditorProps["dialect"];
queryEditorSelectionLanguage: () => "sql" | "text";
semanticCompletionEnabled: boolean;
maxCompletionTables: number;
unknownObjectHighlightEnabled: boolean;
runtime: QueryEditorDiagnosticsRuntime;
metadata: QueryEditorDiagnosticMetadata;
}
@@ -73,6 +77,7 @@ export function useQueryEditorDiagnostics(options: QueryEditorDiagnosticsOptions
const { props, view, settingsStore, connectionStore, sqlDriverProfile, sqlStatementParameterOptions, sqlBehaviorDialect, runtime, metadata } = options;
const SEMANTIC_SQL_COMPLETION_ENABLED = options.semanticCompletionEnabled;
const MAX_COMPLETION_TABLES = options.maxCompletionTables;
const SQL_UNKNOWN_OBJECT_HIGHLIGHT_ENABLED = options.unknownObjectHighlightEnabled;
const MAX_SEMANTIC_DIAGNOSTIC_COLUMN_TABLES = 4;
let semanticDiagnostics: SqlSemanticDiagnostic[] = [];
let semanticDiagnosticTimer: ReturnType<typeof setTimeout> | null = null;
@@ -287,7 +292,7 @@ export function useQueryEditorDiagnostics(options: QueryEditorDiagnosticsOptions
return { tables: enriched, missingTables };
}
async function ensureColumnsForSemanticDiagnostics(tables: SqlTableReference[], scope?: CompletionMetadataScope): Promise<Set<string>> {
async function ensureColumnsForSemanticDiagnostics(tables: SqlTableReference[], scope?: CompletionMetadataScope, limit = MAX_SEMANTIC_DIAGNOSTIC_COLUMN_TABLES): Promise<Set<string>> {
const missingTables = new Set<string>();
const seen = new Set<string>();
const targets: SqlTableReference[] = [];
@@ -303,7 +308,7 @@ export function useQueryEditorDiagnostics(options: QueryEditorDiagnosticsOptions
if (seen.has(normalizedKey)) continue;
seen.add(normalizedKey);
targets.push(table);
if (targets.length >= MAX_SEMANTIC_DIAGNOSTIC_COLUMN_TABLES) break;
if (targets.length >= limit) break;
}
await Promise.all(
targets.map(async (table) => {
@@ -493,6 +498,135 @@ export function useQueryEditorDiagnostics(options: QueryEditorDiagnosticsOptions
onBeforeUnmount(clearScheduledSemanticDiagnostics);
// ==================== Unknown table/column highlighting ====================
const NON_SQL_UNKNOWN_OBJECT_DATABASE_TYPES: ReadonlySet<DatabaseType> = new Set(["qdrant", "milvus", "weaviate", "chromadb", "solr"]);
const MAX_SQL_UNKNOWN_OBJECT_SQL_LENGTH = 200_000;
const MAX_SQL_UNKNOWN_OBJECT_STATEMENTS = 80;
const SQL_UNKNOWN_OBJECT_COLUMN_TABLE_LIMIT = 24;
const MAX_SQL_UNKNOWN_OBJECT_MEMO_ENTRIES = 200;
const sqlUnknownObjectSpanMemo = new Map<string, SqlUnknownObjectSpan[]>();
function sqlUnknownObjectMemoKey(range: SqlTextRange, metadataEpoch: string): string {
return `${sqlTextFingerprint(range.sql)}|${range.from}:${range.to}|${metadataEpoch}`;
}
function rememberSqlUnknownObjectSpans(key: string, spans: SqlUnknownObjectSpan[]) {
sqlUnknownObjectSpanMemo.set(key, spans);
while (sqlUnknownObjectSpanMemo.size > MAX_SQL_UNKNOWN_OBJECT_MEMO_ENTRIES) {
const oldest = sqlUnknownObjectSpanMemo.keys().next();
if (oldest.done) break;
sqlUnknownObjectSpanMemo.delete(oldest.value);
}
}
async function loadSqlUnknownObjectSpans(currentView: EditorViewType): Promise<SqlUnknownObjectSpan[]> {
if (!SQL_UNKNOWN_OBJECT_HIGHLIGHT_ENABLED || !runtime.editorIsActive) return [];
if (options.queryEditorSelectionLanguage() !== "sql") return [];
if (shouldSkipSqlSemanticDiagnostics()) return [];
if (props.databaseType && NON_SQL_UNKNOWN_OBJECT_DATABASE_TYPES.has(props.databaseType)) return [];
if (!props.connectionId || props.database == null) return [];
const doc = currentView.state.doc;
if (doc.length > MAX_SQL_UNKNOWN_OBJECT_SQL_LENGTH) return [];
const sql = doc.toString();
if (!sql.trim()) return [];
if (props.databaseType !== "sqlserver") {
runtime.executableStatementRangeCache = executableStatementRangeCacheForDoc(runtime.executableStatementRangeCache, doc, props.databaseType, sqlStatementParameterOptions());
}
const ranges = sqlSemanticDiagnosticRangesForViewport(sql, [{ from: 0, to: sql.length }], props.databaseType, props.databaseType === "sqlserver" ? undefined : runtime.executableStatementRangeCache?.ranges, sqlStatementParameterOptions()).slice(0, MAX_SQL_UNKNOWN_OBJECT_STATEMENTS);
if (ranges.length === 0) return [];
const metadataEpoch = `${resolveSqlDialectId({ databaseType: props.databaseType, dialect: sqlBehaviorDialect() })}|${props.connectionId}|${props.database}|${props.schema ?? ""}|${props.databaseType}|${semanticDiagnosticRunId}|${metadata.cachedColumnsByTable.size}:${metadata.loadedColumnsByTable.size}`;
const spans: SqlUnknownObjectSpan[] = [];
let analyzedStatements = 0;
let touchedTables = 0;
let columnMetadataLoads = 0;
for (const range of ranges) {
const memoKey = sqlUnknownObjectMemoKey(range, metadataEpoch);
const cachedSpans = sqlUnknownObjectSpanMemo.get(memoKey);
if (cachedSpans) {
spans.push(...cachedSpans);
continue;
}
if (currentView.state.doc !== doc) return [];
const statementSpans: SqlUnknownObjectSpan[] = [];
try {
const analysis = await api.analyzeSqlReferences(
range.sql,
sqlReferenceAnalysisDialectFor({
databaseType: props.databaseType,
identifierQuote: connectionStore.connectionIdentifierQuote(props.connectionId),
fallbackDialect: props.formatDialect ?? props.dialect ?? "generic",
}),
);
if (currentView.state.doc !== doc) return [];
const semanticCursor = Math.max(0, Math.min(currentView.state.selection.main.head - range.from, range.sql.length));
const semanticModel = SEMANTIC_SQL_COMPLETION_ENABLED
? buildSqlSemanticModel(range.sql, semanticCursor, {
databaseType: props.databaseType,
dialect: sqlBehaviorDialect(),
})
: null;
const semanticAnalysis = semanticModel ? mergeSqlSemanticReferenceAnalysis(analysis, semanticModel) : analysis;
const metadataScope = semanticDiagnosticMetadataScope(sql, range);
const scopedAnalysis = {
...semanticAnalysis,
tables: semanticDiagnosticTablesForScope(semanticAnalysis.tables, metadataScope),
};
const { tables, missingTables } = await enrichSemanticDiagnosticTables(scopedAnalysis.tables, metadataScope);
const loadedColumnsBefore = metadata.loadedColumnsByTable.size;
const columnMetadataMissingTables = await ensureColumnsForSemanticDiagnostics(tables, metadataScope, SQL_UNKNOWN_OBJECT_COLUMN_TABLE_LIMIT);
for (const tableKey of columnMetadataMissingTables) missingTables.add(tableKey);
if (currentView.state.doc !== doc) return [];
touchedTables += tables.length;
columnMetadataLoads += Math.max(0, metadata.loadedColumnsByTable.size - loadedColumnsBefore);
const diagnostics = offsetSqlSemanticDiagnostics(
buildSqlSemanticDiagnostics(
{ ...scopedAnalysis, tables },
{
tables: metadata.cachedTables,
columnsByTable: metadata.cachedColumnsByTable,
missingTables,
loadedColumnTables: metadata.loadedColumnsByTable,
sql: range.sql,
databaseType: props.databaseType,
},
),
range,
sql,
);
for (const diagnostic of diagnostics) {
const offsetRange = sqlTextSpanToRange(sql, diagnostic.span);
if (offsetRange) statementSpans.push(offsetRange);
}
} catch {
// A failed parse must not colour anything.
}
analyzedStatements += 1;
rememberSqlUnknownObjectSpans(memoKey, statementSpans);
spans.push(...statementSpans);
}
const dedupedSpans: SqlUnknownObjectSpan[] = [];
const seenSpans = new Set<string>();
for (const span of spans) {
const key = `${span.from}:${span.to}`;
if (seenSpans.has(key)) continue;
seenSpans.add(key);
dedupedSpans.push(span);
}
dedupedSpans.sort((left, right) => left.from - right.from || left.to - right.to);
return dedupedSpans;
}
return {
sqlErrorDecorationRange,
sqlSemanticDecorationRanges,
@@ -502,6 +636,7 @@ export function useQueryEditorDiagnostics(options: QueryEditorDiagnosticsOptions
invalidateSemanticDiagnosticsForDocumentChange,
shouldSkipSqlSemanticDiagnostics,
scheduleSemanticDiagnostics,
loadSqlUnknownObjectSpans,
get diagnostics() {
return semanticDiagnostics;
},