Clearer bracket matching

I’d like to improve the behaviour of the bracket matching, because it’s hard to see which bracket is being matched for adjacent brackets.

Currently it does this:

And I’d like it to do this:

Here’s how I did it:

--- language.js	2025-03-22 13:35:51.620309378 +0200
+++ language.js.bak	2025-03-22 13:36:15.986383576 +0200
@@ -1839,6 +1839,7 @@
         return combineConfig(configs, {
             afterCursor: true,
             brackets: DefaultBrackets,
+            directional: true,
             maxScanDistance: DefaultScanDist,
             renderMatch: defaultRenderMatch
         });
@@ -1861,15 +1862,29 @@
         let decorations = [];
         let config = tr.state.facet(bracketMatchingConfig);
         for (let range of tr.state.selection.ranges) {
+            let match
             if (!range.empty)
                 continue;
-            let match = matchBrackets(tr.state, range.head, -1, config)
-                || (range.head > 0 && matchBrackets(tr.state, range.head - 1, 1, config))
-                || (config.afterCursor &&
-                    (matchBrackets(tr.state, range.head, 1, config) ||
-                        (range.head < tr.state.doc.length && matchBrackets(tr.state, range.head + 1, -1, config))));
-            if (match)
-                decorations = decorations.concat(config.renderMatch(match, tr.state));
+            if (config.directional) {
+                // Only try backward for char at cursor, only try forward for char after cursor.
+                // This limits the highlight to closing brackets before the cursor and opening
+                // brackets after.
+                match = matchBrackets(tr.state, range.head, -1, config)
+                if (match)
+                    decorations = decorations.concat(config.renderMatch(match, tr.state));
+                match = matchBrackets(tr.state, range.head, 1, config)
+                if (match)
+                    decorations = decorations.concat(config.renderMatch(match, tr.state));
+            }
+            else {
+                match = matchBrackets(tr.state, range.head, -1, config)
+                    || (range.head > 0 && matchBrackets(tr.state, range.head - 1, 1, config))
+                    || (config.afterCursor &&
+                        (matchBrackets(tr.state, range.head, 1, config) ||
+                            (range.head < tr.state.doc.length && matchBrackets(tr.state, range.head + 1, -1, config))));
+                if (match)
+                    decorations = decorations.concat(config.renderMatch(match, tr.state));
+            }
         }
         return Decoration.set(decorations, true);
     },
@@ -1914,7 +1929,7 @@
 }
 /**
 Find the matching bracket for the token at `pos`, scanning
-direction `dir`. Only the `brackets` and `maxScanDistance`
+direction `dir`. Only the `brackets` and `maxScanDistance` FIX
 properties are used from `config`, if given. Returns null if no
 bracket was found at `pos`, or a match result otherwise.
 */

Is this something that could go in the main code, or should I make a package?
It could be an option like directional that overrides afterCursor.

Solved with an extension, in case it helps anyone: https://www.npmjs.com/package/@cookshack/codemirror-bracket-matching

Package can also highlight enclosing brackets. (Innermost brackets are highlighted when the cursor is anywhere inside them.)