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。

feat(completion): rank table acronym matches

t8y26570e88e4a87
diff --git a/apps/desktop/src/lib/sqlCompletion.ts b/apps/desktop/src/lib/sqlCompletion.ts
index fa2cb23ad1..d61d7e2772 100644
--- a/apps/desktop/src/lib/sqlCompletion.ts
+++ b/apps/desktop/src/lib/sqlCompletion.ts
@@ -2805,6 +2805,7 @@ function matchesPrefix(candidate: string, prefix: string): boolean {
*
* Scoring tiers:
* Exact match: 3000 - len
+ * Initials match: 2400 + exactInitialsBonus - len
* Prefix match: 2000 - len
* Tight fuzzy: 1500 - gapPenalty + earlyMatchBonus - len (gaps < prefix length)
* Loose fuzzy: 500 + partialEarlyBonus - gapPenalty - len (gaps >= prefix length)
@@ -2821,6 +2822,12 @@ function computeMatchScore(candidate: string, prefix: string): number {
// Prefix match
if (c.startsWith(p)) return 2000 - c.length;
+ const initials = identifierInitials(c);
+ if (initials && initials.startsWith(p)) {
+ const exactInitialsBonus = initials === p ? 400 : 0;
+ return 2400 + exactInitialsBonus - c.length;
+ }
+
// Fuzzy match: chars must appear in order (allows gaps for typos/abbrevs)
let ci = 0;
let totalGap = 0;
@@ -2849,6 +2856,14 @@ function computeMatchScore(candidate: string, prefix: string): number {
return 1200 + earlyMatchBonus - gapPenalty - c.length;
}
+function identifierInitials(candidate: string): string {
+ return candidate
+ .split(/[^a-z0-9]+/)
+ .filter(Boolean)
+ .map((part) => part[0])
+ .join("");
+}
+
function computeBoost(candidate: string, prefix: string): number {
return computeMatchScore(candidate, prefix);
}
diff --git a/packages/app-tests/sqlCompletion.test.ts b/packages/app-tests/sqlCompletion.test.ts
index a019210a41..91b7531c5f 100644
--- a/packages/app-tests/sqlCompletion.test.ts
+++ b/packages/app-tests/sqlCompletion.test.ts
@@ -1074,6 +1074,21 @@ test("suggests table alias after FROM table", () => {
assert.ok(aliasItem!.apply!.includes("AS"), "alias apply should include AS");
});
+test("prioritizes table acronym matches above alias snippets", () => {
+ const acronymTables: SqlCompletionTable[] = [...tables, { name: "user_basic_info", schema: "public", type: "table" }];
+ const items = buildSqlCompletionItems("select * from ubi", "select * from ubi".length, {
+ tables: acronymTables,
+ columnsByTable,
+ });
+
+ assert.equal(items[0]?.label, "user_basic_info");
+ assert.equal(items[0]?.type, "table");
+ assert.ok(
+ items.some((item) => item.type === "snippet" && item.apply === "AS ubi "),
+ "alias snippet should remain available",
+ );
+});
+
// --- CASE snippet ---
test("suggests CASE WHEN snippet", () => {