diff --git a/apps/desktop/src/lib/__tests__/sql/sqlStatementRanges.spec.ts b/apps/desktop/src/lib/__tests__/sql/sqlStatementRanges.spec.ts index db67954da1..4e3b195966 100644 --- a/apps/desktop/src/lib/__tests__/sql/sqlStatementRanges.spec.ts +++ b/apps/desktop/src/lib/__tests__/sql/sqlStatementRanges.spec.ts @@ -1101,6 +1101,12 @@ describe("buildExecutionCandidates", () => { expect(candidateSummaries(candidates)).toEqual(["cursor:select 1", "all:select 1;\n\nselect 2;"]); }); + it("uses the final soft statement when the cursor follows trailing EOF whitespace", () => { + const sql = "SELECT 1\nSELECT 2 "; + const candidates = buildExecutionCandidates(sql, sql.length); + expect(candidateSummaries(candidates)).toEqual(["cursor:SELECT 2", "all:SELECT 1\nSELECT 2"]); + }); + it("dedupes when the cursor statement equals the full document", () => { const sql = "SELECT 1;"; const candidates = buildExecutionCandidates(sql, indexOf(sql, "1")); diff --git a/apps/desktop/src/lib/sql/sqlStatementRanges.ts b/apps/desktop/src/lib/sql/sqlStatementRanges.ts index 385fc5727b..47f2e2623f 100644 --- a/apps/desktop/src/lib/sql/sqlStatementRanges.ts +++ b/apps/desktop/src/lib/sql/sqlStatementRanges.ts @@ -585,8 +585,10 @@ export function statementRangeAtCursor(sql: string, cursorPos: number, databaseT return rangeForCursorInSoftRanges(sql, softRanges, pos) ?? rangeFor(statement, sql); } - if (pos > statement.to && (!next || pos < next.hitFrom) && isCursorOnStatementLine(sql, pos, statement)) { - return rangeForCursorInSoftRanges(sql, softRanges, pos) ?? rangeFor(statement, sql); + if (pos > statement.to && (!next || pos < next.hitFrom)) { + const softRange = rangeForCursorInSoftRanges(sql, softRanges, pos); + if (softRange) return softRange; + if (isCursorOnStatementLine(sql, pos, statement)) return rangeFor(statement, sql); } }