fix(editor): stop per-keystroke full-document statement reparse

Closes #8600
This commit is contained in:
t8y2 committed 2026-09-17 01:30:02 +08:00
1 parent 84ae93aaa8
commit ab5aba9497
5 files changed
+297 -52

No files matched your search

@@ -1,4 +1,4 @@
import type { Text } from "@codemirror/state";
import type { ChangeSet, Text } from "@codemirror/state";
import type { DatabaseType } from "@/types/database";
import { readSqlBracedParameterAt, type SqlParameterOptions } from "@/lib/sql/sqlParameters";
import { executableStatementRanges, type SqlTextRange } from "@/lib/sql/sqlStatementRanges";
@@ -14,6 +14,36 @@ export interface ExecutableStatementRangeCache {
ranges: SqlTextRange[];
}
/**
* Membership-only view of the statement-start positions a run-statement gutter
* needs (`byStart` keys plus `byExecutableLineStart` keys). Kept separate from
* the full cache so it can be cheaply shifted through a ChangeSet on every
* keystroke and fully rebuilt only after typing pauses, instead of re-parsing
* the whole document synchronously per keystroke.
*/
export interface StatementGutterStartIndex {
starts: ReadonlySet<number>;
executableLineStarts: ReadonlySet<number>;
}
export function statementGutterStartIndexForCache(cache: ExecutableStatementRangeCache): StatementGutterStartIndex {
return { starts: new Set(cache.byStart.keys()), executableLineStarts: new Set(cache.byExecutableLineStart.keys()) };
}
export function mapStatementGutterStartIndex(index: StatementGutterStartIndex, changes: ChangeSet): StatementGutterStartIndex {
return { starts: mapStartPositions(index.starts, changes), executableLineStarts: mapStartPositions(index.executableLineStarts, changes) };
}
function mapStartPositions(positions: ReadonlySet<number>, changes: ChangeSet): Set<number> {
const mapped = new Set<number>();
for (const position of positions) mapped.add(changes.mapPos(position, 1));
return mapped;
}
export function statementGutterStartIndexHasStartAt(index: StatementGutterStartIndex, lineFrom: number): boolean {
return index.starts.has(lineFrom) || index.executableLineStarts.has(lineFrom);
}
export type ExecutableStatementRangeParser = (sql: string, databaseType?: DatabaseType, parameterOptions?: SqlParameterOptions) => SqlTextRange[];
export function executableStatementRangeCacheForDoc(