fix(editor): improve SQL completion insertion

This commit is contained in:
t8y2 committed 2026-07-26 13:10:31 +08:00
1 parent b5435e7795
commit 931cff48c0
11 files changed
+101 -2

No files matched your search

@@ -89,6 +89,18 @@ afterEach(() => {
});
describe("QueryEditor completion Tab keymap", () => {
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 },
}).outputText;
const buildCompletionResult = new Function("completionOptionForItem", `${javascript}\nreturn buildCompletionResult;`)(() => ({ label: "created_at" })) as (items: Array<{ label: string }>, from: number) => { filter?: boolean; options: Array<{ label: string }>; from: number } | null;
const result = buildCompletionResult([{ label: "created_at" }], 0);
expect(result).not.toBeNull();
expect(result).not.toHaveProperty("filter");
});
it("keeps normal Tab indentation when completion is inactive", () => {
const harness = createHarness({ completionStatus: () => null });
const view = createView();
@@ -0,0 +1,24 @@
import { describe, expect, it } from "vitest";
import { appendSqlCompletionSpace } from "@/lib/editor/sqlCompletionInsertion";
describe("SQL completion insertion", () => {
it("appends a space to ordinary completions by default", () => {
expect(appendSqlCompletionSpace("SELECT", { enabled: true, itemType: "keyword" })).toBe("SELECT ");
expect(appendSqlCompletionSpace("orders AS o", { enabled: true, itemType: "table" })).toBe("orders AS o ");
});
it("does not add duplicate or invalid spaces", () => {
expect(appendSqlCompletionSpace("SELECT ", { enabled: true, itemType: "keyword" })).toBe("SELECT ");
expect(appendSqlCompletionSpace("public.", { enabled: true, itemType: "schema" })).toBe("public.");
expect(appendSqlCompletionSpace("name", { enabled: true, itemType: "property" })).toBe("name");
expect(appendSqlCompletionSpace("key", { enabled: true, itemType: "text" })).toBe("key");
expect(appendSqlCompletionSpace("orders", { enabled: true, itemType: "table", nextCharacter: ")" })).toBe("orders");
expect(appendSqlCompletionSpace("orders", { enabled: true, itemType: "table", nextCharacter: "," })).toBe("orders");
});
it("honors the setting and leaves snippet-like completions unchanged", () => {
expect(appendSqlCompletionSpace("SELECT", { enabled: false, itemType: "keyword" })).toBe("SELECT");
expect(appendSqlCompletionSpace("CASE ${value}", { enabled: true, itemType: "snippet" })).toBe("CASE ${value}");
expect(appendSqlCompletionSpace("count()", { enabled: true, itemType: "function" })).toBe("count()");
});
});
@@ -0,0 +1,14 @@
export type SqlCompletionItemType = "keyword" | "table" | "column" | "snippet" | "function" | "schema" | "property" | "text";
const COMPLETION_SPACE_BLOCKING_CHARACTERS = new Set([",", ";", ":", ")", "]", "}", "'", '"']);
export function appendSqlCompletionSpace(insertText: string, options: { enabled: boolean; itemType: SqlCompletionItemType; nextCharacter?: string }): string {
if (!options.enabled || options.itemType === "property" || options.itemType === "text" || options.itemType === "schema" || options.itemType === "snippet" || options.itemType === "function") return insertText;
if (!insertText || /\s$/.test(insertText) || insertText.endsWith(".")) return insertText;
const nextCharacter = options.nextCharacter ?? "";
if (/\s/.test(nextCharacter) || COMPLETION_SPACE_BLOCKING_CHARACTERS.has(nextCharacter)) return insertText;
// Avoid producing `table AS t,` or `column)` when the cursor is before SQL punctuation.
return `${insertText} `;
}
@@ -16,6 +16,7 @@ export const EDITOR_SETTINGS_DRAFT_KEYS = [
"showCurrentStatementFrame",
"showInsertValueHints",
"autoAliasTables",
"insertSpaceAfterCompletion",
"wordWrap",
"vimModeEnabled",
"autoCloseBrackets",