diff --git a/apps/desktop/src/lib/__tests__/sql/sqlCompletion.context.spec.ts b/apps/desktop/src/lib/__tests__/sql/sqlCompletion.context.spec.ts index 4f46ce1ef1..6b078eb657 100644 --- a/apps/desktop/src/lib/__tests__/sql/sqlCompletion.context.spec.ts +++ b/apps/desktop/src/lib/__tests__/sql/sqlCompletion.context.spec.ts @@ -1626,10 +1626,20 @@ describe("line block statement boundary", () => { 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 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", () => { diff --git a/apps/desktop/src/lib/sql/sqlCompletion.ts b/apps/desktop/src/lib/sql/sqlCompletion.ts index dc650d077c..614ba95293 100644 --- a/apps/desktop/src/lib/sql/sqlCompletion.ts +++ b/apps/desktop/src/lib/sql/sqlCompletion.ts @@ -2308,9 +2308,20 @@ function currentLineBlockEnd(sql: string, cursor: number, start: number): number const boundedLineEnd = lineEnd >= 0 ? lineEnd : sql.length; const originalTrimmed = sql.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; } + // 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 // top-level statement line ends it. 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; } +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 { void sql; void cursor; diff --git a/packages/app-tests/sqlCompletion.test.ts b/packages/app-tests/sqlCompletion.test.ts index 8d4ac818a7..eb286226a7 100644 --- a/packages/app-tests/sqlCompletion.test.ts +++ b/packages/app-tests/sqlCompletion.test.ts @@ -1343,6 +1343,44 @@ test("uses the full Unicode prefix as the replacement range without result reuse 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", () => { const sql = "select * from user"; const items = buildSqlCompletionItems(sql, sql.length, {