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
@@ -1,7 +1,7 @@
|
||||
import * as langSql from "@codemirror/lang-sql";
|
||||
import { ensureSyntaxTree, foldable } from "@codemirror/language";
|
||||
import { ensureSyntaxTree, foldable, foldService } from "@codemirror/language";
|
||||
import { Compartment, EditorState } from "@codemirror/state";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { createDbxCodeMirrorSqlDialect } from "@/lib/editor/codemirrorSqlDialect";
|
||||
import { collectUnionBranchFoldRanges, createSqlBlockFoldService, sqlBlockFoldService } from "@/lib/editor/codemirrorSqlBlockFolding";
|
||||
|
||||
@@ -21,6 +21,20 @@ function foldedTextAtLine(state: EditorState, lineNumber: number): string | null
|
||||
}
|
||||
|
||||
describe("sqlBlockFoldService", () => {
|
||||
it("does not flatten SQL documents for REST request detection", () => {
|
||||
const state = stateFor("BEGIN\n SELECT 1;\nEND", "mysql", createSqlBlockFoldService("mysql"));
|
||||
const flatten = vi.spyOn(state.doc, "toString");
|
||||
expect(foldedTextAtLine(state, 1)).toBe("\n SELECT 1;\n");
|
||||
expect(flatten).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("uses background ranges without falling back to a synchronous scan", () => {
|
||||
const cached = vi.fn(() => null);
|
||||
const state = stateFor("BEGIN\n SELECT 1;\nEND", "mysql", createSqlBlockFoldService("mysql", cached));
|
||||
const service = state.facet(foldService)[0];
|
||||
expect(service(state, 0, state.doc.line(1).to)).toBeNull();
|
||||
expect(cached).toHaveBeenCalled();
|
||||
});
|
||||
it.each(["elasticsearch", "easysearch", "meilisearch"] as const)("folds one %s REST request without a semicolon", (databaseType) => {
|
||||
const sql = `POST /orders/_search
|
||||
{
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
import { foldService, syntaxTree } from "@codemirror/language";
|
||||
import type { EditorState } from "@codemirror/state";
|
||||
import { elasticsearchRestRequestRanges } from "@/lib/sql/sqlStatementRanges";
|
||||
import type { DatabaseType } from "@/types/database";
|
||||
import { isElasticsearchCompatibleDatabaseType, isMeilisearchDatabaseType, isSolrDatabaseType, type DatabaseType } from "@/types/database";
|
||||
|
||||
// `@lezer/common` is only a transitive dependency here (see sqlSyntaxTreeWindow.ts's comment on
|
||||
// the same pattern), so derive the node types structurally instead of importing them.
|
||||
type SyntaxNode = ReturnType<ReturnType<typeof syntaxTree>["resolve"]>;
|
||||
type Tree = ReturnType<typeof syntaxTree>;
|
||||
|
||||
interface FoldRange {
|
||||
export interface FoldRange {
|
||||
from: number;
|
||||
to: number;
|
||||
}
|
||||
@@ -151,6 +151,7 @@ function addQueryStructureFoldRanges(state: EditorState, tree: Tree, ranges: Map
|
||||
}
|
||||
|
||||
function addRestRequestFoldRanges(state: EditorState, ranges: Map<number, FoldRange>, databaseType?: DatabaseType): boolean {
|
||||
if (databaseType && !isElasticsearchCompatibleDatabaseType(databaseType) && !isMeilisearchDatabaseType(databaseType) && !isSolrDatabaseType(databaseType)) return false;
|
||||
const requests = elasticsearchRestRequestRanges(state.doc.toString(), databaseType ?? "elasticsearch");
|
||||
if (requests.length === 0) return false;
|
||||
|
||||
@@ -160,8 +161,7 @@ function addRestRequestFoldRanges(state: EditorState, ranges: Map<number, FoldRa
|
||||
return true;
|
||||
}
|
||||
|
||||
function computeBlockFoldRanges(state: EditorState, databaseType?: DatabaseType): Map<number, FoldRange> {
|
||||
const tree = syntaxTree(state);
|
||||
export function computeBlockFoldRanges(state: EditorState, databaseType?: DatabaseType, tree: Tree = syntaxTree(state)): Map<number, FoldRange> {
|
||||
const cacheByDatabaseType = rangeCache.get(tree);
|
||||
const cacheKey = databaseType ?? "auto-detect";
|
||||
const cached = cacheByDatabaseType?.get(cacheKey);
|
||||
@@ -220,9 +220,11 @@ function computeBlockFoldRanges(state: EditorState, databaseType?: DatabaseType)
|
||||
}
|
||||
|
||||
/** Creates folding for REST requests, procedural blocks, query parentheses, and UNION branches. */
|
||||
export function createSqlBlockFoldService(databaseType?: DatabaseType) {
|
||||
export function createSqlBlockFoldService(databaseType?: DatabaseType | (() => DatabaseType | undefined), cachedRange?: (state: EditorState, lineStart: number) => FoldRange | null | undefined) {
|
||||
return foldService.of((state, lineStart) => {
|
||||
const range = computeBlockFoldRanges(state, databaseType).get(state.doc.lineAt(lineStart).number);
|
||||
const cached = cachedRange?.(state, lineStart);
|
||||
if (cached !== undefined) return cached;
|
||||
const range = computeBlockFoldRanges(state, typeof databaseType === "function" ? databaseType() : databaseType).get(state.doc.lineAt(lineStart).number);
|
||||
return range ?? null;
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,5 +1,16 @@
|
||||
export const QUERY_EDITOR_FULL_FEATURE_MAX_DOCUMENT_LENGTH = 1024 * 1024;
|
||||
export const QUERY_EDITOR_FULL_FEATURE_MAX_LINE_COUNT = 2_000;
|
||||
|
||||
export function shouldUseQueryEditorLargeDocumentModeForSize(documentLength: number, lineCount: number): boolean {
|
||||
return documentLength > QUERY_EDITOR_FULL_FEATURE_MAX_DOCUMENT_LENGTH || lineCount > QUERY_EDITOR_FULL_FEATURE_MAX_LINE_COUNT;
|
||||
}
|
||||
|
||||
export function shouldUseQueryEditorLargeDocumentMode(document: string): boolean {
|
||||
return document.length > QUERY_EDITOR_FULL_FEATURE_MAX_DOCUMENT_LENGTH;
|
||||
if (document.length > QUERY_EDITOR_FULL_FEATURE_MAX_DOCUMENT_LENGTH) return true;
|
||||
let lineCount = 1;
|
||||
for (let offset = document.indexOf("\n"); offset >= 0; offset = document.indexOf("\n", offset + 1)) {
|
||||
lineCount += 1;
|
||||
if (lineCount > QUERY_EDITOR_FULL_FEATURE_MAX_LINE_COUNT) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
Reference in new issue
Block a user