feat(editor): add query editing utilities

This commit is contained in:
JeaceLiu authored and GitHub committed 2026-07-17 16:32:50 +08:00
1 parent d689d78ab0
commit 782615586c
15 files changed
+989 -59

No files matched your search

@@ -1,6 +1,6 @@
import { EditorState } from "@codemirror/state";
import { describe, expect, it } from "vitest";
import { createEditorSearchQuery } from "@/lib/editor/editorSearchQuery";
import { describe, expect, it, vi } from "vitest";
import { collectEditorSearchMatches, createEditorSearchQuery, replaceEditorSearchMatches } from "@/lib/editor/editorSearchQuery";
function matchedText(search: string, useRegex: boolean): string[] {
const state = EditorState.create({
@@ -25,4 +25,17 @@ describe("editorSearchQuery", () => {
it("allows regular expression mode to match actual line breaks", () => {
expect(matchedText(String.raw`\n`, true)).toEqual(["\n"]);
});
it("collects every scoped match when replacement is uncapped", () => {
const state = EditorState.create({ doc: Array.from({ length: 1001 }, () => "x").join(" ") });
const query = createEditorSearchQuery({ search: "x", caseSensitive: true, useRegex: false });
const matches = collectEditorSearchMatches(query, state, 0, state.doc.length);
const dispatch = vi.fn();
expect(matches).toHaveLength(1001);
expect(collectEditorSearchMatches(query, state, 0, state.doc.length, 1000)).toHaveLength(1000);
expect(replaceEditorSearchMatches({ dispatch }, matches, () => "y")).toBe(true);
expect(dispatch).toHaveBeenCalledOnce();
expect(dispatch.mock.calls[0]?.[0].changes).toHaveLength(1001);
});
});
@@ -0,0 +1,49 @@
import { readFileSync } from "node:fs";
import { EditorSelection, EditorState } from "@codemirror/state";
import { describe, expect, it, vi } from "vitest";
import { blankLineDeletionChanges, replaceSelectedEditorText } from "@/lib/editor/queryEditorTextEdits";
const queryEditorSource = readFileSync(new URL("../../../components/editor/QueryEditor.vue", import.meta.url), "utf8");
function applyBlankLineDeletion(doc: string, from: number, to: number): string {
const state = EditorState.create({
doc,
selection: EditorSelection.range(from, to),
});
return state.update({ changes: blankLineDeletionChanges(state.doc, state.selection.main) }).state.doc.toString();
}
describe("replaceSelectedEditorText", () => {
it("refuses to dispatch changes for a read-only editor", () => {
const state = EditorState.create({
doc: "a,b",
selection: EditorSelection.range(0, 3),
extensions: EditorState.readOnly.of(true),
});
const dispatch = vi.fn();
expect(replaceSelectedEditorText({ state, dispatch }, "'a','b'")).toBe(false);
expect(dispatch).not.toHaveBeenCalled();
});
it("disables and guards delimited-list generation in QueryEditor", () => {
expect(queryEditorSource).toContain("disabled: props.readOnly || !canCopySelectedSql.value");
expect(queryEditorSource).toMatch(/function openDelimitedListDialog\(\) \{\s+if \(props\.readOnly\) return;/);
expect(queryEditorSource).toMatch(/function applyDelimitedListResult\(result: string\) \{[\s\S]*if \(!currentView \|\| props\.readOnly\) return;[\s\S]*replaceSelectedEditorText/);
});
});
describe("blankLineDeletionChanges", () => {
it("preserves one newline between adjacent non-empty SQL", () => {
expect(applyBlankLineDeletion("a\n\nb", 2, 3)).toBe("a\nb");
});
it.each([
["partial-line selection", "select a\n\nselect b", 3, 12, "select a\nselect b"],
["full-line selection", "a\n \nb", 2, 5, "a\nb"],
["document start", "\n\na", 0, 2, "a"],
["document end", "a\n\n", 2, 3, "a\n"],
])("handles %s with line-boundary edits", (_name, doc, from, to, expected) => {
expect(applyBlankLineDeletion(doc, from, to)).toBe(expected);
});
});