feat(completion): use all table name initials for aliases

This commit is contained in:
t8y2
2026-07-21 16:38:53 +08:00
parent eb91f08369
commit bd3c647370
2 changed files with 25 additions and 2 deletions
@@ -114,6 +114,30 @@ describe("sqlCompletion table targets", () => {
});
describe("sqlCompletion table aliases", () => {
it("uses initials from all words for generated aliases", () => {
const sql = "SELECT * FROM mat";
const items = buildSqlCompletionItems(sql, sql.length, {
tables: [{ name: "materials_order_item", type: "table" }],
columnsByTable: new Map(),
autoAliasTables: true,
});
const table = items.find((item) => item.label === "materials_order_item" && item.type === "table");
expect(table?.apply).toBe("materials_order_item AS moi");
});
it("uses every word initial for longer multi-word names", () => {
const sql = "SELECT * FROM sup";
const items = buildSqlCompletionItems(sql, sql.length, {
tables: [{ name: "super_long_customer_order_history_archive_snapshot_daily_replica", type: "table" }],
columnsByTable: new Map(),
autoAliasTables: true,
});
const table = items.find((item) => item.label === "super_long_customer_order_history_archive_snapshot_daily_replica" && item.type === "table");
expect(table?.apply).toBe("super_long_customer_order_history_archive_snapshot_daily_replica AS slcohasdr");
});
it("applies generated aliases to table completions when enabled", () => {
const sql = "SELECT * FROM ord";
const items = buildSqlCompletionItems(sql, sql.length, {
+1 -2
View File
@@ -3403,8 +3403,7 @@ function buildAliasCandidates(tableName: string): string[] {
if (parts.length > 1) {
const initials = parts.map((part) => part[0]).join("");
if (initials.length >= 2) candidates.push(initials.slice(0, 2));
if (initials.length >= 3) candidates.push(initials.slice(0, 3));
if (initials.length >= 2) candidates.push(initials);
candidates.push(parts[0].slice(0, 2), parts[0].slice(0, 3));
} else {
const name = parts[0] ?? tableName.toLowerCase().replace(/[^a-z0-9]/g, "");