fix(editor): stabilize batch column selection
This commit is contained in:
1 parent
f664918e95
commit
143be57d41
5 files changed
+110
-15
No files matched your search
@@ -1,5 +1,5 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { batchColumnSelectionColumnList, batchColumnSelectionReplaceTo, shouldResolveSqlColumnCompletion } from "@/lib/editor/batchColumnSelection";
|
||||
import { batchColumnSelectionColumnList, batchColumnSelectionInsertReplacement, batchColumnSelectionReplaceTo, isBatchColumnSelectionCompletionActive, shouldResolveSqlColumnCompletion } from "@/lib/editor/batchColumnSelection";
|
||||
|
||||
describe("batchColumnSelectionColumnList", () => {
|
||||
it("keeps a typed qualifier on every projection after the first", () => {
|
||||
@@ -25,6 +25,40 @@ describe("batchColumnSelectionReplaceTo", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("batchColumnSelectionInsertReplacement", () => {
|
||||
it("consumes whitespace before an existing closing parenthesis", () => {
|
||||
expect(
|
||||
batchColumnSelectionInsertReplacement({
|
||||
document: "INSERT INTO users (id )",
|
||||
to: "INSERT INTO users (id".length,
|
||||
columns: "id, name",
|
||||
valuesKeyword: "VALUES",
|
||||
valueCount: 2,
|
||||
}),
|
||||
).toEqual({ replaceTo: "INSERT INTO users (id )".length, insert: "id, name) VALUES (${1:value}, ${2:value})" });
|
||||
});
|
||||
|
||||
it("does not duplicate an existing VALUES clause", () => {
|
||||
expect(
|
||||
batchColumnSelectionInsertReplacement({
|
||||
document: "INSERT INTO users (id) VALUES (1)",
|
||||
to: "INSERT INTO users (id".length,
|
||||
columns: "id, name",
|
||||
valuesKeyword: "VALUES",
|
||||
valueCount: 2,
|
||||
}),
|
||||
).toEqual({ replaceTo: "INSERT INTO users (id)".length, insert: "id, name)" });
|
||||
});
|
||||
});
|
||||
|
||||
describe("isBatchColumnSelectionCompletionActive", () => {
|
||||
it("only accepts a currently active completion popup", () => {
|
||||
expect(isBatchColumnSelectionCompletionActive("active")).toBe(true);
|
||||
expect(isBatchColumnSelectionCompletionActive("pending")).toBe(false);
|
||||
expect(isBatchColumnSelectionCompletionActive(null)).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("shouldResolveSqlColumnCompletion", () => {
|
||||
it("loads fields after SELECT space when a FROM table is already known", () => {
|
||||
expect(shouldResolveSqlColumnCompletion({ suggestColumns: true, hasReferencedTables: true, prefix: "", typedActivation: false, selectListColumnContext: true })).toBe(true);
|
||||
|
||||
@@ -95,6 +95,7 @@ function createHarness(options: {
|
||||
}).outputText;
|
||||
const factory = new Function(
|
||||
"codeMirrorCompletionStatus",
|
||||
"isBatchColumnSelectionCompletionActive",
|
||||
"codeMirrorAcceptCompletion",
|
||||
"codeMirrorSelectedCompletionIndex",
|
||||
"codeMirrorSelectFirstCompletion",
|
||||
@@ -114,6 +115,7 @@ function createHarness(options: {
|
||||
);
|
||||
return factory(
|
||||
options.completionStatus,
|
||||
(status: "active" | "pending" | null) => status === "active",
|
||||
options.acceptCompletion ?? (() => false),
|
||||
options.selectedCompletionIndex ?? (() => 0),
|
||||
options.selectFirstCompletion ?? (() => false),
|
||||
@@ -164,6 +166,9 @@ afterEach(() => {
|
||||
});
|
||||
|
||||
describe("QueryEditor completion Tab keymap", () => {
|
||||
it("guards the batch INSERT snippet factory before applying", () => {
|
||||
expect(queryEditorSource).toContain('if (session.mode === "insert" && !codeMirrorSnippetCompletion)');
|
||||
});
|
||||
it("closes completion and suppresses its restart for the Enter newline", () => {
|
||||
const closeCompletion = vi.fn(() => true);
|
||||
let harness: TabHarness;
|
||||
@@ -197,6 +202,26 @@ describe("QueryEditor completion Tab keymap", () => {
|
||||
expect(harness.consumeSqlCompletionAutoStartSuppression()).toBe(false);
|
||||
});
|
||||
|
||||
it("does not apply a stale batch selection after the completion popup closes", () => {
|
||||
const applySelectedBatchColumnSelection = vi.fn(() => true);
|
||||
const insertNewlineKeepIndent = vi.fn(() => true);
|
||||
const harness = createHarness({ completionStatus: () => null, applySelectedBatchColumnSelection, insertNewlineKeepIndent, acceptCompletionShortcut: "Tab" });
|
||||
const view = createView();
|
||||
|
||||
expect(harness.handleEnter(view)).toBe(true);
|
||||
expect(harness.handleTab(view)).toBe(true);
|
||||
expect(applySelectedBatchColumnSelection).not.toHaveBeenCalled();
|
||||
expect(insertNewlineKeepIndent).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("applies a batch selection only while the completion popup is active", () => {
|
||||
const applySelectedBatchColumnSelection = vi.fn(() => true);
|
||||
const harness = createHarness({ completionStatus: () => "active", applySelectedBatchColumnSelection });
|
||||
|
||||
expect(harness.handleEnter(createView())).toBe(true);
|
||||
expect(applySelectedBatchColumnSelection).toHaveBeenCalledOnce();
|
||||
});
|
||||
|
||||
it("keeps CodeMirror prefix filtering enabled for SQL completion results", () => {
|
||||
const javascript = ts.transpileModule(extractFunction("buildCompletionResult"), {
|
||||
compilerOptions: { module: ts.ModuleKind.None, target: ts.ScriptTarget.ES2022 },
|
||||
|
||||
@@ -17,3 +17,18 @@ export function batchColumnSelectionReplaceTo(options: { to: number; mode: Batch
|
||||
const { to, mode, nextCharacter, replaceClosingQuote } = options;
|
||||
return replaceClosingQuote === nextCharacter || (mode === "insert" && nextCharacter === ")") ? to + 1 : to;
|
||||
}
|
||||
|
||||
export function batchColumnSelectionInsertReplacement(options: { document: string; to: number; columns: string; valuesKeyword: "values" | "VALUES"; valueCount: number }): { replaceTo: number; insert: string } {
|
||||
const suffix = options.document.slice(options.to);
|
||||
const closingParenthesis = suffix.match(/^\s*\)/);
|
||||
const replaceTo = closingParenthesis ? options.to + closingParenthesis[0].length : options.to;
|
||||
const hasExistingValues = /^\s*\)\s*VALUES\b/i.test(suffix);
|
||||
if (hasExistingValues) return { replaceTo, insert: `${options.columns})` };
|
||||
|
||||
const values = Array.from({ length: options.valueCount }, (_, index) => `\${${index + 1}:value}`).join(", ");
|
||||
return { replaceTo, insert: `${options.columns}) ${options.valuesKeyword} (${values})` };
|
||||
}
|
||||
|
||||
export function isBatchColumnSelectionCompletionActive(status: "active" | "pending" | null): boolean {
|
||||
return status === "active";
|
||||
}
|
||||
Reference in new issue
Block a user