fix(sql): improve completion triggers
Co-authored-by: zipg <4047349+zipg@users.noreply.github.com>
This commit is contained in:
1 parent
90bdacfd12
commit
7ccc632ce8
4 files changed
+191
-20
No files matched your search
@@ -1,5 +1,18 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { buildSqlCompletionItems, getSqlCompletionContext } from "@/lib/sql/sqlCompletion";
|
||||
import { buildSqlCompletionItems, getSqlCompletionContext, shouldAutoOpenSqlCompletion } from "@/lib/sql/sqlCompletion";
|
||||
|
||||
describe("sqlCompletion keyword snippets", () => {
|
||||
it("auto-opens and suggests SELECT when typing sel", () => {
|
||||
const sql = "sel";
|
||||
const items = buildSqlCompletionItems(sql, sql.length, {
|
||||
tables: [],
|
||||
columnsByTable: new Map(),
|
||||
});
|
||||
|
||||
expect(shouldAutoOpenSqlCompletion(sql, sql.length)).toBe(true);
|
||||
expect(items).toEqual(expect.arrayContaining([expect.objectContaining({ label: "select *", type: "snippet" }), expect.objectContaining({ label: "SELECT", type: "keyword" })]));
|
||||
});
|
||||
});
|
||||
|
||||
describe("sqlCompletion quoted schema qualifiers", () => {
|
||||
it("parses quoted PostgreSQL schema names before a dot", () => {
|
||||
@@ -28,6 +41,20 @@ describe("sqlCompletion quoted schema qualifiers", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("sqlCompletion table targets", () => {
|
||||
it("does not suggest aliases while completing an empty FROM target before LIMIT", () => {
|
||||
const sql = "SELECT *\nFROM \nLIMIT 100;";
|
||||
const cursor = "SELECT *\nFROM ".length;
|
||||
const items = buildSqlCompletionItems(sql, cursor, {
|
||||
tables: [{ name: "users", type: "table" }],
|
||||
columnsByTable: new Map(),
|
||||
});
|
||||
|
||||
expect(items.some((item) => item.type === "snippet" && item.detail === "alias for LIMIT")).toBe(false);
|
||||
expect(items.some((item) => item.type === "table" && item.label === "users")).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("sqlCompletion table aliases", () => {
|
||||
it("applies generated aliases to table completions when enabled", () => {
|
||||
const sql = "SELECT * FROM ord";
|
||||
@@ -145,6 +172,18 @@ describe("sqlCompletion scoped context classification", () => {
|
||||
expect(context.suggestRoutines).toBe(false);
|
||||
});
|
||||
|
||||
it("auto-opens column completion after WHERE whitespace before LIMIT", () => {
|
||||
const sql = "SELECT *\nFROM t_0001 AS t0 WHERE \nLIMIT 100;";
|
||||
const cursor = "SELECT *\nFROM t_0001 AS t0 WHERE ".length;
|
||||
const context = getSqlCompletionContext(sql, cursor);
|
||||
|
||||
expect(context.contextKind).toBe("column");
|
||||
expect(context.prefix).toBe("");
|
||||
expect(context.referencedTables).toEqual(expect.arrayContaining([expect.objectContaining({ name: "t_0001", alias: "t0" })]));
|
||||
expect(context.suggestColumns).toBe(true);
|
||||
expect(shouldAutoOpenSqlCompletion(sql, cursor)).toBe(true);
|
||||
});
|
||||
|
||||
it("classifies CALL routine contexts", () => {
|
||||
const sql = "CALL usp_";
|
||||
const context = getSqlCompletionContext(sql, sql.length);
|
||||
|
||||
@@ -1262,7 +1262,8 @@ class SqlCompletionProvider {
|
||||
this.items.push(...buildInsertAllColumnItems(context, this.input.columnsByTable, this.t, this.dialect));
|
||||
}
|
||||
|
||||
if (!pendingJoinKeyword && context.referencedTables.length > 0 && !context.suggestColumns && !context.insertTable) {
|
||||
const emptyTableNameCompletion = !context.prefix && (context.suggestTables || context.exclusiveTableSuggestions);
|
||||
if (!pendingJoinKeyword && !emptyTableNameCompletion && context.referencedTables.length > 0 && !context.suggestColumns && !context.insertTable) {
|
||||
this.items.push(...buildAliasItems(context, this.databaseType));
|
||||
}
|
||||
|
||||
@@ -1304,10 +1305,23 @@ export function shouldAutoOpenSqlCompletion(sql: string, cursor: number): boolea
|
||||
const context = getSqlCompletionContext(sql, cursor);
|
||||
if (previousChar === "(" && context.insertTable) return true;
|
||||
if (/[,;()[\]]/.test(previousChar)) return false;
|
||||
if (context.exclusiveTableSuggestions || context.exclusiveColumnSuggestions || context.exclusiveRoutineSuggestions || context.suggestTables) {
|
||||
if (context.exclusiveTableSuggestions || context.exclusiveRoutineSuggestions || context.suggestTables) {
|
||||
return true;
|
||||
}
|
||||
return /[\w$.@]/.test(previousChar);
|
||||
if (context.exclusiveColumnSuggestions || shouldAutoOpenColumnCompletion(context, sql, cursor)) return true;
|
||||
return /[A-Za-z_$@.]/.test(previousChar);
|
||||
}
|
||||
|
||||
function shouldAutoOpenColumnCompletion(context: SqlCompletionContext, sql: string, cursor: number): boolean {
|
||||
if (!context.suggestColumns || context.referencedTables.length === 0) return false;
|
||||
if (context.prefix.length > 0) return true;
|
||||
return isColumnCompletionExpressionStart(sql.slice(0, cursor));
|
||||
}
|
||||
|
||||
function isColumnCompletionExpressionStart(beforeCursor: string): boolean {
|
||||
const cleaned = stripSqlLiterals(beforeCursor).trimEnd();
|
||||
if (!cleaned) return false;
|
||||
return /(?:\b(?:where|on|having|and|or|not|is|like|in|between|by)\b|[,(])$/i.test(cleaned);
|
||||
}
|
||||
|
||||
export function isSqlCompletionSuppressedContext(sql: string, cursor: number): boolean {
|
||||
@@ -1819,9 +1833,10 @@ function isInColumnContext(beforeCursor: string): boolean {
|
||||
|
||||
// Check the last 3 words for column-context keywords
|
||||
for (let i = lastWords.length - 1; i >= Math.max(0, lastWords.length - 3); i--) {
|
||||
const word = lastWords[i]?.toLowerCase().replace(/[^a-z0-9.]/g, "") ?? "";
|
||||
const rawWord = lastWords[i]?.toLowerCase() ?? "";
|
||||
if (/^[=<>!+\-/(,]$/.test(rawWord)) return true;
|
||||
const word = rawWord.replace(/[^a-z0-9.]/g, "");
|
||||
// Operators that indicate column context
|
||||
if (/^[=<>!+\-*/(,]$/.test(word)) return true;
|
||||
// Keywords that directly precede column expressions
|
||||
if (["where", "on", "having", "set", "and", "or", "not", "is", "like", "in", "between", "select"].includes(word)) {
|
||||
return true;
|
||||
@@ -2054,6 +2069,7 @@ function isAfterSelectBodyExpression(beforeToken: string): boolean {
|
||||
if (!/^\s*(?:with\b[\s\S]*\bselect\b|select\b)/i.test(cleaned)) return false;
|
||||
if (!/\bfrom\b/i.test(cleaned)) return false;
|
||||
if (/\b(?:limit|offset|union|intersect|except)\b/i.test(cleaned)) return false;
|
||||
if (/[,.(+\-*/%<>=!&|]$/.test(cleaned)) return false;
|
||||
const lastKeyword = /\b([A-Za-z_][\w$]*)\s*$/.exec(cleaned)?.[1]?.toLowerCase();
|
||||
if (lastKeyword && SELECT_BODY_INCOMPLETE_TAIL_KEYWORDS.has(lastKeyword)) return false;
|
||||
return true;
|
||||
|
||||
Reference in new issue
Block a user