feat(sql): offer keyword completion on full word match for case conversion
This commit is contained in:
1 parent
4dbbfec86f
commit
971a95aa80
3 files changed
+155
-8
No files matched your search
@@ -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" })]));
|
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", () => {
|
it("offers DuckDB-specific query and statement keywords", () => {
|
||||||
const expectedKeywords = ["QUALIFY", "SUMMARIZE", "PIVOT", "UNPIVOT", "ASOF", "POSITIONAL", "FROM"];
|
const expectedKeywords = ["QUALIFY", "SUMMARIZE", "PIVOT", "UNPIVOT", "ASOF", "POSITIONAL", "FROM"];
|
||||||
|
|
||||||
|
|||||||
@@ -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" };
|
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) {
|
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" };
|
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" };
|
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 {
|
return {
|
||||||
kind: "update_column",
|
kind: "update_column",
|
||||||
prefix: trailing.prefix,
|
prefix: trailing.prefix,
|
||||||
|
|||||||
@@ -42,6 +42,9 @@ const SQL_KEYWORDS = [
|
|||||||
"INNER",
|
"INNER",
|
||||||
"OUTER",
|
"OUTER",
|
||||||
"ON",
|
"ON",
|
||||||
|
"GROUP",
|
||||||
|
"ORDER",
|
||||||
|
"BY",
|
||||||
"GROUP BY",
|
"GROUP BY",
|
||||||
"ORDER BY",
|
"ORDER BY",
|
||||||
"ASC",
|
"ASC",
|
||||||
@@ -301,6 +304,9 @@ const COMMON_SQL_KEYWORDS = [
|
|||||||
"INNER",
|
"INNER",
|
||||||
"OUTER",
|
"OUTER",
|
||||||
"ON",
|
"ON",
|
||||||
|
"GROUP",
|
||||||
|
"ORDER",
|
||||||
|
"BY",
|
||||||
"GROUP BY",
|
"GROUP BY",
|
||||||
"ORDER BY",
|
"ORDER BY",
|
||||||
"ASC",
|
"ASC",
|
||||||
@@ -724,6 +730,9 @@ const HIGH_FREQUENCY_KEYWORDS = new Set([
|
|||||||
"ON",
|
"ON",
|
||||||
"IN",
|
"IN",
|
||||||
"AS",
|
"AS",
|
||||||
|
"GROUP",
|
||||||
|
"ORDER",
|
||||||
|
"BY",
|
||||||
"GROUP BY",
|
"GROUP BY",
|
||||||
"ORDER BY",
|
"ORDER BY",
|
||||||
"LEFT",
|
"LEFT",
|
||||||
@@ -5764,7 +5773,7 @@ function buildKeywordPrefixContinuationItems(prefix: string, context: SqlComplet
|
|||||||
const normalizedPrefix = prefix.toLowerCase();
|
const normalizedPrefix = prefix.toLowerCase();
|
||||||
return buildKeywordItems(prefix, context, databaseType, keywordCase).filter((item) => {
|
return buildKeywordItems(prefix, context, databaseType, keywordCase).filter((item) => {
|
||||||
const normalizedLabel = item.label.toLowerCase();
|
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);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user