fix(editor): ignore SQL Server transaction BEGIN in folding

This commit is contained in:
Melody Ruch authored and GitHub committed 2026-08-19 15:15:20 +08:00
1 parent f937ab9c6d
commit f4f894d979
3 files changed
+288

No files matched your search

@@ -0,0 +1,117 @@
import { foldService, syntaxTree } from "@codemirror/language";
import type { EditorState } from "@codemirror/state";
// `@lezer/common` is only a transitive dependency here (see sqlSyntaxTreeWindow.ts's comment on
// the same pattern), so derive the node types structurally instead of importing them.
type SyntaxNode = ReturnType<ReturnType<typeof syntaxTree>["resolve"]>;
type Tree = ReturnType<typeof syntaxTree>;
interface FoldRange {
from: number;
to: number;
}
// `@codemirror/lang-sql`'s grammar is deliberately shallow (see its `foldNodeProp`, which only
// covers the flat `Statement` and `BlockComment` nodes): `BEGIN`/`CASE`/`END` are just `Keyword`
// leaf tokens with no enclosing block node, so there is nothing for tree-based folding to attach
// to. This foldService reconstructs block structure from the token stream instead.
//
// Scoped to `BEGIN...END` and `CASE...END` (issue #6574) -- `BEGIN TRY`/`END TRY` and
// `BEGIN CATCH`/`END CATCH` fall out of this for free, since `BEGIN` is matched regardless of a
// trailing word. `IF`/`WHILE`/`LOOP` are deliberately excluded as openers: MySQL's
// `IF(cond, a, b)` is a builtin scalar function, so treating bare `IF` as a block opener would
// misparse every such call as an unterminated block and desync all fold ranges after it. T-SQL
// (the dialect in #6574) doesn't need `IF` support anyway -- its `IF`/`ELSE` bodies are
// themselves wrapped in `BEGIN...END`, which this already folds.
const BLOCK_OPENERS = new Set(["BEGIN", "CASE"]);
// Continuation words whose `END <word>` pairing closes something this service DID push an opener
// for, so the `END` must pop the stack: `END CASE` closes a procedural `CASE` (the CASE-expression
// form closes with plain `END` instead); `END TRY`/`END CATCH` close T-SQL's `BEGIN TRY`/
// `BEGIN CATCH` -- the leading `BEGIN` there already pushed a generic entry (see BLOCK_OPENERS;
// the following `TRY`/`CATCH` word itself isn't inspected on push), so its closer must be
// recognized too or that entry is never popped and desyncs every fold range after it.
const TRACKED_END_CONTINUATIONS = new Set(["CASE", "TRY", "CATCH"]);
// `END IF`/`END WHILE`/`END LOOP` close constructs with no `BEGIN`/`CASE` prefix at all -- bare
// `IF`/`WHILE`/`LOOP` are never pushed (see BLOCK_OPENERS comment), so these `END`s must NOT pop
// the stack; nothing on it belongs to them.
const UNTRACKED_END_CONTINUATIONS = new Set(["IF", "WHILE", "LOOP"]);
// A comment is legal SQL wherever whitespace is, so "END /* note */ CASE" must still read as a
// continuation; this matches a run of pure whitespace and/or `--`/`/* */` comments so real code
// between the two words (e.g. a `;` starting an unrelated statement) still fails the check.
const GAP_IS_WHITESPACE_OR_COMMENT = /^(?:\s|--[^\n]*|\/\*[\s\S]*?\*\/)*$/;
// T-SQL transaction openers do not have a matching `END`, so they must not consume the closer
// of an enclosing procedural `BEGIN...END` block. Match complete keyword tokens only: `TRAN` is
// SQL Server's documented abbreviation, while similar identifiers such as `TRANS` remain blocks.
const SQLSERVER_TRANSACTION_BEGIN_WORDS = new Set(["TRAN", "TRANSACTION"]);
function isSqlServerTransactionBegin(state: EditorState, tokens: SyntaxNode[], index: number): boolean {
const next = tokens[index + 1];
if (!next || !GAP_IS_WHITESPACE_OR_COMMENT.test(state.sliceDoc(tokens[index].to, next.from))) return false;
const nextText = state.sliceDoc(next.from, next.to).toUpperCase();
if (SQLSERVER_TRANSACTION_BEGIN_WORDS.has(nextText)) return true;
if (nextText !== "DISTRIBUTED") return false;
const transaction = tokens[index + 2];
return Boolean(transaction && GAP_IS_WHITESPACE_OR_COMMENT.test(state.sliceDoc(next.to, transaction.from)) && state.sliceDoc(transaction.from, transaction.to).toUpperCase() === "TRANSACTION");
}
// Keyed by the `Tree` instance (not `Text`): the syntax tree is also invalidated when the SQL
// dialect is reconfigured with the document unchanged (QueryEditor.vue's databaseType/dialect
// watcher), and when Lezer's incremental parser finishes covering more of a large document in the
// background -- both produce a new `Tree` without a new `Text`, so keying on `Text` alone would
// keep serving ranges computed from a stale or partial parse.
const rangeCache = new WeakMap<Tree, Map<number, FoldRange>>();
function computeBlockFoldRanges(state: EditorState): Map<number, FoldRange> {
const tree = syntaxTree(state);
const cached = rangeCache.get(tree);
if (cached) return cached;
const byOpenerLine = new Map<number, FoldRange>();
const tokens: SyntaxNode[] = [];
tree.iterate({
enter(node) {
if (node.name === "Keyword") tokens.push(node.node);
},
});
const stack: SyntaxNode[] = [];
for (let i = 0; i < tokens.length; i++) {
const token = tokens[i];
const text = state.sliceDoc(token.from, token.to).toUpperCase();
if (text === "END") {
const next = tokens[i + 1];
const continuation = next && GAP_IS_WHITESPACE_OR_COMMENT.test(state.sliceDoc(token.to, next.from)) ? state.sliceDoc(next.from, next.to).toUpperCase() : null;
if (continuation && UNTRACKED_END_CONTINUATIONS.has(continuation)) {
i++; // "END IF"/"END WHILE"/... -- closes something we don't track; skip past it untouched
continue;
}
if (continuation && TRACKED_END_CONTINUATIONS.has(continuation)) i++; // "END CASE" -- consume the word, not a new opener
const opener = stack.pop();
if (opener) {
const openerLine = state.doc.lineAt(opener.to);
const endLine = state.doc.lineAt(token.from);
// Same source line as its opener (e.g. "BEGIN END"): nothing to collapse, and the
// from/to formula below would otherwise invert (from > to) since `from` is the end of
// that shared line while `to` is a position earlier on it.
if (openerLine.number !== endLine.number) {
byOpenerLine.set(openerLine.number, { from: openerLine.to, to: token.from });
}
}
} else if (BLOCK_OPENERS.has(text) && (text !== "BEGIN" || !isSqlServerTransactionBegin(state, tokens, i))) {
stack.push(token);
}
}
rangeCache.set(tree, byOpenerLine);
return byOpenerLine;
}
/** Folds `BEGIN...END` and `CASE...END` blocks in the SQL editor -- see module doc comment. */
export const sqlBlockFoldService = foldService.of((state, lineStart) => {
const range = computeBlockFoldRanges(state).get(state.doc.lineAt(lineStart).number);
return range ?? null;
});