Run markText over ranges

I am working on a mode, and in addition to styling, I want to use the result of the parsing to replace or collapse ranges using doc.markText(…).

Eg/ in my mode I could add a style, say ‘cm-widget-x’, to every contiguous region that I want to replace with ‘widget-x’.

I’m understanding the mode bit, but I haven’t yet found clarity about the best place to hook into CodeMirror to apply markText. Does anyone know where a good place to start might be?

There is no way to automatically apply markText-like effects to tokens styled by the mode. So you’ll have to listen for changes, find the text you need to style (possibly using getTokens), and call markText on them.