From 47beea46527bf641e801857ffd6d3f0f585d824a Mon Sep 17 00:00:00 2001 From: Horizontal42 <75383519+Horizontal42@users.noreply.github.com> Date: Wed, 8 Jul 2026 14:15:04 +0300 Subject: [PATCH] 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. --- src/ui/editor.ts | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/ui/editor.ts b/src/ui/editor.ts index 247a544..72b3166 100644 --- a/src/ui/editor.ts +++ b/src/ui/editor.ts @@ -65,8 +65,11 @@ function buildDecorations(state: EditorState): DecorationSet { const highlightField = StateField.define({ create: buildDecorations, - update(_value, tr) { - return buildDecorations(tr.state); + update(value, tr) { + 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), }); @@ -98,8 +101,9 @@ function buildVarHighlight(state: EditorState): DecorationSet { const varHighlightField = StateField.define({ create: buildVarHighlight, - update(_value, tr) { - return buildVarHighlight(tr.state); + update(value, tr) { + if (tr.docChanged || tr.selection) return buildVarHighlight(tr.state); + return value; }, provide: (f) => EditorView.decorations.from(f), });