Files
openidea/src/test/syntax.ts
T
Arnaud Ritti 780e57d0c5 fix(test): complete syntax parsing across language helpers (#2042)
Headless language tests could read an earlier, partial syntax tree after ensureSyntaxTree finished parsing, so highlight and state-based assertions depended on the initial 20 ms parse budget and failed intermittently under load. Shared test helpers now return the completed tree (or publish it to state), and every affected test uses them.

Co-authored-by: arnaud-ritti <77437157+arnaud-ritti@users.noreply.github.com>
2026-11-08 02:45:26 +08:00

18 lines
783 B
TypeScript

import { ensureSyntaxTree, language, syntaxTree } from "@codemirror/language";
import type { EditorState } from "@codemirror/state";
export function completedSyntaxTree(state: EditorState, timeout = 1000) {
const tree = ensureSyntaxTree(state, state.doc.length, timeout);
if (!tree) throw new Error("Test syntax parsing did not finish");
return tree;
}
export function completedSyntaxState(state: EditorState, timeout = 5000) {
if (!state.facet(language)) return state;
const tree = completedSyntaxTree(state, timeout);
// A transaction snapshots the completed parse context into the immutable state.
const parsed = state.update({}).state;
if (syntaxTree(parsed) !== tree) throw new Error("Test state did not publish the completed syntax tree");
return parsed;
}