fix(completion): rank CREATE OR REPLACE keyword over REPLACE function

This commit is contained in:
zipg authored and GitHub committed 2026-10-02 00:43:08 +08:00
1 parent ff926cc61b
commit 4ed3662da2
5 files changed
+31

No files matched your search

@@ -51,6 +51,34 @@ describe("SQL completion replacement", () => {
}); });
describe("sqlCompletion keyword snippets", () => { describe("sqlCompletion keyword snippets", () => {
it.each(["mysql", "postgres", "oracle"] as const)("offers the REPLACE keyword in %s CREATE OR statements", (databaseType) => {
for (const text of ["CREATE OR ", "CREATE OR r", "CREATE OR repla", "-- heading\nCREATE /* comment */ OR repla"]) {
const items = buildSqlCompletionItems(text, text.length, { tables: [], columnsByTable: new Map(), databaseType });
expect(items[0], text).toMatchObject({ label: "REPLACE", type: "keyword" });
expect(items[0].apply ?? items[0].label).toBe("REPLACE");
}
});
it("preserves REPLACE function completion in SELECT expressions", () => {
const text = "SELECT repla";
const items = buildSqlCompletionItems(text, text.length, { tables: [], columnsByTable: new Map(), databaseType: "mysql" });
expect(items[0]).toMatchObject({ label: "REPLACE", type: "function" });
expect(items[0]?.apply).toMatch(/^REPLACE\(/);
});
it.each(["sqlserver", "sqlite"] as const)("does not suggest unsupported CREATE OR REPLACE syntax on %s", (databaseType) => {
const text = "CREATE OR repla";
const items = buildSqlCompletionItems(text, text.length, { tables: [], columnsByTable: new Map(), databaseType });
expect(items.some((item) => item.type === "keyword" && item.label === "REPLACE")).toBe(false);
});
it("honors lower-case keyword preferences for CREATE OR REPLACE", () => {
const text = "create or repla";
const items = buildSqlCompletionItems(text, text.length, { tables: [], columnsByTable: new Map(), databaseType: "postgres", keywordCase: "lower" });
expect(items[0]).toMatchObject({ label: "replace", type: "keyword" });
expect(items[0].apply ?? items[0].label).toBe("replace");
});
it("auto-opens and suggests SELECT when typing sel", () => { it("auto-opens and suggests SELECT when typing sel", () => {
const sql = "sel"; const sql = "sel";
const items = buildSqlCompletionItems(sql, sql.length, { const items = buildSqlCompletionItems(sql, sql.length, {
@@ -3213,6 +3213,9 @@ function preferredKeywordsForCompletion(
databaseType: DatabaseType | undefined, databaseType: DatabaseType | undefined,
): string[] { ): string[] {
const keywords: string[] = []; const keywords: string[] = [];
if (databaseType !== "sqlserver" && databaseType !== "sqlite" && /^create\s+or$/i.test(maskSqlLiteralsAndComments(beforeToken, databaseType).trim())) {
keywords.push("REPLACE");
}
if (selectListColumnContext && hasSelectListExpression(beforeCursor, databaseType)) keywords.push("FROM"); if (selectListColumnContext && hasSelectListExpression(beforeCursor, databaseType)) keywords.push("FROM");
if (isAfterJoinModifierContext(beforeCursor, databaseType)) keywords.push("JOIN"); if (isAfterJoinModifierContext(beforeCursor, databaseType)) keywords.push("JOIN");
if (!exclusiveTableSuggestions && isAfterSelectBodyExpression(beforeToken, databaseType)) keywords.push("LIMIT"); if (!exclusiveTableSuggestions && isAfterSelectBodyExpression(beforeToken, databaseType)) keywords.push("LIMIT");
Binary file not shown.

After

Width:  |  Height:  |  Size: 6.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB