Maintaining decorations after a replace

Hello – thank you for this library: it works well, and learning how to write an extension has been very fun and pleasant.

This all functions fine for me so far – but I’m trying to add a new feature:

Is there a way to maintain these Decorations after this replace? It seems tough, since the full-text replace could be a complete rewrite of the content, which would render the Decorations meaningless – but I’m curious if you see a good solution that lets me keep the source of truth outside of CodeMirror. (If not, I can move it into CodeMirror, just not ideal for other parts of app.)

Update: I found a decent workaround: I’m inserting magic keywords into the actual CSS text that signal the start and end of a CSS color (e.g. /* start_color */#abcdef/* end_color */), and using a view plugin to replace that text – including magic keywords – with a widget. Magic keywords are preserved across full-text replaces, so things work as expected.

I’d love to get rid of these magic keywords and use internal state if possible, but I get that I might be asking CodeMirror to infer too much.

Indeed, mapping positions (for obvious reasons) doesn’t work through full-text replaces. You could try to set up your data model to propagate patches instead of full documents, or retroactively diff the current and new value to try and guess the change that happened, and then apply only that.

1 Like

Thank you for the advice, that makes sense. I’ll continue with keeping the “magic keywords” in the text, as that is working well.