From 971a95aa80aedec0290e386fdaee0663e3f068ea Mon Sep 17 00:00:00 2001 From: lxk955 <164191365+lxk955@users.noreply.github.com> Date: Sun, 4 Oct 2026 22:21:46 +0800 Subject: [PATCH] feat(sql): offer keyword completion on full word match for case conversion --- .../sql/sqlCompletion.context.spec.ts | 141 ++++++++++++++++++ apps/desktop/src/lib/sql/semantic/model.ts | 11 +- apps/desktop/src/lib/sql/sqlCompletion.ts | 11 +- 3 files changed, 155 insertions(+), 8 deletions(-) diff --git a/apps/desktop/src/lib/__tests__/sql/sqlCompletion.context.spec.ts b/apps/desktop/src/lib/__tests__/sql/sqlCompletion.context.spec.ts index 0990abf0f2..2538c11eb5 100644 --- a/apps/desktop/src/lib/__tests__/sql/sqlCompletion.context.spec.ts +++ b/apps/desktop/src/lib/__tests__/sql/sqlCompletion.context.spec.ts @@ -90,6 +90,147 @@ describe("sqlCompletion keyword snippets", () => { expect(items).toEqual(expect.arrayContaining([expect.objectContaining({ label: "select *", type: "snippet" }), expect.objectContaining({ label: "SELECT", type: "keyword" })])); }); + it("offers uppercase keyword completion when typing full lowercase keywords for case conversion (#8325)", () => { + const testCases = [ + { sql: "select", word: "SELECT" }, + { sql: "SELECT id from", word: "FROM" }, + { sql: "SELECT * FROM users where", word: "WHERE" }, + { sql: "SELECT * FROM users WHERE id = 1 and", word: "AND" }, + { sql: "SELECT * FROM users WHERE id = 1 or", word: "OR" }, + { sql: "SELECT * FROM users WHERE id is", word: "IS" }, + { sql: "SELECT * FROM users WHERE id is null", word: "NULL" }, + { sql: "SELECT * FROM users WHERE id not", word: "NOT" }, + { sql: "SELECT * FROM users WHERE id between", word: "BETWEEN" }, + { sql: "SELECT * FROM users WHERE name like", word: "LIKE" }, + { sql: "SELECT * FROM users WHERE id in", word: "IN" }, + { sql: "SELECT * FROM users order", word: "ORDER" }, + { sql: "SELECT * FROM users ORDER by", word: "BY" }, + { sql: "SELECT * FROM users group", word: "GROUP" }, + { sql: "SELECT * FROM users GROUP by", word: "BY" }, + { sql: "SELECT * FROM users GROUP BY id having", word: "HAVING" }, + { sql: "SELECT * FROM users limit", word: "LIMIT" }, + { sql: "SELECT * FROM users LIMIT 10 offset", word: "OFFSET" }, + { sql: "SELECT * FROM users join", word: "JOIN" }, + { sql: "SELECT * FROM users inner", word: "INNER" }, + { sql: "SELECT * FROM users left", word: "LEFT" }, + { sql: "SELECT * FROM users right", word: "RIGHT" }, + { sql: "SELECT * FROM users full", word: "FULL" }, + { sql: "SELECT * FROM users cross", word: "CROSS" }, + { sql: "SELECT * FROM users JOIN orders on", word: "ON" }, + { sql: "UPDATE users set", word: "SET" }, + { sql: "INSERT into", word: "INTO" }, + { sql: "INSERT INTO users values", word: "VALUES" }, + { sql: "SELECT distinct", word: "DISTINCT" }, + { sql: "SELECT id as", word: "AS" }, + { sql: "SELECT CASE when", word: "WHEN" }, + { sql: "SELECT CASE WHEN 1 then", word: "THEN" }, + { sql: "SELECT CASE WHEN 1 THEN 2 else", word: "ELSE" }, + { sql: "SELECT CASE WHEN 1 THEN 2 ELSE 3 end", word: "END" }, + { sql: "CREATE table", word: "TABLE" }, + { sql: "DROP table", word: "TABLE" }, + { sql: "ALTER table", word: "TABLE" }, + ]; + + for (const { sql, word } of testCases) { + const legacy = getSqlCompletionContext(sql, sql.length); + const semanticContext = sqlCompletionContextFromSemantic(buildSqlSemanticModel(sql, sql.length), legacy); + const items = buildSqlCompletionItemsFromContext(semanticContext, { + tables: [{ name: "users" }, { name: "orders" }], + columnsByTable: new Map([ + [ + "users", + [ + { name: "id", table: "users" }, + { name: "name", table: "users" }, + ], + ], + [ + "orders", + [ + { name: "id", table: "orders" }, + { name: "user_id", table: "orders" }, + ], + ], + ]), + keywordCase: "upper", + }); + + const keywordItem = items.find((item) => item.label === word && item.type === "keyword"); + expect(keywordItem, `Expected keyword ${word} in completion for "${sql}"`).toBeDefined(); + expect(keywordItem?.exactMatch, `Expected exactMatch flag for ${word}`).toBe(true); + expect( + items.slice(0, 2).map((item) => item.label), + `Expected ${word} in top suggestions for "${sql}"`, + ).toContain(word); + } + }); + + it("offers lowercase keyword completion when typing full uppercase keywords with keywordCase 'lower' (#8325)", () => { + const sql = "SELECT * FROM users WHERE"; + const legacy = getSqlCompletionContext(sql, sql.length); + const semanticContext = sqlCompletionContextFromSemantic(buildSqlSemanticModel(sql, sql.length), legacy); + const items = buildSqlCompletionItemsFromContext(semanticContext, { + tables: [{ name: "users" }], + columnsByTable: new Map([ + [ + "users", + [ + { name: "id", table: "users" }, + { name: "name", table: "users" }, + ], + ], + ]), + keywordCase: "lower", + }); + + const whereItem = items.find((item) => item.label === "where" && item.type === "keyword"); + expect(whereItem).toBeDefined(); + expect(whereItem?.exactMatch).toBe(true); + expect(items[0]?.label).toBe("where"); + }); + + it("keeps multi-word keywords available alongside individual keywords (#8325)", () => { + const orderSql = "SELECT * FROM users order"; + const orderLegacy = getSqlCompletionContext(orderSql, orderSql.length); + const orderContext = sqlCompletionContextFromSemantic(buildSqlSemanticModel(orderSql, orderSql.length), orderLegacy); + const orderItems = buildSqlCompletionItemsFromContext(orderContext, { + tables: [{ name: "users" }], + columnsByTable: new Map([ + [ + "users", + [ + { name: "id", table: "users" }, + { name: "name", table: "users" }, + ], + ], + ]), + keywordCase: "upper", + }); + + expect(orderItems.some((item) => item.label === "ORDER" && item.type === "keyword")).toBe(true); + expect(orderItems.some((item) => item.label === "ORDER BY" && item.type === "keyword")).toBe(true); + + const groupSql = "SELECT * FROM users group"; + const groupLegacy = getSqlCompletionContext(groupSql, groupSql.length); + const groupContext = sqlCompletionContextFromSemantic(buildSqlSemanticModel(groupSql, groupSql.length), groupLegacy); + const groupItems = buildSqlCompletionItemsFromContext(groupContext, { + tables: [{ name: "users" }], + columnsByTable: new Map([ + [ + "users", + [ + { name: "id", table: "users" }, + { name: "name", table: "users" }, + ], + ], + ]), + keywordCase: "upper", + }); + + expect(groupItems.some((item) => item.label === "GROUP" && item.type === "keyword")).toBe(true); + expect(groupItems.some((item) => item.label === "GROUP BY" && item.type === "keyword")).toBe(true); + }); + it("offers DuckDB-specific query and statement keywords", () => { const expectedKeywords = ["QUALIFY", "SUMMARIZE", "PIVOT", "UNPIVOT", "ASOF", "POSITIONAL", "FROM"]; diff --git a/apps/desktop/src/lib/sql/semantic/model.ts b/apps/desktop/src/lib/sql/semantic/model.ts index 8d4b207e6a..85cca23b8b 100644 --- a/apps/desktop/src/lib/sql/semantic/model.ts +++ b/apps/desktop/src/lib/sql/semantic/model.ts @@ -1026,20 +1026,17 @@ function buildCursorIntent(tokens: readonly SqlSemanticToken[], cursor: number, return { kind: "alias_column", prefix: trailing.prefix, replacementRange: trailing.replacementRange, qualifierParts: trailing.qualifierParts, targetSourceId: targetSource.id, expectedObjectKinds: ["column"], confidence: "high" }; } - // A table introducer only introduces the slot that follows it, so both checks read - // the word before the identifier being completed (wordBeforeTrailing) or before its - // whole qualified name (wordBeforeReplacement). `previous` must not be used here: it - // is the word at the cursor, so typing `update`/`from`/`join` in full made the - // keyword introduce its own slot and dropped its own completion (issue #10415). if (TABLE_INTRODUCERS.has(wordBeforeReplacement) || TABLE_INTRODUCERS.has(wordBeforeTrailing) || tableListContinuation) { return { kind: "table", prefix: trailing.prefix, replacementRange: trailing.replacementRange, qualifierParts: trailing.qualifierParts, expectedObjectKinds: ["table", "view"], confidence: "high" }; } - if (previous === "call" || previous === "exec" || previous === "execute") { + const routineIntroducer = wordBeforeTrailing || wordBeforeReplacement; + if (routineIntroducer === "call" || routineIntroducer === "exec" || routineIntroducer === "execute") { return { kind: "routine", prefix: trailing.prefix, replacementRange: trailing.replacementRange, qualifierParts: trailing.qualifierParts, expectedObjectKinds: ["routine", "procedure", "function"], confidence: "high" }; } - if (previous === "set") { + const updateColumnIntroducer = wordBeforeTrailing || wordBeforeReplacement; + if (updateColumnIntroducer === "set") { return { kind: "update_column", prefix: trailing.prefix, diff --git a/apps/desktop/src/lib/sql/sqlCompletion.ts b/apps/desktop/src/lib/sql/sqlCompletion.ts index 70d9fadeca..f59c9d2003 100644 --- a/apps/desktop/src/lib/sql/sqlCompletion.ts +++ b/apps/desktop/src/lib/sql/sqlCompletion.ts @@ -42,6 +42,9 @@ const SQL_KEYWORDS = [ "INNER", "OUTER", "ON", + "GROUP", + "ORDER", + "BY", "GROUP BY", "ORDER BY", "ASC", @@ -301,6 +304,9 @@ const COMMON_SQL_KEYWORDS = [ "INNER", "OUTER", "ON", + "GROUP", + "ORDER", + "BY", "GROUP BY", "ORDER BY", "ASC", @@ -724,6 +730,9 @@ const HIGH_FREQUENCY_KEYWORDS = new Set([ "ON", "IN", "AS", + "GROUP", + "ORDER", + "BY", "GROUP BY", "ORDER BY", "LEFT", @@ -5764,7 +5773,7 @@ function buildKeywordPrefixContinuationItems(prefix: string, context: SqlComplet const normalizedPrefix = prefix.toLowerCase(); return buildKeywordItems(prefix, context, databaseType, keywordCase).filter((item) => { const normalizedLabel = item.label.toLowerCase(); - return normalizedLabel.length > normalizedPrefix.length && normalizedLabel.startsWith(normalizedPrefix); + return (normalizedLabel.length > normalizedPrefix.length && normalizedLabel.startsWith(normalizedPrefix)) || (normalizedLabel === normalizedPrefix && item.label !== prefix); }); }