Get all folded sections start & end line numbers

Is there a good way to get the start and end line numbers for all folded ranges? ie, if I have lines 27 - 29 and 324 - 382 folded, I’d be able to get those 4 numbers out somehow?

Have tried a few things but markers disappear if out of viewport.

Well, definitely don’t query the DOM. You could use getAllMarks and check for those with an __isFold property (not documented but unlikely to change at this point). That’s not super cheap when there’s a lot of marks, though.

Thanks - that’s what I’m aiming for if I also grab line info from lineInfo. Re cheapness, this is as efficient as I can make it?

const allMarks = cm.getAllMarks()
if (allMarks && allMarks[0]) {
    for (let i = 0; i < allMarks.length; i++) {
        if (allMarks[i].__isFold === true) {
            const firstLine = thisCM.lineInfo(allMarks[i].lines[0]).line
            console.log((firstLine + 1) + " to " + (firstLine + allMarks[i].lines.length));

UPDATE: I assume it’s a cheaper to use getLineNumber than lineInfo if I simply want the line number?

You can just call .find on the marks, no need to mess with undocumented properties like lineInfo.

1 Like

Thank you, much appreciated.