From a95762a536810083939d82507da6a22b7d18561a Mon Sep 17 00:00:00 2001 From: t8y2 <1156263951@qq.com> Date: Sat, 3 Oct 2026 03:57:07 +0000 Subject: [PATCH] fix(editor): disable SQL language features for Qdrant Closes #10604 --- .../src/components/editor/QueryEditor.vue | 33 ++-- .../QueryEditor.language.integration.spec.ts | 177 ++++++++++++++++++ .../editor/queryEditorSqlExtensions.ts | 13 +- .../src/lib/editor/codemirrorSqlDialect.ts | 4 + 4 files changed, 209 insertions(+), 18 deletions(-) create mode 100644 apps/desktop/src/components/editor/__tests__/QueryEditor.language.integration.spec.ts diff --git a/apps/desktop/src/components/editor/QueryEditor.vue b/apps/desktop/src/components/editor/QueryEditor.vue index 173084b163..d7d82c7a34 100644 --- a/apps/desktop/src/components/editor/QueryEditor.vue +++ b/apps/desktop/src/components/editor/QueryEditor.vue @@ -2423,20 +2423,25 @@ watch([() => props.clientSessionId, () => props.completionContextVersion], () => scheduleSemanticDiagnostics(); }); -watch([() => props.databaseType, () => props.dialect, () => props.syntaxDialect, sqlDriverProfile], () => { - executableStatementRangeCache = null; - statementBoundaries.invalidate(); - if (!view.value || !codeMirrorRuntime.sqlLanguageComp || !codeMirrorRuntime.buildSqlLanguageExtension || !codeMirrorRuntime.sqlSemanticHighlightComp || !codeMirrorRuntime.buildSqlSemanticHighlightExtension || !codeMirrorRuntime.sqlSignatureComp || !codeMirrorRuntime.buildSqlSignatureExtension) - return; - // Signature tooltips depend on the external dialect, so refresh them even when the document and selection stay unchanged. - view.value.dispatch({ - effects: [ - codeMirrorRuntime.sqlLanguageComp.reconfigure(codeMirrorRuntime.buildSqlLanguageExtension()), - codeMirrorRuntime.sqlSemanticHighlightComp.reconfigure(codeMirrorRuntime.buildSqlSemanticHighlightExtension()), - codeMirrorRuntime.sqlSignatureComp.reconfigure(codeMirrorRuntime.buildSqlSignatureExtension()), - ], - }); -}); +watch( + [() => props.tabId, () => props.databaseType, () => props.dialect, () => props.syntaxDialect, sqlDriverProfile], + () => { + executableStatementRangeCache = null; + statementBoundaries.invalidate(); + if (!view.value || !codeMirrorRuntime.sqlLanguageComp || !codeMirrorRuntime.buildSqlLanguageExtension || !codeMirrorRuntime.sqlSemanticHighlightComp || !codeMirrorRuntime.buildSqlSemanticHighlightExtension || !codeMirrorRuntime.sqlSignatureComp || !codeMirrorRuntime.buildSqlSignatureExtension) + return; + // Signature tooltips depend on the external dialect, so refresh them even when the document and selection stay unchanged. + view.value.dispatch({ + effects: [ + codeMirrorRuntime.sqlLanguageComp.reconfigure(codeMirrorRuntime.buildSqlLanguageExtension()), + codeMirrorRuntime.sqlSemanticHighlightComp.reconfigure(codeMirrorRuntime.buildSqlSemanticHighlightExtension()), + codeMirrorRuntime.sqlSignatureComp.reconfigure(codeMirrorRuntime.buildSqlSignatureExtension()), + ], + }); + applyEditorCompletionExtension(); + }, + { flush: "post" }, +); // openGauss compatibility mode is loaded asynchronously from the backend into a // dedicated store map (not the sidebar tree). A restored tab may open before the diff --git a/apps/desktop/src/components/editor/__tests__/QueryEditor.language.integration.spec.ts b/apps/desktop/src/components/editor/__tests__/QueryEditor.language.integration.spec.ts new file mode 100644 index 0000000000..5bbcdc965a --- /dev/null +++ b/apps/desktop/src/components/editor/__tests__/QueryEditor.language.integration.spec.ts @@ -0,0 +1,177 @@ +// @vitest-environment happy-dom + +import { createApp, h, nextTick, reactive, shallowRef } from "vue"; +import { createPinia, setActivePinia } from "pinia"; +import { createI18n } from "vue-i18n"; +import { language, syntaxTree } from "@codemirror/language"; +import { EditorView, showTooltip } from "@codemirror/view"; +import { toggleLineComment, undo } from "@codemirror/commands"; +import { completionStatus, currentCompletions, startCompletion } from "@codemirror/autocomplete"; +import { afterEach, describe, expect, it, vi } from "vitest"; +import QueryEditor from "../QueryEditor.vue"; +import type { QueryEditorProps } from "../queryEditorTypes"; +import { useSettingsStore } from "@/stores/settingsStore"; + +const cleanups: Array<() => void> = []; + +afterEach(() => { + for (const cleanup of cleanups.splice(0).reverse()) cleanup(); + vi.restoreAllMocks(); +}); + +async function mountEditor(overrides: Partial = {}) { + const pinia = createPinia(); + setActivePinia(pinia); + const settings = useSettingsStore(); + settings.editorSettings.showExecutionTargetPicker = false; + settings.editorSettings.completionTriggerMode = "manual"; + const props = reactive({ modelValue: "S", connectionId: "language-test", databaseType: "qdrant", dialect: "mysql", autoFocus: false, ...overrides }); + const editor = shallowRef>(); + const onExecute = vi.fn(); + const host = document.createElement("div"); + document.body.append(host); + const app = createApp({ + render: () => + h(QueryEditor, { + ...props, + ref: editor, + onExecute, + "onUpdate:modelValue": (value: string) => { + props.modelValue = value; + }, + }), + }); + app.use(pinia); + app.use(createI18n({ legacy: false, locale: "en", messages: { en: {} }, missingWarn: false, fallbackWarn: false })); + app.mount(host); + cleanups.push(() => { + app.unmount(); + host.remove(); + }); + await vi.waitFor(() => expect(host.querySelector(".cm-editor")).not.toBeNull(), { timeout: 5000 }); + const view = EditorView.findFromDOM(host.querySelector(".cm-editor") as HTMLElement)!; + return { editor: editor.value!, view, props, host, onExecute, settings }; +} + +function expectPlainText(view: EditorView) { + expect(view.state.facet(language)).toBeNull(); + expect(view.state.languageDataAt("autocomplete", 0)).toEqual([]); + expect(startCompletion(view)).toBe(false); + expect(completionStatus(view.state)).toBeNull(); + expect(currentCompletions(view.state)).toEqual([]); +} + +async function expectSqlCompletion(view: EditorView) { + view.dispatch({ selection: { anchor: view.state.doc.length } }); + expect(startCompletion(view)).toBe(true); + await vi.waitFor(() => expect(completionStatus(view.state)).toBe("active")); + expect(currentCompletions(view.state).map((option) => option.label)).toEqual(expect.arrayContaining(["SELECT", "SUM", "SUBSTR"])); +} + +describe("QueryEditor language capabilities", () => { + it("does not install SQL grammar or expose SQL completions for Qdrant", async () => { + const { view } = await mountEditor(); + expectPlainText(view); + }); + + it("does not offer SQL function signatures for Qdrant", async () => { + const { view } = await mountEditor({ modelValue: "SELECT SUM(", initialSelection: { anchor: 11, head: 11 } }); + expect(view.state.facet(showTooltip).filter(Boolean)).toEqual([]); + }); + + it.each(["mysql", "postgres", "jdbc", undefined] as const)("preserves SQL grammar and completion for %s", async (databaseType) => { + const { view, props } = await mountEditor({ databaseType, dialect: databaseType === "postgres" ? "postgres" : "mysql" }); + expect(view.state.facet(language)).not.toBeNull(); + expect(view.state.languageDataAt("autocomplete", 0)).not.toEqual([]); + await expectSqlCompletion(view); + props.modelValue = "SELECT SUM("; + await nextTick(); + view.dispatch({ selection: { anchor: view.state.doc.length } }); + expect(syntaxTree(view.state).resolveInner(3).name).toBe("Keyword"); + expect(view.state.facet(showTooltip).filter(Boolean)).not.toEqual([]); + }); + + it("clears active SQL completion on a connection type change and restores it on return", async () => { + const { view, props, host } = await mountEditor({ databaseType: "mysql" }); + await expectSqlCompletion(view); + props.databaseType = "qdrant"; + await nextTick(); + expectPlainText(view); + expect(view.state.doc.toString()).toBe("S"); + props.databaseType = "mysql"; + await nextTick(); + await expectSqlCompletion(view); + expect(host.querySelector(".cm-editor")).toBe(view.dom); + }); + + it("keeps the Qdrant language gate when cached editor tabs are restored", async () => { + const { view, props } = await mountEditor({ tabId: "qdrant-tab" }); + expectPlainText(view); + props.tabId = "sql-tab"; + props.databaseType = "mysql"; + await nextTick(); + await expectSqlCompletion(view); + props.tabId = "qdrant-tab"; + props.databaseType = "qdrant"; + await nextTick(); + expectPlainText(view); + }); + + it("reconfigures a cached SQL tab reassigned to Qdrant even when the active database type stays the same", async () => { + const { view, props } = await mountEditor({ tabId: "reassigned-tab", databaseType: "mysql" }); + await expectSqlCompletion(view); + props.tabId = "qdrant-tab"; + props.databaseType = "qdrant"; + await nextTick(); + expectPlainText(view); + props.tabId = "reassigned-tab"; + await nextTick(); + expectPlainText(view); + }); + + it("keeps SQL completion disabled after completion settings change", async () => { + const { view, settings } = await mountEditor(); + settings.editorSettings.selectFirstCompletionOnOpen = !settings.editorSettings.selectFirstCompletionOnOpen; + await nextTick(); + expectPlainText(view); + }); + + it("preserves MongoDB shell comments when switching from Qdrant", async () => { + const { view, props } = await mountEditor({ modelValue: "db.items.find({})" }); + props.databaseType = "mongodb"; + await nextTick(); + expect(toggleLineComment(view)).toBe(true); + expect(view.state.doc.toString()).toBe("// db.items.find({})"); + expect(undo(view)).toBe(true); + expect(view.state.doc.toString()).toBe("db.items.find({})"); + }); + + it.each([false, true])("preserves Qdrant clipboard input and read-only behavior (readOnly: %s)", async (readOnly) => { + const { view } = await mountEditor({ modelValue: "", readOnly }); + const request = '{"collection":"items","limit":10}'; + const event = new Event("paste", { bubbles: true, cancelable: true }); + Object.defineProperty(event, "clipboardData", { value: { getData: (type: string) => (type === "text/plain" ? request : ""), files: [] } }); + view.contentDOM.dispatchEvent(event); + await nextTick(); + expect(view.state.doc.toString()).toBe(readOnly ? "" : request); + expectPlainText(view); + }); + + it("preserves JSON editing, comments, undo, selection and execution for Qdrant", async () => { + const request = '{"collection":"items","limit":10}'; + const { view, editor, onExecute } = await mountEditor({ modelValue: request }); + const limit = request.indexOf("10"); + view.dispatch({ changes: { from: limit, to: limit + 2, insert: "20" }, userEvent: "input" }); + expect(view.state.doc.toString()).toBe(request.replace("10", "20")); + expect(undo(view)).toBe(true); + expect(view.state.doc.toString()).toBe(request); + expect(toggleLineComment(view)).toBe(true); + expect(view.state.doc.toString()).toBe(`-- ${request}`); + expect(undo(view)).toBe(true); + view.dispatch({ selection: { anchor: 0, head: request.length } }); + expect(editor.captureExecutionSnapshot()?.selectedSql).toBe(request); + expect(editor.requestExecute({ bypassPicker: true })).toBe(true); + expect(onExecute.mock.lastCall?.[0].selectedSql).toBe(request); + expect(view.state.doc.toString()).toBe(request); + }); +}); diff --git a/apps/desktop/src/components/editor/queryEditorSqlExtensions.ts b/apps/desktop/src/components/editor/queryEditorSqlExtensions.ts index ab0bd94c6a..03cf3d3690 100644 --- a/apps/desktop/src/components/editor/queryEditorSqlExtensions.ts +++ b/apps/desktop/src/components/editor/queryEditorSqlExtensions.ts @@ -13,7 +13,7 @@ import { createStatementGutterMarkerDom } from "@/lib/editor/codemirrorStatement import { compareSqlCompletions } from "@/lib/editor/sqlCompletionPresentation"; import { currentStatementFrameLayer } from "@/lib/editor/codemirrorCurrentStatementFrameLayer"; import { createSqlAliasHighlights } from "@/lib/editor/codemirrorSqlAliasHighlights"; -import { createDbxCodeMirrorSqlDialect } from "@/lib/editor/codemirrorSqlDialect"; +import { createDbxCodeMirrorSqlDialect, supportsQueryEditorSqlLanguage } from "@/lib/editor/codemirrorSqlDialect"; import { sqlSemanticTableNameSpansForSyntaxTree } from "@/lib/editor/codemirrorSqlSemanticHighlight"; import { queryEditorCommentTokens, queryEditorLineCommentToken, queryEditorWordLanguageData } from "@/lib/editor/queryEditorLineComment"; import { createShellLineCommentHighlight } from "@/lib/editor/codemirrorShellLineCommentHighlight"; @@ -293,6 +293,7 @@ export function configureQueryEditorSqlExtensions(options: QueryEditorSqlExtensi }; codeMirrorRuntime.buildSqlSignatureExtension = () => { + if (!supportsQueryEditorSqlLanguage(props.databaseType)) return []; return showTooltip.compute(["doc", "selection"], (currentState) => { const cursor = currentState.selection.main.head; // Signature detection only scans backward from the cursor, so window the @@ -311,6 +312,7 @@ export function configureQueryEditorSqlExtensions(options: QueryEditorSqlExtensi }; codeMirrorRuntime.buildSqlCompletionExtension = () => { + if (!supportsQueryEditorSqlLanguage(props.databaseType)) return []; return autocompletion({ activateOnTyping: true, defaultKeymap: false, @@ -330,9 +332,11 @@ export function configureQueryEditorSqlExtensions(options: QueryEditorSqlExtensi const shellLineCommentHighlightPlugin = createShellLineCommentHighlight({ ViewPlugin, Decoration, highlightingFor, syntaxTree }); codeMirrorRuntime.buildSqlLanguageExtension = () => [ - langSql.sql({ - dialect: createDbxCodeMirrorSqlDialect(langSql, props.syntaxDialect ?? props.dialect, props.databaseType, sqlDriverProfile.value), - }), + supportsQueryEditorSqlLanguage(props.databaseType) + ? langSql.sql({ + dialect: createDbxCodeMirrorSqlDialect(langSql, props.syntaxDialect ?? props.dialect, props.databaseType, sqlDriverProfile.value), + }) + : [], // Non-SQL editors (MongoDB shell) keep the SQL grammar for highlighting, so override the // comment marker that toggleLineComment reads from language data. Prec.highest(EditorState.languageData.of(() => [{ commentTokens: queryEditorCommentTokens(props.databaseType) }])), @@ -350,6 +354,7 @@ export function configureQueryEditorSqlExtensions(options: QueryEditorSqlExtensi const refreshSqlSemanticHighlightEffect = StateEffect.define(); codeMirrorRuntime.buildSqlSemanticHighlightExtension = () => { + if (!supportsQueryEditorSqlLanguage(props.databaseType)) return []; return [ createSqlAliasHighlights({ databaseType: props.databaseType, dialect: sqlBehaviorDialect(), enabled: queryEditorSelectionLanguage() === "sql" }), ViewPlugin.fromClass( diff --git a/apps/desktop/src/lib/editor/codemirrorSqlDialect.ts b/apps/desktop/src/lib/editor/codemirrorSqlDialect.ts index df056e35c2..70958ac9ac 100644 --- a/apps/desktop/src/lib/editor/codemirrorSqlDialect.ts +++ b/apps/desktop/src/lib/editor/codemirrorSqlDialect.ts @@ -4,6 +4,10 @@ import { driverProfileSqlBuiltinTerms } from "@/lib/database/driverProfileExtens export type CodeMirrorSqlDialectName = "mysql" | "postgres" | "sqlserver" | "clickhouse" | "soql"; +export function supportsQueryEditorSqlLanguage(databaseType?: DatabaseType): boolean { + return databaseType !== "qdrant"; +} + type CodeMirrorSqlLanguageModule = Pick; const MYSQL_CODEMIRROR_DATABASE_TYPES = new Set(["mysql", "doris", "starrocks", "manticoresearch", "goldendb", "gbase"]);