avoid rebuilding decoration fields on every editor transaction

highlightField and varHighlightField rebuilt from scratch on every
CodeMirror transaction, including ones that touch neither the doc nor
the selection (e.g. the setResults effect firing after evaluation).
varHighlightField now only rebuilds when the doc or selection actually
changed; highlightField remaps existing decorations through doc edits
and only does a full rebuild when new results arrive.
This commit is contained in:
Horizontal42 2026-07-08 14:15:04 +03:00
parent 00bd1248e1
commit 47beea4652

View file

@ -65,8 +65,11 @@ function buildDecorations(state: EditorState): DecorationSet {
const highlightField = StateField.define<DecorationSet>({ const highlightField = StateField.define<DecorationSet>({
create: buildDecorations, create: buildDecorations,
update(_value, tr) { update(value, tr) {
return buildDecorations(tr.state); const hasResults = tr.effects.some((e) => e.is(setResults));
if (hasResults) return buildDecorations(tr.state);
if (tr.docChanged) return value.map(tr.changes);
return value;
}, },
provide: (f) => EditorView.decorations.from(f), provide: (f) => EditorView.decorations.from(f),
}); });
@ -98,8 +101,9 @@ function buildVarHighlight(state: EditorState): DecorationSet {
const varHighlightField = StateField.define<DecorationSet>({ const varHighlightField = StateField.define<DecorationSet>({
create: buildVarHighlight, create: buildVarHighlight,
update(_value, tr) { update(value, tr) {
return buildVarHighlight(tr.state); if (tr.docChanged || tr.selection) return buildVarHighlight(tr.state);
return value;
}, },
provide: (f) => EditorView.decorations.from(f), provide: (f) => EditorView.decorations.from(f),
}); });