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(completion): let exact matches outrank pinned sequences

弈秋aecb62a4422c
diff --git a/apps/desktop/src/lib/__tests__/sql/sqlCompletion.snippet.spec.ts b/apps/desktop/src/lib/__tests__/sql/sqlCompletion.snippet.spec.ts
index 9fc700dc59..757d7bff34 100644
--- a/apps/desktop/src/lib/__tests__/sql/sqlCompletion.snippet.spec.ts
+++ b/apps/desktop/src/lib/__tests__/sql/sqlCompletion.snippet.spec.ts
@@ -306,3 +306,40 @@ describe("buildSnippetItems", () => {
expect(items).toEqual([]);
});
});
+
+describe("snippet ranking", () => {
+ const SEQUENCE_OBJECTS = [
+ { name: "SEQ_SEL_A", type: "sequence" as const },
+ { name: "SEQ_SEL_B", type: "sequence" as const },
+ ];
+
+ // Regression for #13115: sequences pin themselves to the top of the list while
+ // no columns are expected, so on an Oracle-family connection (Dameng here)
+ // typing the `sel` trigger listed every `SEL*` sequence above `select *` and
+ // Tab expanded a sequence instead of the snippet.
+ it("ranks an exactly matched snippet above sequences without a column context", () => {
+ const items = buildSqlCompletionItems("sel", 3, {
+ databaseType: "dameng",
+ tables: [],
+ columnsByTable: new Map(),
+ objects: SEQUENCE_OBJECTS,
+ });
+
+ expect(items[0]).toMatchObject({ type: "snippet", label: "select *", filterText: "sel" });
+ expect(items.filter((item) => item.type === "sequence").map((item) => item.label)).toEqual(["SEQ_SEL_A", "SEQ_SEL_B"]);
+ });
+
+ it("keeps sequences ahead of ordinary candidates when the prefix is only a partial match", () => {
+ const items = buildSqlCompletionItems("se", 2, {
+ databaseType: "dameng",
+ tables: [],
+ columnsByTable: new Map(),
+ objects: SEQUENCE_OBJECTS,
+ });
+
+ const firstSequence = items.findIndex((item) => item.type === "sequence");
+ const firstNonSequence = items.findIndex((item) => item.type !== "sequence");
+ expect(firstSequence).toBeGreaterThanOrEqual(0);
+ expect(firstSequence).toBeLessThan(firstNonSequence);
+ });
+});
diff --git a/apps/desktop/src/lib/sql/sqlCompletion.ts b/apps/desktop/src/lib/sql/sqlCompletion.ts
index bddfdf7754..bb7e6c9c72 100644
--- a/apps/desktop/src/lib/sql/sqlCompletion.ts
+++ b/apps/desktop/src/lib/sql/sqlCompletion.ts
@@ -6757,10 +6757,15 @@ function dedupeAndSort(items: SqlCompletionItem[], context?: SqlCompletionContex
function compareCompletionItems(left: SqlCompletionItem, right: SqlCompletionItem, context?: SqlCompletionContext): number {
const columnsExpected = !!context && context.suggestColumns && context.referencedTables.length > 0;
+ // An exact match on what the user typed has to win before the "surfaces first"
+ // rule below: sequences (and variables) pin themselves to the top of the list
+ // whenever no columns are expected, so on Oracle-family connections typing the
+ // `sel` snippet trigger listed every `SEL*` sequence above `select *` and Tab
+ // expanded a sequence instead of the snippet (#13115).
+ if (!left.exactMatch !== !right.exactMatch) return left.exactMatch ? -1 : 1;
const leftSurfacesFirst = left.type === "variable" || (!columnsExpected && left.type === "sequence");
const rightSurfacesFirst = right.type === "variable" || (!columnsExpected && right.type === "sequence");
if (leftSurfacesFirst !== rightSurfacesFirst) return leftSurfacesFirst ? -1 : 1;
- if (!left.exactMatch !== !right.exactMatch) return left.exactMatch ? -1 : 1;
const leftBonus = getHistoryBoost(left.label, left.type);
const rightBonus = getHistoryBoost(right.label, right.type);
return right.boost + rightBonus + getTypePriorityBoost(right.type, columnsExpected) - (left.boost + leftBonus + getTypePriorityBoost(left.type, columnsExpected));