1 parent
41feb0ab20
commit
a95762a536
4 files changed
+209
-18
No files matched your search
@@ -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
|
||||
|
||||
+177
@@ -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<QueryEditorProps> = {}) {
|
||||
const pinia = createPinia();
|
||||
setActivePinia(pinia);
|
||||
const settings = useSettingsStore();
|
||||
settings.editorSettings.showExecutionTargetPicker = false;
|
||||
settings.editorSettings.completionTriggerMode = "manual";
|
||||
const props = reactive<QueryEditorProps>({ modelValue: "S", connectionId: "language-test", databaseType: "qdrant", dialect: "mysql", autoFocus: false, ...overrides });
|
||||
const editor = shallowRef<InstanceType<typeof QueryEditor>>();
|
||||
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);
|
||||
});
|
||||
});
|
||||
@@ -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<null>();
|
||||
|
||||
codeMirrorRuntime.buildSqlSemanticHighlightExtension = () => {
|
||||
if (!supportsQueryEditorSqlLanguage(props.databaseType)) return [];
|
||||
return [
|
||||
createSqlAliasHighlights({ databaseType: props.databaseType, dialect: sqlBehaviorDialect(), enabled: queryEditorSelectionLanguage() === "sql" }),
|
||||
ViewPlugin.fromClass(
|
||||
|
||||
Reference in new issue
Block a user