feat(editor): add executable statement markers and per-statement run

This commit is contained in:
二丫讲梵 authored and GitHub committed 2026-06-28 12:13:29 +08:00
1 parent baeaeba431
commit df43561738
3 files changed
+169 -13

No files matched your search

+28 -3
View File
@@ -878,6 +878,11 @@ function normalizeSql(sql: string): string {
* cursor statement and the full document are effectively the same SQL — in
* that case only a single candidate is returned to avoid duplicates.
*/
export function executableStatementRanges(sql: string, databaseType?: DatabaseType): SqlTextRange[] {
if (databaseType === "redis") return redisExecutableCommandRanges(sql);
return splitSqlStatementRanges(sql, databaseType).flatMap((statement) => splitStatementRangeAtSoftStarts(sql, statement, databaseType).map((range) => rangeFor(range, sql)));
}
export function buildExecutionCandidates(sql: string, cursorPos: number, databaseType?: DatabaseType): SqlExecutionCandidate[] {
const full = fullSqlRange(sql);
const cursorStatement = databaseType === "redis" ? redisCommandRangeAtCursor(sql, cursorPos) : statementRangeAtCursor(sql, cursorPos, databaseType);
@@ -911,15 +916,35 @@ function candidateFromRange(range: SqlTextRange, kind: SqlExecutionCandidate["ki
function redisExecutableCommandCount(sql: string): number {
let count = 0;
for (const line of sql.split(/\r?\n/)) {
const trimmed = line.trim();
if (!trimmed || trimmed.startsWith("#")) continue;
for (const range of redisExecutableCommandRanges(sql)) {
if (!range.sql.trim()) continue;
count += 1;
if (count > 1) return count;
}
return count;
}
function redisExecutableCommandRanges(sql: string): SqlTextRange[] {
const ranges: SqlTextRange[] = [];
let lineStart = 0;
while (lineStart <= sql.length) {
let lineEnd = sql.indexOf("\n", lineStart);
if (lineEnd === -1) lineEnd = sql.length;
const rawLine = sql.slice(lineStart, lineEnd);
const leadingWhitespace = rawLine.length - rawLine.trimStart().length;
const trailingWhitespace = rawLine.length - rawLine.trimEnd().length;
const trimmedLine = rawLine.trim();
if (trimmedLine && !trimmedLine.startsWith("#")) {
const from = lineStart + leadingWhitespace;
const to = lineStart + rawLine.length - trailingWhitespace;
ranges.push({ from, to, sql: sql.slice(from, to) });
}
if (lineEnd >= sql.length) break;
lineStart = lineEnd + 1;
}
return ranges;
}
function redisCommandRangeAtCursor(sql: string, cursorPos: number): SqlTextRange | null {
const pos = clampCursor(sql, cursorPos);
if (isCursorOnBlankLine(sql, pos)) return null;