Skip to content

Highlight matching brackets in the REPL input - #35

Merged
okcodes merged 4 commits into
mainfrom
feature/repl-bracket-matching
Sep 6, 2026
Merged

okcodes merged 4 commits into
mainfrom
feature/repl-bracket-matching

Conversation

@okcodes

@okcodes okcodes commented Sep 6, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Adds DevTools-style bracket-pair highlighting to the REPL's live input: the bracket next to the caret and its match get a subtle background + underline, matching Chrome's console behavior.
  • The tie-break for tightly-packed brackets (}] with no space) follows Chrome/CodeMirror's convention: the character immediately behind the caret wins over the one in front. With whitespace around brackets, this never matters since only one side ever touches a bracket character.
  • New findMatchingBrackets() (src/runtime/bracket-matcher.ts) reuses the existing tokenizer's punctuation stream with a stack, so brackets inside strings/comments are never candidates, and mismatched closers are left unpaired instead of being force-matched.
  • highlightCode() now takes an optional matched-pair offset tuple and wraps just those two punctuation tokens in a .vc-bracket-match span.
  • The REPL wires this up inside the existing debounced highlight update (reading live caret/selection state at execution time, not values captured when the event fired), and adds click/keyup/select listeners so pure caret movement - not just typing - refreshes the highlight.
  • No highlight is shown while there's an active text selection.

Test plan

  • pnpm test - all 116 tests pass, including 10 new bracket-matcher.test.ts cases (simple pairs from all four caret sides, the tight-nesting tie-break, whitespace-separated matching from either side, unmatched/mismatched brackets, brackets inside strings, empty input)
  • pnpm typecheck - clean
  • pnpm lint - clean (one pre-existing, unrelated warning in tokenizer.test.ts)
  • Manually verified in the local example app (examples/local/react-vite-plugin): typed [{ONE:{a:1},TWO:[1,2]}] into the REPL and inspected the rendered backdrop HTML at several caret positions to confirm the correct pair highlights, confirmed the whitespace case matches from either side, and confirmed no highlight while text is selected

🤖 Generated with Claude Code

okcodes and others added 4 commits September 6, 2026 08:23
Adds DevTools-style bracket-pair highlighting to the REPL's live input:
the bracket adjacent to the caret and its match get a subtle
background plus underline, updated as the caret moves (not just as
text changes).

findMatchingBrackets() (bracket-matcher.ts) resolves which bracket is
"active" for the caret the same way Chrome/CodeMirror do - checking
the character immediately behind the caret before the one in front -
so back-to-back brackets with no whitespace (`}]`) resolve
unambiguously, while whitespace-separated brackets match from either
side since only one side is ever touching a bracket character.
Matching itself walks the existing tokenizer's punctuation stream with
a stack, so brackets inside strings/comments are never candidates and
mismatched closers are left unpaired rather than mispairing.

highlightCode() takes an optional matched-pair offset tuple and wraps
just those two punctuation tokens; the REPL wires it up by reading
live caret/selection state (not values captured at event time) inside
the existing debounced highlight update, and adds click/keyup/select
listeners so pure caret movement (not just typing) also refreshes it.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@okcodes
okcodes merged commit 6a9bfe6 into main Sep 6, 2026
2 checks passed
@okcodes
okcodes deleted the feature/repl-bracket-matching branch September 6, 2026 14:29

This branch was previously deployed

1 inactive deployment
github-pages — 916edfcf Deployed Sep 6, 2026 by okcodes via deploy-website #7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant