diff options
| author | _ov4 <ov4@ov4.org> | 2026-10-13 06:02:56 +0000 |
|---|---|---|
| committer | _ov4 <ov4@ov4.org> | 2026-10-13 06:11:36 +0000 |
| commit | 9ec4fbddd559d93b3d6b072dc8f56d2a572585bd (patch) | |
| tree | ac576c0da3b936ca6198b7f84944c76727a89c6b /plugin/src/brackets.js | |
| parent | e52266d8ce8823866b2dad3e04ffc78438e08397 (diff) | |
feat(plugin): color bracket pairs and mark unmatched brackets red
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Diffstat (limited to 'plugin/src/brackets.js')
| -rw-r--r-- | plugin/src/brackets.js | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/plugin/src/brackets.js b/plugin/src/brackets.js new file mode 100644 index 0000000..ad79ea5 --- /dev/null +++ b/plugin/src/brackets.js @@ -0,0 +1,25 @@ +'use strict'; + +const { codeOnly } = require('./lexical'); +const pairs = { ')': '(', ']': '[' }; + +function unmatchedBrackets(text) { + const code = codeOnly(text); + const stack = []; + const unmatched = []; + for (let offset = 0; offset < code.length; offset++) { + const char = code[offset]; + if (char === '(' || char === '[') { + stack.push({ char, offset }); + } else if (pairs[char]) { + const opening = stack.pop(); + if (!opening || opening.char !== pairs[char]) { + if (opening) unmatched.push(opening.offset); + unmatched.push(offset); + } + } + } + return unmatched.concat(stack.map(opening => opening.offset)).sort((a, b) => a - b); +} + +module.exports = { unmatchedBrackets }; |
