Extending bracket matching to look for the closest matching brackets

At Wikipedia, we had a customized fork of the bracket matching add-on for CodeMirror 5 that would highlight surrounding brackets, and not just when the cursor is next to a bracket. This is especially useful for MediaWiki wikitext because that language has lots of inline syntax, so the contextual highlighting really helps.

Anyway, I’d like to do the same in CodeMirror 6 but I’m not sure how to extend the native bracket matching. Basically, I’d like to provide my own implementation of matchBrackets() which would call the native one, and if null is returned (cursor is not a bracket) then it would it search for surrounding brackets and call matchBrackets() on those so that they are highlighted.

What would be the recommended way of doing this, if any, aside from having to rewrite all of the bracket matching code?

I also wanted to ask if there was any appetite to perhaps add an option for this in the bracket matching config. I know some IDEs have this behaviour by default, seemingly most do not, but nonetheless I assume its usefulness extends beyond Wikipedia.

Thanks for your time!

The @codemirror/language package exports a matchBrackets function that will do most of the scanning for you. You’d only have to reimplement the part that finds the closest surrounding bracket, and creates decorations for this. That seems like it can be reasonably done as a separate extension.