Dynamic Decoration


In the different examples from the documentation, the pattern is usually to have a StateField to store the decoration ranges (for instance underlineField), a StateEffect to dispatch the changes (for instance underlineEffect) and a Decoration object (for instance underlineMark) used by the StateField to create the ranges according to the StateEffect.

This is fine when the decoration object is independent from the changes. In my case it’s not, and I was wondering if it would be ok to instantiate this object ob the fly inside the StateField::update method or if that would lead to a dangling reference.

No, that definitely works—it might just become a performance problem if you need to scan the whole document to build up the decoration set. In some cases it is preferable to use a view plugin to build decorations only for the visible viewport (see this example).

Great, thanks for your answer!