How to render comment whenever a comment is closed


#1

Hello, I’m looking into a way to render the comment content inside the code whenever a comment is closed. I figured that I can use doc.markText with replacedWith to render it. But I’m not sure how can I know the range of the comment with CodeMirror API ?

From what I can tell from the manual, there is cm.getLineTokens to scan and make a map of all comments. And rescan again on change event.

If there is a better to do this, I would love to know. Thank you very much.


#2

Scanning getLineTokens is a reasonable approach. Redoing it for the whole document on every change is likely to get expensive in big documents. You could listen to "changes" events and incrementally update your data structure, though that’s a little more involved.


#3

Thank you very much for your quick response. I’ll look into this


#4

If what you need is a cheap synchronous transform, maybe do it directly in renderLine hook.
Besides being probably simpler than subscribing to changes, this avoids work for lines outside rendered viewport.
Note that you must not modify the editor state during renderLine.