Custom uncollapse behavior in codemirror/merge

Hello! I’ve recently started using CodeMirror + @codemirror/merge, which have been awesome. One thing I’ve been struggling with however is overriding the default collapsing behavior in the merge library.

Right now, a 90 unchanged lines button renders over collapsed, unchanged areas. When I click that text, it uncollapses the entire 90 lines. I want to implement a partial uncollapse feature (similar to GitHub), where I can click an up or down arrow to incrementally uncollapse 20 lines in either direction.

I was able to mostly do this by building my own extension, but keep running into bugs that are testing my knowledge of CodeMirror. One particular issue is getting the collapse state to sync across two editors when in split view.

I’m curious if either a) this is something CodeMirror would consider supporting natively or b) anyone might have any tips on the best way to implement. Many thanks!

I don’t really want to complicate the implementation in @codemirror/merge to support partial uncollapsing (especially since uncollapseEffect is exported, and would have to change to support this).

Would having an exported way to find the sibling of a given editor help with the issue of syncing across split view editors?

Totally makes sense. Yes, an exported way to find the sibling of an editor would solve the split view syncing problem on my end. Thank you so much in advance!

See this patch:

Thanks! Unfortunately, this always seems to throw the error:

[Error] TypeError: Right side of assignment cannot be destructured
mergeViewSiblings (@codemirror_merge.js:856)
expandWithSibling (incrementalCollapse.tsx:82)
(anonymous function) (incrementalCollapse.tsx:127)

FWIW, I’m using @uiw/react-codemirror, though I don’t believe that would make a difference. One other issue I’m running into is actually tracking which collapsed sections “match”. For example, if I add a line in the middle of a file, the uncollapsed sections now have different line numbers in the two files.

I realize these are both hand-wavy problems, as I haven’t had a ton of time to invest in making this work. Hoping I can revisit it in the coming days and provide more details.

It should only do that in an editor without merge extension active. But yeah, it would be better if it just returned null then. See this patch.