fix(sql): stop completion at the next top-level statement

This commit is contained in:
dienaso authored and GitHub committed 2026-09-18 01:35:53 +08:00
1 parent 7b164d0084
commit 4cdf18d652
2 files changed
+137 -3

No files matched your search

@@ -1484,3 +1484,62 @@ describe("select alias visibility", () => {
expect(untyped.selectAliases).toContain("cnt");
});
});
describe("line block statement boundary", () => {
it.each(["-- explanatory comment", "/* explanatory comment */", "/*\n explanatory comment\n*/"])("keeps column sources after %s", (comment) => {
const sql = `select na\n${comment}\nfrom users`;
const cursor = "select na".length;
const context = getSqlCompletionContext(sql, cursor, { databaseType: "iris" });
expect(context.referencedTables.map((table) => table.name)).toEqual(["users"]);
const items = buildSqlCompletionItems(sql, cursor, {
databaseType: "iris",
tables: [{ name: "users", type: "table" }],
columnsByTable: new Map([["users", [{ name: "name", table: "users", key: "name" } as never]]]),
});
expect(items.some((item) => item.label === "name")).toBe(true);
});
it("still ends the block at a real blank line", () => {
const sql = "select na\n\nfrom users";
const context = getSqlCompletionContext(sql, "select na".length, { databaseType: "iris" });
expect(context.referencedTables).toEqual([]);
});
it("stops the active block at a top-level statement line without a semicolon", () => {
// t8y2/dbx#9370: two semicolon-free SELECT lines — completion at the first
// statement's WHERE must not pull the second statement's table in.
const sql = "select * from CT_Nation where\nselect * from CT_CityArea";
const cursor = sql.indexOf("where") + "where".length;
const context = getSqlCompletionContext(sql, cursor, { databaseType: "iris" });
expect(context.referencedTables.map((table) => table.name)).toEqual(["CT_Nation"]);
});
it("keeps column completion unqualified once the second statement is excluded", () => {
const sql = "select * from CT_Nation where c\nselect * from CT_CityArea";
const cursor = sql.indexOf("where") + "where c".length;
const items = buildSqlCompletionItems(sql, cursor, {
databaseType: "iris",
tables: [
{ name: "CT_Nation", type: "table" },
{ name: "CT_CityArea", type: "table" },
],
columnsByTable: new Map([
["CT_Nation", [{ name: "CTNAT_Code", table: "CT_Nation", key: "k1" } as never]],
["CT_CityArea", [{ name: "CITAREA_Code", table: "CT_CityArea", key: "k2" } as never]],
]),
});
expect(items.some((item) => item.label === "CT_CityArea.CITAREA_Code")).toBe(false);
expect(items.some((item) => item.label === "CTNAT_Code")).toBe(true);
});
it("does not end the block at a SELECT line inside parentheses", () => {
const sql = "select *\nfrom (\n select id from users\n) x\nwhere |";
const cursor = sql.length;
const context = getSqlCompletionContext(sql, cursor, { databaseType: "mysql" });
expect(context.referencedTables.map((table) => table.name)).toEqual(expect.arrayContaining(["users"]));
});
});