fix(editor): move large SQL analysis off the input path
This commit is contained in:
30 files changed
+1241
-211
No files matched your search
+85
-3
@@ -6,12 +6,15 @@ import { createI18n } from "vue-i18n";
|
||||
import { language } from "@codemirror/language";
|
||||
import { EditorView } from "@codemirror/view";
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { QUERY_EDITOR_FULL_FEATURE_MAX_DOCUMENT_LENGTH } from "@/lib/editor/queryEditorLargeDocument";
|
||||
import { QUERY_EDITOR_FULL_FEATURE_MAX_DOCUMENT_LENGTH, QUERY_EDITOR_FULL_FEATURE_MAX_LINE_COUNT } from "@/lib/editor/queryEditorLargeDocument";
|
||||
import type { QueryEditorProps } from "../queryEditorTypes";
|
||||
|
||||
const analysisProbe = vi.hoisted(() => ({
|
||||
documentLengths: [] as number[],
|
||||
documentLineCounts: [] as number[],
|
||||
backgroundDocuments: [] as string[],
|
||||
failAtLength: Number.POSITIVE_INFINITY,
|
||||
failAtLineCount: Number.POSITIVE_INFINITY,
|
||||
}));
|
||||
|
||||
vi.mock("@/lib/sql/executableStatementRangeCache", async (importOriginal) => {
|
||||
@@ -19,13 +22,41 @@ vi.mock("@/lib/sql/executableStatementRangeCache", async (importOriginal) => {
|
||||
return {
|
||||
...actual,
|
||||
executableStatementRangeCacheForDoc: (...args: Parameters<typeof actual.executableStatementRangeCacheForDoc>) => {
|
||||
if (args[0]?.doc === args[1]) return actual.executableStatementRangeCacheForDoc(...args);
|
||||
analysisProbe.documentLengths.push(args[1].length);
|
||||
if (args[1].length >= analysisProbe.failAtLength) throw new Error("unbounded statement analysis");
|
||||
analysisProbe.documentLineCounts.push(args[1].lines);
|
||||
if (args[1].length >= analysisProbe.failAtLength || args[1].lines >= analysisProbe.failAtLineCount) throw new Error("unbounded statement analysis");
|
||||
return actual.executableStatementRangeCacheForDoc(...args);
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
vi.mock("@/lib/sql/sqlStatementAnalysisWorker", async () => {
|
||||
const actual = await vi.importActual<typeof import("@/lib/sql/executableStatementRangeCache")>("@/lib/sql/executableStatementRangeCache");
|
||||
const { Text } = await import("@codemirror/state");
|
||||
return {
|
||||
createSqlStatementAnalysisWorker: () => {
|
||||
let generation = 0;
|
||||
return {
|
||||
async analyze(request: import("@/lib/sql/sqlStatementAnalysis").SqlStatementAnalysisRequest) {
|
||||
const current = ++generation;
|
||||
analysisProbe.backgroundDocuments.push(request.sql);
|
||||
await Promise.resolve();
|
||||
if (current !== generation) return null;
|
||||
const { doc: _doc, ...result } = actual.executableStatementRangeCacheForDoc(null, Text.of(request.sql.split("\n")), request.databaseType, request.parameterOptions);
|
||||
return result;
|
||||
},
|
||||
cancel() {
|
||||
generation++;
|
||||
},
|
||||
dispose() {
|
||||
generation++;
|
||||
},
|
||||
};
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
import QueryEditor from "../QueryEditor.vue";
|
||||
|
||||
function oraclePackage(procedureCount: number): string {
|
||||
@@ -50,7 +81,10 @@ const cleanups: Array<() => void> = [];
|
||||
|
||||
beforeEach(() => {
|
||||
analysisProbe.documentLengths = [];
|
||||
analysisProbe.documentLineCounts = [];
|
||||
analysisProbe.backgroundDocuments = [];
|
||||
analysisProbe.failAtLength = Number.POSITIVE_INFINITY;
|
||||
analysisProbe.failAtLineCount = Number.POSITIVE_INFINITY;
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
@@ -103,6 +137,40 @@ describe("QueryEditor large document mode", () => {
|
||||
expect(view.state.doc.toString()).toBe(source);
|
||||
});
|
||||
|
||||
it("keeps SQL language features while bounding statement analysis above the line budget", async () => {
|
||||
const lineCount = QUERY_EDITOR_FULL_FEATURE_MAX_LINE_COUNT + 1;
|
||||
const source = Array.from({ length: lineCount }, (_, index) => `SELECT ${index};`).join("\n");
|
||||
expect(source.length).toBeLessThan(QUERY_EDITOR_FULL_FEATURE_MAX_DOCUMENT_LENGTH);
|
||||
analysisProbe.failAtLineCount = lineCount;
|
||||
|
||||
const { host, view } = await mountEditor(source);
|
||||
|
||||
expect(host.firstElementChild?.getAttribute("data-large-document-mode")).toBe("true");
|
||||
expect(analysisProbe.documentLineCounts).not.toContain(lineCount);
|
||||
expect(view.state.facet(language)).not.toBeNull();
|
||||
expect(analysisProbe.backgroundDocuments).toContain(source);
|
||||
});
|
||||
|
||||
it("enters bounded mode when typing crosses the line budget", async () => {
|
||||
const source = Array.from({ length: QUERY_EDITOR_FULL_FEATURE_MAX_LINE_COUNT }, (_, index) => `SELECT ${index};`).join("\n");
|
||||
const { host, view } = await mountEditor(source);
|
||||
analysisProbe.documentLengths = [];
|
||||
analysisProbe.documentLineCounts = [];
|
||||
|
||||
view.dispatch({
|
||||
changes: { from: view.state.doc.length, insert: "\nS" },
|
||||
selection: { anchor: view.state.doc.length + 2 },
|
||||
userEvent: "input.type",
|
||||
});
|
||||
await nextTick();
|
||||
await new Promise((resolve) => setTimeout(resolve, 250));
|
||||
|
||||
expect(host.firstElementChild?.getAttribute("data-large-document-mode")).toBe("true");
|
||||
expect(view.state.doc.lines).toBe(QUERY_EDITOR_FULL_FEATURE_MAX_LINE_COUNT + 1);
|
||||
expect(view.state.facet(language)).not.toBeNull();
|
||||
expect(analysisProbe.documentLineCounts).not.toContain(QUERY_EDITOR_FULL_FEATURE_MAX_LINE_COUNT + 1);
|
||||
});
|
||||
|
||||
it("keeps a realistically large package editable and searchable without full-document analysis", async () => {
|
||||
const source = oraclePackage(4_000);
|
||||
expect(source.length).toBeGreaterThan(QUERY_EDITOR_FULL_FEATURE_MAX_DOCUMENT_LENGTH);
|
||||
@@ -112,7 +180,7 @@ describe("QueryEditor large document mode", () => {
|
||||
|
||||
expect(host.firstElementChild?.getAttribute("data-large-document-mode")).toBe("true");
|
||||
expect(analysisProbe.documentLengths).not.toContain(source.length);
|
||||
expect(view.state.facet(language)).toBeNull();
|
||||
expect(view.state.facet(language)).not.toBeNull();
|
||||
expect(view.state.doc.toString()).toBe(source);
|
||||
expect(view.state.readOnly).toBe(false);
|
||||
expect(view.state.sliceDoc(0, 24)).toBe(source.slice(0, 24));
|
||||
@@ -137,4 +205,18 @@ describe("QueryEditor large document mode", () => {
|
||||
expect(view.state.readOnly).toBe(true);
|
||||
expect(view.contentDOM.getAttribute("contenteditable")).toBe("false");
|
||||
});
|
||||
|
||||
it("returns to the ordinary path after shrinking and preserves undo", async () => {
|
||||
const source = Array.from({ length: 2_001 }, (_, index) => `SELECT ${index};`).join("\n");
|
||||
const { host, view } = await mountEditor(source);
|
||||
view.dispatch({ changes: { from: 0, to: view.state.doc.length, insert: "SELECT 1;" }, userEvent: "input" });
|
||||
await nextTick();
|
||||
expect(host.firstElementChild?.hasAttribute("data-large-document-mode")).toBe(false);
|
||||
expect(view.state.facet(language)).not.toBeNull();
|
||||
const { undo } = await import("@codemirror/commands");
|
||||
expect(undo(view)).toBe(true);
|
||||
await nextTick();
|
||||
expect(view.state.doc.toString()).toBe(source);
|
||||
expect(host.firstElementChild?.getAttribute("data-large-document-mode")).toBe("true");
|
||||
});
|
||||
});
|
||||
+37
-1
@@ -10,6 +10,7 @@ import { useQueryEditorCompletionMetadata } from "../useQueryEditorCompletionMet
|
||||
import type { QueryEditorProps } from "../queryEditorTypes";
|
||||
import type { RedisCommandDocumentation } from "@/lib/redis/redisCommandDocs";
|
||||
import type { SqlCompletionTable } from "@/lib/sql/sqlCompletion";
|
||||
import { analyzeSqlCompletion, type SqlCompletionAnalysisResult } from "@/lib/sql/sqlCompletionAnalysis";
|
||||
|
||||
vi.mock("@/stores/connectionStore", () => ({ COMPLETION_METADATA_CONCURRENCY: 4 }));
|
||||
vi.mock("@/lib/backend/api", () => ({}));
|
||||
@@ -31,7 +32,7 @@ afterEach(() => {
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
function createHarness(overrides: Partial<QueryEditorProps> = {}) {
|
||||
function createHarness(overrides: Partial<QueryEditorProps> = {}, configureMetadata?: (metadata: ReturnType<typeof useQueryEditorCompletionMetadata>) => void) {
|
||||
const props = reactive<QueryEditorProps>({ modelValue: "SEL", databaseType: "mysql", dialect: "mysql", connectionId: "connection", database: "demo", ...overrides });
|
||||
const parent = document.createElement("div");
|
||||
document.body.append(parent);
|
||||
@@ -65,6 +66,7 @@ function createHarness(overrides: Partial<QueryEditorProps> = {}) {
|
||||
const connectionStore = store as unknown as Options["connectionStore"];
|
||||
const settings = reactive({ editorSettings: { completionTriggerMode: "positional", snippets: [], sqlFormatter: { keywordCase: "upper", functionCase: "upper" }, autoAliasTables: false, tableCompletionSchemaQualification: "collision", generateSqlQuoteIdentifiers: false } });
|
||||
const metadata = useQueryEditorCompletionMetadata({ props, view, connectionStore, sqlBehaviorDialect: () => props.dialect, remoteLatencyBudgetMs: 40, maxCompletionTables: 100, onDemandMinPrefix: 2, semanticCompletionEnabled: false });
|
||||
configureMetadata?.(metadata);
|
||||
const startCompletion = vi.fn(() => true);
|
||||
const runtime: Options["runtime"] = { codeMirrorStartCompletion: startCompletion, codeMirrorInsertCompletionText: insertCompletionText, codeMirrorSnippetCompletion: snippetCompletion, codeMirrorCompletionStatus: () => null, imeCompositionActive: false };
|
||||
const batchSelection = {
|
||||
@@ -102,6 +104,40 @@ function createHarness(overrides: Partial<QueryEditorProps> = {}) {
|
||||
}
|
||||
|
||||
describe("QueryEditor completion provider ownership", () => {
|
||||
it.each(["cursor", "document", "view", "composition", "mode"])("rejects asynchronous analysis after a %s change", async (change) => {
|
||||
let resolve!: (result: SqlCompletionAnalysisResult) => void;
|
||||
const pending = new Promise<SqlCompletionAnalysisResult>((done) => {
|
||||
resolve = done;
|
||||
});
|
||||
const { currentView, view, runtime, settings, completion, startCompletion } = createHarness({}, (metadata) => {
|
||||
vi.spyOn(metadata, "getEditorSqlCompletionAnalysis").mockReturnValue(pending);
|
||||
});
|
||||
completion.scheduleDeferredCompletionTrigger(currentView, "L", "");
|
||||
await vi.advanceTimersByTimeAsync(25);
|
||||
expect(startCompletion).not.toHaveBeenCalled();
|
||||
if (change === "cursor") currentView.dispatch({ selection: { anchor: 0 } });
|
||||
if (change === "document") currentView.dispatch({ changes: { from: 0, insert: " " } });
|
||||
if (change === "view") view.value = null;
|
||||
if (change === "composition") runtime.imeCompositionActive = true;
|
||||
if (change === "mode") settings.editorSettings.completionTriggerMode = "manual";
|
||||
resolve(analyzeSqlCompletion({ sql: "SEL", cursor: 3, databaseType: "mysql", semanticCompletionEnabled: false }));
|
||||
await vi.advanceTimersByTimeAsync(50);
|
||||
expect(startCompletion).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("discards a provider result invalidated while background analysis is running", async () => {
|
||||
let resolve!: (result: SqlCompletionAnalysisResult) => void;
|
||||
const pending = new Promise<SqlCompletionAnalysisResult>((done) => {
|
||||
resolve = done;
|
||||
});
|
||||
const { provide, completion } = createHarness({}, (metadata) => {
|
||||
vi.spyOn(metadata, "getEditorSqlCompletionAnalysis").mockReturnValue(pending);
|
||||
});
|
||||
const result = provide();
|
||||
completion.invalidateRequests();
|
||||
resolve(analyzeSqlCompletion({ sql: "SEL", cursor: 3, databaseType: "mysql", semanticCompletionEnabled: false }));
|
||||
await expect(result).resolves.toBeNull();
|
||||
});
|
||||
it("keeps manual completion available while automatic completion is disabled", async () => {
|
||||
const { settings, provide, completion, currentView, startCompletion } = createHarness({ database: undefined });
|
||||
settings.editorSettings.completionTriggerMode = "manual";
|
||||
|
||||
+43
-3
@@ -20,7 +20,7 @@ afterEach(() => {
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
function createHarness() {
|
||||
function createHarness(doc = "SELECT 1;\nSELECT 2;") {
|
||||
const view = shallowRef<EditorView | null>(null);
|
||||
const cache = { value: null as ExecutableStatementRangeCache | null };
|
||||
const runtime = createQueryEditorCodeMirrorRuntime();
|
||||
@@ -34,7 +34,7 @@ function createHarness() {
|
||||
});
|
||||
const parent = document.createElement("div");
|
||||
document.body.append(parent);
|
||||
const currentView = new EditorView({ parent, state: EditorState.create({ doc: "SELECT 1;\nSELECT 2;", extensions: [boundaries.createTrackingPlugin(ViewPlugin)] }) });
|
||||
const currentView = new EditorView({ parent, state: EditorState.create({ doc, extensions: [boundaries.createTrackingPlugin(ViewPlugin)] }) });
|
||||
view.value = currentView;
|
||||
cleanups.push(() => {
|
||||
currentView.destroy();
|
||||
@@ -87,6 +87,19 @@ describe("QueryEditor shared statement boundaries", () => {
|
||||
expect(boundaries.statementBoundariesForState(currentView.state).doc).toBe(currentView.state.doc);
|
||||
});
|
||||
|
||||
it("clears a pending refresh when the editor is paused", async () => {
|
||||
const { currentView, cache, boundaries } = createHarness();
|
||||
boundaries.statementBoundariesForState(currentView.state);
|
||||
currentView.dispatch({ changes: { from: 0, insert: " " } });
|
||||
|
||||
boundaries.clear();
|
||||
const dispatch = vi.spyOn(currentView, "dispatch");
|
||||
await vi.advanceTimersByTimeAsync(200);
|
||||
|
||||
expect(cache.value).toBeNull();
|
||||
expect(dispatch).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it.each(["destroyed", "replaced", "detached"])("does not refresh an editor that was %s", async (reason) => {
|
||||
const { currentView, view, parent, boundaries, cache } = createHarness();
|
||||
boundaries.statementBoundariesForState(currentView.state);
|
||||
@@ -98,6 +111,33 @@ describe("QueryEditor shared statement boundaries", () => {
|
||||
const dispatch = vi.spyOn(currentView, "dispatch");
|
||||
await vi.advanceTimersByTimeAsync(200);
|
||||
expect(dispatch).not.toHaveBeenCalled();
|
||||
expect(cache.value).toBe(previousCache);
|
||||
expect(cache.value).toBe(reason === "destroyed" ? null : previousCache);
|
||||
});
|
||||
|
||||
it("maps large-document folds while an exact background rebuild is pending", async () => {
|
||||
const source = `BEGIN\n${"SELECT 1;\n".repeat(2_001)}END;`;
|
||||
const { currentView, boundaries } = createHarness(source);
|
||||
const initial = boundaries.statementBoundariesForState(currentView.state);
|
||||
expect(initial.fresh).toBe(false);
|
||||
await boundaries.ensureStatementCache(currentView.state);
|
||||
const original = boundaries.foldRangeForState(currentView.state, 0);
|
||||
expect(original).toEqual({ from: 5, to: source.indexOf("END;") });
|
||||
currentView.dispatch({ changes: { from: 0, insert: "-- prefix\n" } });
|
||||
expect(boundaries.statementBoundariesForState(currentView.state).fresh).toBe(false);
|
||||
expect(boundaries.foldRangeForState(currentView.state, 10)).toEqual({ from: original!.from + 10, to: original!.to + 10 });
|
||||
await vi.advanceTimersByTimeAsync(150);
|
||||
expect(boundaries.statementBoundariesForState(currentView.state).fresh).toBe(true);
|
||||
});
|
||||
|
||||
it("never installs a background result for an edited or paused document", async () => {
|
||||
const { currentView, boundaries, cache } = createHarness("SELECT 1;\n".repeat(2_001));
|
||||
const pending = boundaries.ensureStatementCache(currentView.state);
|
||||
currentView.dispatch({ changes: { from: 0, insert: " " } });
|
||||
await expect(pending).resolves.toBeNull();
|
||||
expect(cache.value).toBeNull();
|
||||
const paused = boundaries.ensureStatementCache(currentView.state);
|
||||
boundaries.clear();
|
||||
await expect(paused).resolves.toBeNull();
|
||||
expect(cache.value).toBeNull();
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user