[Folding] Show number of lines / elements folded

First, I want to say thank you so much for this incredible library :pray:

My problem is the customization of the folding. Inside codeFoldingconfigplaceholderText or placeholderDOM⁠ I want to display the number of collapsed lines or for example the number of collapsed elements inside an array.

Here’s an img for an example:
image

Can you pls tell me in which direction I should look in the docs to find how to do this, thank you!

Another example:

const users = [
	{
		id: 10,
		name: "User1",
	},
	{
		id: 11,
		name: "User2",
	},
	{
		id: 12,
		name: "User3",
	},
]

folded result:

const users = [<3 items>]

It’s not something that the library supports. If you really need this you’ll have to fork (part of) @codemirror/language/src/fold.ts to implement your own custom folding system.

@marijn would you be open to a PR that implements this functionality? was thinking of updating placeholderText to also accept a function that accepts a single parameter, range ({from, to}), and returns a string.

placeholderDOM/Text are currently called from a place that knows nothing about the folded range, so this is definitely not a matter of simply adding a parameter. It’d need to overhaul the way the widgets used by this code work.

1 Like

gotcha - didn’t realize that it wasn’t easily accessible. makes sense

I’ve found a not-too-cumbersome way to include this in the existing framework. Does this patch look like it’d provide what you need here?

2 Likes

yeah that looks like it would work! just curious why add a new option instead of just passing down the to/from range into the placeholder directly (e.g. in place of the prepare param)

The DOM creation function is called when the widget is rendered. The new function is called when the widget is created, and can be used for multiple renders (for example, when the widget is scrolled out of the viewport and then scrolled back into it). At the time where the widget is rendered, the original folded range may not match the current range for that fold anymore (and widgets don’t know their position, so, as I mentioned before, that information just isn’t available in the code that calls placeholderDOM⁠).

2 Likes