fix(editor): preserve current SQL execution boundaries
This commit is contained in:
13 files changed
+232
-44
No files matched your search
@@ -2,15 +2,42 @@ import { describe, expect, it } from "vitest";
|
||||
import { currentStatementFrameRangeTo, estimateInlineHintVisualColumns, isWideSqlChar, shouldRebuildCurrentStatementFrame, visualSqlColumns, visualSqlColumnsWithInlineHints } from "@/lib/sql/currentStatementFrame";
|
||||
import type { SqlTextRange } from "@/lib/sql/sqlStatementRanges";
|
||||
|
||||
function frameDocument(sql: string) {
|
||||
return {
|
||||
length: sql.length,
|
||||
sliceString: (from: number, to: number) => sql.slice(from, to),
|
||||
};
|
||||
}
|
||||
|
||||
describe("currentStatementFrameRangeTo", () => {
|
||||
it("includes a directly adjacent trailing semicolon in frame width calculations", () => {
|
||||
const sql = "SELECT 1;";
|
||||
const range: SqlTextRange = { from: 0, to: "SELECT 1".length, sql: "SELECT 1" };
|
||||
expect(currentStatementFrameRangeTo(";", range)).toBe(range.to + 1);
|
||||
expect(currentStatementFrameRangeTo(frameDocument(sql), range)).toBe(sql.length);
|
||||
});
|
||||
|
||||
it("does not extend the frame when the next character is not a semicolon", () => {
|
||||
it("includes a trailing semicolon on its own line", () => {
|
||||
const sql = "SELECT 1\n;\n\nSELECT 2;";
|
||||
const range: SqlTextRange = { from: 0, to: "SELECT 1".length, sql: "SELECT 1" };
|
||||
expect(currentStatementFrameRangeTo("\n", range)).toBe(range.to);
|
||||
expect(currentStatementFrameRangeTo(frameDocument(sql), range)).toBe(sql.indexOf(";") + 1);
|
||||
});
|
||||
|
||||
it("does not extend the frame across a comment before a later semicolon", () => {
|
||||
const sql = "SELECT 1\n-- comment\n;";
|
||||
const range: SqlTextRange = { from: 0, to: "SELECT 1".length, sql: "SELECT 1" };
|
||||
expect(currentStatementFrameRangeTo(frameDocument(sql), range)).toBe(range.to);
|
||||
});
|
||||
|
||||
it("does not extend the frame across a blank line before a later semicolon", () => {
|
||||
const sql = "SELECT 1\n\n;";
|
||||
const range: SqlTextRange = { from: 0, to: "SELECT 1".length, sql: "SELECT 1" };
|
||||
expect(currentStatementFrameRangeTo(frameDocument(sql), range)).toBe(range.to);
|
||||
});
|
||||
|
||||
it("does not extend the frame when the next non-whitespace character is not a semicolon", () => {
|
||||
const sql = "SELECT 1\n\nSELECT 2";
|
||||
const range: SqlTextRange = { from: 0, to: "SELECT 1".length, sql: "SELECT 1" };
|
||||
expect(currentStatementFrameRangeTo(frameDocument(sql), range)).toBe(range.to);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -93,6 +93,24 @@ describe("executableStatementRangeCacheForDoc", () => {
|
||||
expect(executableStatementRangeAtCursor(cache, semicolonGapCursor)?.sql).toBe("SELECT 1");
|
||||
});
|
||||
|
||||
it("keeps a standalone next-line semicolon attached to the current statement", () => {
|
||||
const sql = "SELECT *\nFROM users\n;\n\nSELECT * FROM audit;";
|
||||
const doc = Text.of(sql.split("\n"));
|
||||
const cache = executableStatementRangeCacheForDoc(null, doc, "mysql");
|
||||
const delimiterCursor = sql.indexOf(";");
|
||||
|
||||
expect(executableStatementRangeAtCursor(cache, delimiterCursor)?.sql).toBe("SELECT *\nFROM users");
|
||||
expect(executableStatementRangeAtCursor(cache, delimiterCursor + 1)?.sql).toBe("SELECT *\nFROM users");
|
||||
});
|
||||
|
||||
it("does not attach a semicolon after a blank line to the previous statement", () => {
|
||||
const sql = "SELECT 1\n\n;";
|
||||
const doc = Text.of(sql.split("\n"));
|
||||
const cache = executableStatementRangeCacheForDoc(null, doc, "mysql");
|
||||
|
||||
expect(executableStatementRangeAtCursor(cache, sql.indexOf(";"))).toBeNull();
|
||||
});
|
||||
|
||||
it("returns null for blank and pure comment cursor lines", () => {
|
||||
const doc = Text.of(["SELECT 1;", "-- comment", "/* block comment */", "", "SELECT 2;"]);
|
||||
const cache = executableStatementRangeCacheForDoc(null, doc, "mysql");
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { resolveExecutableSql } from "@/lib/sql/sqlExecutionTarget";
|
||||
import { executionCandidateForMode, resolveExecutableSql, type SqlExecutionCandidate } from "@/lib/sql/sqlExecutionTarget";
|
||||
|
||||
function candidate(kind: SqlExecutionCandidate["kind"], supportedKinds: SqlExecutionCandidate["supportedKinds"]): SqlExecutionCandidate {
|
||||
return { kind, supportedKinds, label: kind, sql: "SELECT 1", from: 0, to: 8 };
|
||||
}
|
||||
|
||||
describe("resolveExecutableSql", () => {
|
||||
it("uses selected SQL before cursor-mode resolution", () => {
|
||||
@@ -10,3 +14,27 @@ describe("resolveExecutableSql", () => {
|
||||
expect(resolveExecutableSql(sql, selectedSql, { mode: "current", cursorPos: cursorAfterFirstSemicolon })).toBe("select 2;");
|
||||
});
|
||||
});
|
||||
|
||||
describe("executionCandidateForMode", () => {
|
||||
it("does not fall back to all SQL when current mode has no cursor statement", () => {
|
||||
const all = candidate("all", ["all"]);
|
||||
|
||||
expect(executionCandidateForMode([all], "current")).toBeNull();
|
||||
expect(executionCandidateForMode([all], "all")).toBe(all);
|
||||
});
|
||||
|
||||
it("uses the deduplicated candidate when one statement is both current and all", () => {
|
||||
const currentAndAll = candidate("all", ["cursor", "all"]);
|
||||
|
||||
expect(executionCandidateForMode([currentAndAll], "current")).toBe(currentAndAll);
|
||||
expect(executionCandidateForMode([currentAndAll], "all")).toBe(currentAndAll);
|
||||
});
|
||||
|
||||
it("selects the exact target when current and all candidates are distinct", () => {
|
||||
const current = candidate("cursor", ["cursor"]);
|
||||
const all = candidate("all", ["all"]);
|
||||
|
||||
expect(executionCandidateForMode([current, all], "current")).toBe(current);
|
||||
expect(executionCandidateForMode([current, all], "all")).toBe(all);
|
||||
});
|
||||
});
|
||||
@@ -1,4 +1,5 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { executionCandidateForMode } from "@/lib/sql/sqlExecutionTarget";
|
||||
import { buildExecutionCandidates, currentExecutableStatementRange, executableStatementRanges, fullSqlRange, hasMultipleExecutionTargets, splitSqlStatementRanges, statementRangeAtCursor, supportsExecutionTargetPicker } from "@/lib/sql/sqlStatementRanges";
|
||||
|
||||
function indexOf(sql: string, needle: string, occurrence = 1): number {
|
||||
@@ -531,6 +532,22 @@ GET /_cat/indices`;
|
||||
expect(range?.sql.trim()).toBe("SELECT *\nFROM system_dept");
|
||||
});
|
||||
|
||||
it("keeps a standalone next-line semicolon cursor on the current multi-line statement", () => {
|
||||
const sql = "SELECT *\nFROM system_dept\n;\n\nSELECT * FROM sys;";
|
||||
const delimiterPos = sql.indexOf(";");
|
||||
|
||||
expect(statementRangeAtCursor(sql, delimiterPos)?.sql.trim()).toBe("SELECT *\nFROM system_dept");
|
||||
expect(statementRangeAtCursor(sql, delimiterPos + 1)?.sql.trim()).toBe("SELECT *\nFROM system_dept");
|
||||
});
|
||||
|
||||
it("assigns a standalone trailing semicolon to the final soft statement", () => {
|
||||
const sql = "SELECT * FROM `t_0001`\nSELECT * FROM `t_0001` LIMIT 1\n;";
|
||||
const delimiterPos = sql.lastIndexOf(";");
|
||||
|
||||
expect(statementRangeAtCursor(sql, delimiterPos, "mysql")?.sql).toBe("SELECT * FROM `t_0001` LIMIT 1");
|
||||
expect(statementRangeAtCursor(sql, delimiterPos + 1, "mysql")?.sql).toBe("SELECT * FROM `t_0001` LIMIT 1");
|
||||
});
|
||||
|
||||
it("returns the next same-line statement when the cursor is inside it", () => {
|
||||
const sql = "SELECT 1; SELECT 2;";
|
||||
const pos = indexOf(sql, "SELECT 2") + 1;
|
||||
@@ -1113,6 +1130,20 @@ describe("buildExecutionCandidates", () => {
|
||||
expect(candidateLabels(candidates)).toEqual(["currentStatement", "allStatements"]);
|
||||
});
|
||||
|
||||
it("keeps a MySQL FORCE INDEX query current when its semicolon is on the next line", () => {
|
||||
const firstStatement = `SELECT count(*)
|
||||
FROM cus_loan_status_copy1 t2
|
||||
LEFT JOIN case_allocation_details_copy1 t4 FORCE INDEX(idx_debt_case_number)
|
||||
ON t2.caseno = t4.debt_case_number
|
||||
WHERE t2.product_name = '12345'
|
||||
;`;
|
||||
const sql = `${firstStatement}\n\nSELECT count(*) FROM cus_loan_status_copy1;`;
|
||||
const candidates = buildExecutionCandidates(sql, indexOf(sql, "12345"), "mysql");
|
||||
|
||||
expect(candidateKinds(candidates)).toEqual(["cursor", "all"]);
|
||||
expect(executionCandidateForMode(candidates, "current")?.sql.trim()).toBe(firstStatement.replace(/\n;$/, ""));
|
||||
});
|
||||
|
||||
it("uses the current statement when the cursor is immediately after its semicolon before a blank line", () => {
|
||||
const sql = "select 1;\n\nselect 2;";
|
||||
const cursorAfterFirstSemicolon = sql.indexOf(";") + 1;
|
||||
@@ -1126,6 +1157,13 @@ describe("buildExecutionCandidates", () => {
|
||||
expect(candidateSummaries(candidates)).toEqual(["cursor:SELECT 2", "all:SELECT 1\nSELECT 2"]);
|
||||
});
|
||||
|
||||
it("uses the final soft statement when the cursor is on a standalone trailing semicolon", () => {
|
||||
const sql = "SELECT * FROM `t_0001`\nSELECT * FROM `t_0001` LIMIT 1\n;";
|
||||
const candidates = buildExecutionCandidates(sql, sql.lastIndexOf(";"), "mysql");
|
||||
|
||||
expect(candidateSummaries(candidates)).toEqual(["cursor:SELECT * FROM `t_0001` LIMIT 1", "all:SELECT * FROM `t_0001`\nSELECT * FROM `t_0001` LIMIT 1\n;"]);
|
||||
});
|
||||
|
||||
it("dedupes when the cursor statement equals the full document", () => {
|
||||
const sql = "SELECT 1;";
|
||||
const candidates = buildExecutionCandidates(sql, indexOf(sql, "1"));
|
||||
@@ -1137,6 +1175,16 @@ describe("buildExecutionCandidates", () => {
|
||||
const sql = "SELECT 1;\n\nSELECT 2;";
|
||||
const candidates = buildExecutionCandidates(sql, sql.indexOf("\n") + 1);
|
||||
expect(candidateKinds(candidates)).toEqual(["all"]);
|
||||
expect(candidates[0].supportedKinds).toEqual(["all"]);
|
||||
expect(executionCandidateForMode(candidates, "current")).toBeNull();
|
||||
expect(executionCandidateForMode(candidates, "all")).toBe(candidates[0]);
|
||||
});
|
||||
|
||||
it("marks a deduplicated single-statement candidate as both current and all", () => {
|
||||
const sql = "SELECT 1;";
|
||||
const candidates = buildExecutionCandidates(sql, indexOf(sql, "1"));
|
||||
|
||||
expect(candidates[0].supportedKinds).toEqual(["cursor", "all"]);
|
||||
});
|
||||
|
||||
it("returns no candidates for an empty document", () => {
|
||||
|
||||
Reference in new issue
Block a user