Skip to content

armgit / dbx

Public Imported from GitHub
0

25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。

fix(editor): append completion space before newlines and statement boundaries (#12948)

Loc Nguyenebf8a78ec44a
diff --git a/apps/desktop/src/components/editor/__tests__/useQueryEditorCompletion.spec.ts b/apps/desktop/src/components/editor/__tests__/useQueryEditorCompletion.spec.ts
index dab92eec1d..b381223860 100644
--- a/apps/desktop/src/components/editor/__tests__/useQueryEditorCompletion.spec.ts
+++ b/apps/desktop/src/components/editor/__tests__/useQueryEditorCompletion.spec.ts
@@ -362,6 +362,47 @@ describe.each([false, true])("Snowflake namespace completion (semantic=%s)", (se
});
});
+describe("SQL completion trailing space insertion", () => {
+ beforeEach(() => vi.useRealTimers());
+
+ it.each([
+ ["at end of doc", "sele", 4, "SELECT "],
+ ["before a newline", "sele\n", 4, "SELECT \n"],
+ ["in the middle statement of a multi-statement doc", "SELECT 1;\nsele\nSELECT 2;", 14, "SELECT 1;\nSELECT \nSELECT 2;"],
+ ])("appends trailing space when insertSpaceAfterCompletion is enabled (%s)", async (_name, doc, cursor, expected) => {
+ const { provide, currentView, settings } = createHarness({ databaseType: "mysql", dialect: "mysql", modelValue: doc });
+ settings.editorSettings.insertSpaceAfterCompletion = true;
+ currentView.dispatch({ selection: { anchor: cursor } });
+ const result = await provide();
+ const option = result?.options.find((candidate) => candidate.label === "SELECT");
+ expect(option).toBeDefined();
+ (option!.apply as (view: EditorView, completion: unknown, from: number, to: number) => void)(currentView, option, result!.from, cursor);
+ expect(currentView.state.doc.toString()).toBe(expected);
+ });
+
+ it("does not append trailing space when insertSpaceAfterCompletion is disabled", async () => {
+ const { provide, currentView, settings } = createHarness({ databaseType: "mysql", dialect: "mysql", modelValue: "sele" });
+ settings.editorSettings.insertSpaceAfterCompletion = false;
+ currentView.dispatch({ selection: { anchor: 4 } });
+ const result = await provide();
+ const option = result?.options.find((candidate) => candidate.label === "SELECT");
+ expect(option).toBeDefined();
+ (option!.apply as (view: EditorView, completion: unknown, from: number, to: number) => void)(currentView, option, result!.from, 4);
+ expect(currentView.state.doc.toString()).toBe("SELECT");
+ });
+
+ it("does not append duplicate space when next character is already whitespace", async () => {
+ const { provide, currentView, settings } = createHarness({ databaseType: "mysql", dialect: "mysql", modelValue: "sele FROM users;" });
+ settings.editorSettings.insertSpaceAfterCompletion = true;
+ currentView.dispatch({ selection: { anchor: 4 } });
+ const result = await provide();
+ const option = result?.options.find((candidate) => candidate.label === "SELECT");
+ expect(option).toBeDefined();
+ (option!.apply as (view: EditorView, completion: unknown, from: number, to: number) => void)(currentView, option, result!.from, 4);
+ expect(currentView.state.doc.toString()).toBe("SELECT FROM users;");
+ });
+});
+
describe("QueryEditor completion provider ownership", () => {
it.each(["cursor", "document", "view", "composition", "mode"])("rejects asynchronous analysis after a %s change", async (change) => {
let resolve!: (result: SqlCompletionAnalysisResult) => void;
diff --git a/apps/desktop/src/lib/__tests__/editor/sqlCompletionInsertion.spec.ts b/apps/desktop/src/lib/__tests__/editor/sqlCompletionInsertion.spec.ts
index 36f30f02e8..883a6df768 100644
--- a/apps/desktop/src/lib/__tests__/editor/sqlCompletionInsertion.spec.ts
+++ b/apps/desktop/src/lib/__tests__/editor/sqlCompletionInsertion.spec.ts
@@ -15,6 +15,8 @@ describe("SQL completion insertion", () => {
expect(appendSqlCompletionSpace("tt", { enabled: true, itemType: "variable" })).toBe("tt");
expect(appendSqlCompletionSpace("orders", { enabled: true, itemType: "table", nextCharacter: ")" })).toBe("orders");
expect(appendSqlCompletionSpace("orders", { enabled: true, itemType: "table", nextCharacter: "," })).toBe("orders");
+ expect(appendSqlCompletionSpace("SELECT", { enabled: true, itemType: "keyword", nextCharacter: " " })).toBe("SELECT");
+ expect(appendSqlCompletionSpace("SELECT", { enabled: true, itemType: "keyword", nextCharacter: "\t" })).toBe("SELECT");
});
it("keeps the space when a statement terminator follows the cursor", () => {
@@ -24,6 +26,13 @@ describe("SQL completion insertion", () => {
expect(appendSqlCompletionSpace("SALARY", { enabled: true, itemType: "column", nextCharacter: ";" })).toBe("SALARY ");
});
+ it("appends a space at end of doc, before a newline, and in multi-statement documents", () => {
+ expect(appendSqlCompletionSpace("SELECT", { enabled: true, itemType: "keyword", nextCharacter: "" })).toBe("SELECT ");
+ expect(appendSqlCompletionSpace("SELECT", { enabled: true, itemType: "keyword", nextCharacter: "\n" })).toBe("SELECT ");
+ expect(appendSqlCompletionSpace("SELECT", { enabled: true, itemType: "keyword", nextCharacter: "\r" })).toBe("SELECT ");
+ expect(appendSqlCompletionSpace("orders", { enabled: true, itemType: "table", nextCharacter: "\n" })).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}");
diff --git a/apps/desktop/src/lib/editor/sqlCompletionInsertion.ts b/apps/desktop/src/lib/editor/sqlCompletionInsertion.ts
index cfaa1a09bc..0287637123 100644
--- a/apps/desktop/src/lib/editor/sqlCompletionInsertion.ts
+++ b/apps/desktop/src/lib/editor/sqlCompletionInsertion.ts
@@ -3,6 +3,9 @@ export type SqlCompletionItemType = "keyword" | "table" | "column" | "snippet" |
// `;` is deliberately absent: a statement terminator is never glued to the token
// in front of it, and accepting `BETWEEN` before an existing `;` used to insert
// `BETWEEN;`, leaving the user no way to keep typing the operand.
+//
+// Newlines and statement boundaries should also not suppress the auto-space;
+// only an existing space/tab or punctuation should prevent it.
const COMPLETION_SPACE_BLOCKING_CHARACTERS = new Set([",", ":", ")", "]", "}", "'", '"']);
export function appendSqlCompletionSpace(insertText: string, options: { enabled: boolean; itemType: SqlCompletionItemType; nextCharacter?: string }): string {
@@ -10,7 +13,7 @@ export function appendSqlCompletionSpace(insertText: string, options: { enabled:
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;
+ if (/^[ \t]/.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} `;