CM6 Fold API - any way to get the current folded regions

I’m working with @codemirror/fold right now and I have not found a good way to request for the currently folded regions.

The use case I’m going after is to persist the fold status by line number in hopes of restoring it in a later writing session. This could be done previously on CM5 with getAllMarks() and querying the (undocumented) __isFold property on a Mark object.

So the question is: would it make sense to add some kind of API to get all currently folded regions, or should I be looking into writing a custom fold plugin?

PS. It might also make sense to add an API for querying whether a single line is folded, which could be useful for, say, implementing a toggleFold command.

Ok, in addition to that, I think it may also make sense to have a way to programmatically fold or unfold regions (such as in the case of “loading” a previously persisted folding state), rather than having to set a selection and call foldCode, before restoring the previous selection.

This is currently being done by the unfoldAll function.

The problem you will run into is the fact that the state field is not exported.

Thank you. The problem is exactly that the state field is not exported, though marijn may know a better/cleaner way to expose the functionality. It would also require to export foldEffect and unfoldEffect for programmatic access to folding specific ranges.

This patch exports some more functionality for this. @codemirror/fold 0.18.1 includes those changes.

2 Likes