feat(editor): batch-select and insert SQL completion columns
This commit is contained in:
15 files changed
+440
-14
No files matched your search
@@ -0,0 +1,36 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { batchColumnSelectionColumnList, batchColumnSelectionReplaceTo, shouldResolveSqlColumnCompletion } from "@/lib/editor/batchColumnSelection";
|
||||
|
||||
describe("batchColumnSelectionColumnList", () => {
|
||||
it("keeps a typed qualifier on every projection after the first", () => {
|
||||
expect(batchColumnSelectionColumnList(["method", "path", "remark"], "select", "ap")).toBe("method, ap.path, ap.remark");
|
||||
});
|
||||
|
||||
it("does not add a qualifier to INSERT target columns", () => {
|
||||
expect(batchColumnSelectionColumnList(["id", "name"], "insert", "users")).toBe("id, name");
|
||||
});
|
||||
});
|
||||
|
||||
describe("batchColumnSelectionReplaceTo", () => {
|
||||
it("consumes the auto-inserted INSERT closing parenthesis", () => {
|
||||
expect(batchColumnSelectionReplaceTo({ to: 20, mode: "insert", nextCharacter: ")" })).toBe(21);
|
||||
});
|
||||
|
||||
it("keeps the replacement boundary when INSERT has no closing parenthesis", () => {
|
||||
expect(batchColumnSelectionReplaceTo({ to: 20, mode: "insert", nextCharacter: "" })).toBe(20);
|
||||
});
|
||||
|
||||
it("continues consuming a matching closing identifier quote", () => {
|
||||
expect(batchColumnSelectionReplaceTo({ to: 20, mode: "select", nextCharacter: '"', replaceClosingQuote: '"' })).toBe(21);
|
||||
});
|
||||
});
|
||||
|
||||
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);
|
||||
});
|
||||
|
||||
it("keeps empty non-SELECT column contexts from fetching metadata", () => {
|
||||
expect(shouldResolveSqlColumnCompletion({ suggestColumns: true, hasReferencedTables: true, prefix: "", typedActivation: false, selectListColumnContext: false })).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -60,6 +60,7 @@ function createHarness(options: {
|
||||
acceptCompletion?: (view: MockView) => boolean;
|
||||
selectedCompletionIndex?: (state: MockState) => number | null;
|
||||
selectFirstCompletion?: (view: MockView) => boolean;
|
||||
applySelectedBatchColumnSelection?: (view: MockView) => boolean;
|
||||
closeCompletion?: (view: MockView) => boolean;
|
||||
insertNewlineKeepIndent?: (view: MockView) => boolean;
|
||||
nextSnippetField?: (view: MockView) => boolean;
|
||||
@@ -99,6 +100,7 @@ function createHarness(options: {
|
||||
"codeMirrorSelectFirstCompletion",
|
||||
"acceptSelectedCompletionWithRetry",
|
||||
"acceptSelectedOrFirstCompletion",
|
||||
"applySelectedBatchColumnSelection",
|
||||
"codeMirrorCloseCompletion",
|
||||
"codeMirrorInsertNewlineKeepIndent",
|
||||
"insertQueryEditorNewline",
|
||||
@@ -117,6 +119,7 @@ function createHarness(options: {
|
||||
options.selectFirstCompletion ?? (() => false),
|
||||
acceptSelectedCompletionWithRetry,
|
||||
acceptSelectedOrFirstCompletion,
|
||||
options.applySelectedBatchColumnSelection ?? (() => false),
|
||||
options.closeCompletion ?? (() => false),
|
||||
options.insertNewlineKeepIndent ?? (() => false),
|
||||
(view: MockView, fallback: ((view: MockView) => boolean) | null | undefined) => fallback?.(view) ?? false,
|
||||
@@ -207,7 +210,7 @@ describe("QueryEditor completion Tab keymap", () => {
|
||||
});
|
||||
|
||||
it("keeps ASCII single-character filtering while allowing Han pinyin matches", () => {
|
||||
const source = [extractFunction("completionItemsForBypassedFilter"), extractFunction("shouldBypassCompletionFilter"), extractFunction("buildCompletionResult")].join("\n");
|
||||
const source = [extractFunction("isBatchColumnSelectionAction"), extractFunction("completionItemsForBypassedFilter"), extractFunction("shouldBypassCompletionFilter"), extractFunction("buildCompletionResult")].join("\n");
|
||||
const javascript = ts.transpileModule(source, {
|
||||
compilerOptions: { module: ts.ModuleKind.None, target: ts.ScriptTarget.ES2022 },
|
||||
}).outputText;
|
||||
|
||||
@@ -304,6 +304,34 @@ describe("semantic SQL completion candidates", () => {
|
||||
expect(columns.find((item) => item.label === "tl.villageId")).toMatchObject({ filterText: "villageId", apply: "tl.villageId" });
|
||||
});
|
||||
|
||||
it("marks SELECT projection columns as batch-selectable", () => {
|
||||
const columnsByTable = new Map<string, SqlCompletionColumn[]>([["users", ["id", "name"].map((name) => ({ name, table: "users" }))]]);
|
||||
|
||||
const { items } = semanticCompletion("SELECT | FROM users", { columnsByTable });
|
||||
|
||||
expect(items.filter((item) => item.type === "column")).toEqual(expect.arrayContaining([expect.objectContaining({ label: "id", apply: "id", batchSelectionMode: "select" }), expect.objectContaining({ label: "name", apply: "name", batchSelectionMode: "select" })]));
|
||||
});
|
||||
|
||||
it("retains a typed table alias for batch-selected projection columns", () => {
|
||||
const columnsByTable = new Map<string, SqlCompletionColumn[]>([["users", ["id", "name"].map((name) => ({ name, table: "users" }))]]);
|
||||
|
||||
const { items } = semanticCompletion("SELECT u.| FROM users u", { columnsByTable });
|
||||
|
||||
expect(items.filter((item) => item.type === "column")).toEqual(
|
||||
expect.arrayContaining([expect.objectContaining({ label: "id", apply: "id", batchSelectionMode: "select", batchSelectionQualifier: "u" }), expect.objectContaining({ label: "name", apply: "name", batchSelectionMode: "select", batchSelectionQualifier: "u" })]),
|
||||
);
|
||||
});
|
||||
|
||||
it("retains a typed table name when the referenced table also has an alias", () => {
|
||||
const columnsByTable = new Map<string, SqlCompletionColumn[]>([["users", ["id", "name"].map((name) => ({ name, table: "users" }))]]);
|
||||
|
||||
const { items } = semanticCompletion("SELECT users.| FROM users u", { columnsByTable });
|
||||
|
||||
expect(items.filter((item) => item.type === "column")).toEqual(
|
||||
expect.arrayContaining([expect.objectContaining({ label: "id", apply: "id", batchSelectionMode: "select", batchSelectionQualifier: "users" }), expect.objectContaining({ label: "name", apply: "name", batchSelectionMode: "select", batchSelectionQualifier: "users" })]),
|
||||
);
|
||||
});
|
||||
|
||||
it("completes columns for aliases in comma-separated table lists", () => {
|
||||
const columnsByTable = new Map<string, SqlCompletionColumn[]>([
|
||||
["table_a", ["id", "name"].map((name) => ({ name, table: "table_a" }))],
|
||||
@@ -472,6 +500,14 @@ WHERE a.id = b.fk_kpi_set_score_id`,
|
||||
expect(allColumns?.detail).toBe("3 columns: id, name, email) VALUES (value, value, value)");
|
||||
});
|
||||
|
||||
it("marks INSERT target columns as batch-selectable", () => {
|
||||
const columnsByTable = new Map<string, SqlCompletionColumn[]>([["users", ["id", "name"].map((name) => ({ name, table: "users" }))]]);
|
||||
|
||||
const { items } = semanticCompletion("INSERT INTO users (|", { columnsByTable });
|
||||
|
||||
expect(items.filter((item) => item.type === "column")).toEqual(expect.arrayContaining([expect.objectContaining({ label: "id", apply: "id", batchSelectionMode: "insert" }), expect.objectContaining({ label: "name", apply: "name", batchSelectionMode: "insert" })]));
|
||||
});
|
||||
|
||||
it("uses the configured keyword case for INSERT all-column snippets", () => {
|
||||
const columnsByTable = new Map<string, SqlCompletionColumn[]>([["users", ["id", "name"].map((name) => ({ name, table: "users" }))]]);
|
||||
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
export type BatchColumnSelectionMode = "select" | "insert";
|
||||
|
||||
export function batchColumnSelectionColumnList(candidates: string[], mode: BatchColumnSelectionMode, qualifier?: string): string {
|
||||
return candidates.map((candidate, index) => (mode === "select" && qualifier && index > 0 ? `${qualifier}.${candidate}` : candidate)).join(", ");
|
||||
}
|
||||
|
||||
export function shouldResolveSqlColumnCompletion(options: { suggestColumns: boolean; hasReferencedTables: boolean; prefix: string; typedActivation: boolean; selectListColumnContext: boolean }): boolean {
|
||||
return options.suggestColumns && options.hasReferencedTables && (options.prefix.length > 0 || options.typedActivation || options.selectListColumnContext);
|
||||
}
|
||||
|
||||
/**
|
||||
* The INSERT batch action writes its own closing parenthesis before VALUES.
|
||||
* Consume an existing one (normally inserted by CodeMirror's auto-close
|
||||
* brackets extension) so the resulting statement has exactly one `)`.
|
||||
*/
|
||||
export function batchColumnSelectionReplaceTo(options: { to: number; mode: BatchColumnSelectionMode; nextCharacter: string; replaceClosingQuote?: string }): number {
|
||||
const { to, mode, nextCharacter, replaceClosingQuote } = options;
|
||||
return replaceClosingQuote === nextCharacter || (mode === "insert" && nextCharacter === ")") ? to + 1 : to;
|
||||
}
|
||||
@@ -1291,6 +1291,10 @@ export interface SqlCompletionItem {
|
||||
boost: number;
|
||||
exactMatch?: boolean;
|
||||
dedupeKey?: string;
|
||||
/** Enables the query editor's checkbox-based batch insertion for this column. */
|
||||
batchSelectionMode?: "select" | "insert";
|
||||
/** Qualifier to prepend to every batch-selected column after the first one. */
|
||||
batchSelectionQualifier?: string;
|
||||
}
|
||||
|
||||
export function shouldChainSqlCompletionAfterAccept(item: { type?: string; apply?: string }): boolean {
|
||||
@@ -4505,6 +4509,8 @@ function buildColumnItems(context: SqlCompletionContext, columnsByTable: Map<str
|
||||
.sort((left, right) => right.boost - left.boost || left.index - right.index)
|
||||
.slice(0, context.insertTable || !context.prefix ? 50 : context.qualifier ? 30 : 20);
|
||||
|
||||
const batchSelectionMode = context.insertTable ? "insert" : context.statementKind === "select" && context.selectListColumnContext ? "select" : undefined;
|
||||
|
||||
return rankedColumns.map(({ column, boost }) => {
|
||||
return {
|
||||
label: column.displayLabel,
|
||||
@@ -4514,6 +4520,11 @@ function buildColumnItems(context: SqlCompletionContext, columnsByTable: Map<str
|
||||
info: buildColumnInfo(column),
|
||||
apply: buildColumnApply(column, context, dialect),
|
||||
boost,
|
||||
batchSelectionMode,
|
||||
// The first selected column keeps the qualifier already present in the
|
||||
// document. Subsequent columns must use that same user-typed qualifier,
|
||||
// not a referenced-table alias which may be different from it.
|
||||
batchSelectionQualifier: batchSelectionMode === "select" && context.qualifier ? (context.qualifierParts ?? context.qualifier.split(".").filter(Boolean)).map((part) => quoteCompletionApplyIdentifier(part, dialect)).join(".") : undefined,
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user