fix(completion): keep FROM tables across blank lines in one statement

This commit is contained in:
dienaso authored and GitHub committed 2026-09-26 06:23:21 +08:00
1 parent 1364293202
commit ec9935d3ea
3 files changed
+75 -3

No files matched your search

@@ -1626,10 +1626,20 @@ describe("line block statement boundary", () => {
expect(items.some((item) => item.label === "name")).toBe(true); expect(items.some((item) => item.label === "name")).toBe(true);
}); });
it("still ends the block at a real blank line", () => { it("still ends the block at a blank line before a new statement", () => {
// #10196 refined the blank-line rule: a blank line only ends the block when
// the next non-empty line opens a new top-level statement. A blank line
// before FROM/WHERE continues the same statement.
const sql = "select na from t1\n\nselect nb from t2";
const context = getSqlCompletionContext(sql, "select na from t1".length, { databaseType: "iris" });
expect(context.referencedTables.map((table) => table.name)).toEqual(["t1"]);
});
it("keeps FROM tables when the select list is separated from FROM by a blank line (#10196)", () => {
const sql = "select na\n\nfrom users"; const sql = "select na\n\nfrom users";
const context = getSqlCompletionContext(sql, "select na".length, { databaseType: "iris" }); const context = getSqlCompletionContext(sql, "select na".length, { databaseType: "iris" });
expect(context.referencedTables).toEqual([]); expect(context.referencedTables.map((table) => table.name)).toEqual(["users"]);
expect(context.suggestColumns).toBe(true);
}); });
it("stops the active block at a top-level statement line without a semicolon", () => { it("stops the active block at a top-level statement line without a semicolon", () => {
+25 -1
View File
@@ -2308,9 +2308,20 @@ function currentLineBlockEnd(sql: string, cursor: number, start: number): number
const boundedLineEnd = lineEnd >= 0 ? lineEnd : sql.length; const boundedLineEnd = lineEnd >= 0 ? lineEnd : sql.length;
const originalTrimmed = sql.slice(lineStart, boundedLineEnd).trimStart(); const originalTrimmed = sql.slice(lineStart, boundedLineEnd).trimStart();
const trimmed = masked.slice(lineStart, boundedLineEnd).trimStart(); const trimmed = masked.slice(lineStart, boundedLineEnd).trimStart();
if (lineStart > start && (!originalTrimmed || /^(get|post|put|delete|patch|head)\s+\//i.test(originalTrimmed))) { if (lineStart > start && /^(get|post|put|delete|patch|head)\s+\//i.test(originalTrimmed)) {
return lineStart; return lineStart;
} }
// A blank line only ends the block when the next non-empty line opens a new
// top-level statement. Blank lines *inside* one statement (`SELECT list`
// then a blank line then `FROM t`) must keep the later FROM in scope so
// column hints still resolve (#10196); #9370's next-statement exclusion is
// already handled by the statement-start rule below.
if (lineStart > start && !originalTrimmed && depth === 0) {
const nextContent = nextNonEmptyLineTrimmed(sql, boundedLineEnd);
if (nextContent === null || STATEMENT_START_LINE_PATTERN.test(nextContent) || /^(get|post|put|delete|patch|head)\s+\//i.test(nextContent)) {
return lineStart;
}
}
// The cursor's own line always belongs to the block; only a following // The cursor's own line always belongs to the block; only a following
// top-level statement line ends it. // top-level statement line ends it.
if (!atCursorLine && lineStart > start && depth === 0 && STATEMENT_START_LINE_PATTERN.test(trimmed)) { if (!atCursorLine && lineStart > start && depth === 0 && STATEMENT_START_LINE_PATTERN.test(trimmed)) {
@@ -2328,6 +2339,19 @@ function currentLineBlockEnd(sql: string, cursor: number, start: number): number
return null; return null;
} }
function nextNonEmptyLineTrimmed(sql: string, from: number): string | null {
let offset = from;
while (offset < sql.length) {
const lineEnd = sql.indexOf("\n", offset);
const boundedEnd = lineEnd >= 0 ? lineEnd : sql.length;
const trimmed = sql.slice(offset, boundedEnd).trim();
if (trimmed) return trimmed;
if (lineEnd < 0) return null;
offset = lineEnd + 1;
}
return null;
}
export function getSqlCompletionResultValidFor(sql: string, cursor: number): RegExp | undefined { export function getSqlCompletionResultValidFor(sql: string, cursor: number): RegExp | undefined {
void sql; void sql;
void cursor; void cursor;
+38
View File
@@ -1343,6 +1343,44 @@ test("uses the full Unicode prefix as the replacement range without result reuse
assert.equal(getSqlCompletionResultValidFor(sql, sql.length), undefined); assert.equal(getSqlCompletionResultValidFor(sql, sql.length), undefined);
}); });
test("keeps FROM tables when the select list is separated from FROM by a blank line (#10196)", () => {
const sql = "SELECT id, name\n\nFROM users";
const cursor = "SELECT id, name".length;
const context = getSqlCompletionContext(sql, cursor, { databaseType: "mysql" });
assert.deepEqual(
context.referencedTables.map((table) => table.name),
["users"],
);
assert.equal(context.suggestColumns, true);
assert.equal(shouldAutoOpenSqlCompletion(sql, cursor, { databaseType: "mysql" }), true);
});
test("keeps column hints while typing a select-list prefix before a blank-line FROM (#10196)", () => {
const sql = "SELECT id, na\n\nFROM users";
const cursor = sql.indexOf("na") + 2;
const context = getSqlCompletionContext(sql, cursor, { databaseType: "mysql" });
assert.equal(context.suggestColumns, true);
assert.deepEqual(
context.referencedTables.map((table) => table.name),
["users"],
);
assert.equal(shouldAutoOpenSqlCompletion(sql, cursor, { databaseType: "mysql" }), true);
});
test("select-list column context still works without a blank line before FROM", () => {
const sql = "SELECT id, name\nFROM users";
const cursor = "SELECT id, name".length;
const context = getSqlCompletionContext(sql, cursor, { databaseType: "mysql" });
assert.equal(context.suggestColumns, true);
assert.deepEqual(
context.referencedTables.map((table) => table.name),
["users"],
);
});
test("ranks prefix matches above substring matches for table names", () => { test("ranks prefix matches above substring matches for table names", () => {
const sql = "select * from user"; const sql = "select * from user";
const items = buildSqlCompletionItems(sql, sql.length, { const items = buildSqlCompletionItems(sql, sql.length, {