Best approach for state-field decorations determined by syntax

Hello!
I’m working on a math editor with extended markdown syntax.
Among others, I’ve implemented widget decorations for math in display mode (that render the math as you type it).

As the math code in display mode can span multiple lines, these decorations are provided through a state field; however, unlike the underline example provided in the docs, my decorations are determined by syntax (similar to markdown code blocks, except with $$ delimiters instead of ```).

Currently, I achieve this by iterating the entire syntax tree on every update, throwing the old widgets and recomputing the new widgets by their positions.
Of course, this feels wasteful. But more than that, on long documents - seems to happen over the 3000 character mark - this also seems to create a flickering effect, which I’m assuming is caused by the parser splitting the work into multiple chunks on large documents (I remember having read this on some discussion here, but could not find it).

Not sure if it’s relevant, but this is what the flickering looks like:
flicker
And here I edit text around the 3000 character threshold:
threshold


I’ve also thought of implementing refined diff logic between old & new decorations on every update (by looking at the changed areas rather than recomputing the decorations from scratch) but couldn’t really make it work, perhaps because I’m not familiar enough with CodeMirror.

Then, what’s the best approach for implementing such syntax based, line break replacing decorations?

As always, thanks a lot for your time and effort, and for making CodeMirror!

What the highlighter does here is, when the new syntax tree doesn’t cover the viewport yet, is to just map its old decorations through the changes and wait for more parsing to happen before it recomputes them. There may be other things about the structure of the syntax highlighter that you can steal for structuring this plugin.

Ok, cool!
I don’t think I have access to a view object, but a similar solution can be achieved using syntaxTreeAvailable(). Using that, the flickering has stopped and everything seems to be working smoothly.

Thanks again and have a great day!