Skip to main content

Module bracket

Module bracket 

Source
Expand description

Bracket matching — the shared structural pass behind bracket-pair colorization, matching-bracket highlight, indent guides, and folding. ()/[]/{} pair by a stack automaton (quotes are not nestable), yielding each bracket’s nesting depth and matched-partner offset and flagging the unmatched.

Matches all brackets regardless of string or comment context: this is a purely structural scan with no lexer state, so a ) inside a string still counts as a bracket. Excluding string/comment brackets would need a scope class threaded through from the highlight layer, which carries only a resolved color, not lexical context.

Brackets is a thin façade over one SumTree<BracketItem> (see bracket_tree) that stores only PRIMARY facts — each bracket’s char and delta-gap position. Every derived fact a caller reads — open, depth, partner, “what pair encloses this” — is recomputed by an O(log) cursor descent, so no cached copy can drift, and an edit is a pure SumTree::replace of the edited byte range with the rescanned brackets. Because no partner offset is stored, there is nothing to repair across the edit boundary — the tree is always internally consistent by construction. Brackets::match_text is the O(n) load-time constructor (and the tests’ oracle); Brackets::apply_edit rides one committed patch through the same tree, on the forward path and on every undo/redo step alike.

Structs§

Bracket
One bracket occurrence in the document — fully derived on demand (nothing here is stored; the tree holds only the char and position).
Brackets
Every bracket in a document, riding one augmented SumTree that is the sole owner of position. Queries are O(log) descents; an edit is a SumTree::replace of the edited byte range.