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.

By default a purely structural scan — every ()[]{} counts, even inside a string or comment. An optional BracketConfig makes it comment/string aware: a lightweight, line-local SkipContext lexer (NOT the highlight layer — that carries only resolved colors, no lexical scope, and runs lazily while brackets are eager and whole-document) excludes brackets inside line comments, strings, and char literals, so they are not colored, matched, folded, or indent-guided. It is opt-in and zero-cost when off. Block comments and multi-line strings are deliberately out of scope: their lexer state is non-local, which would make an edit’s cost scale with the document — the whole reason the scan stays line-local and restartable at every line boundary.

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).
BracketConfig
Language config for comment/string-aware bracket matching: which delimiters open a line comment / string / char literal, so brackets inside them are not counted. All constructs are line-local — the state resets at every \n, so block comments and multi-line strings are deliberately not handled (that would make an edit’s cost scale with the document; see the module doc). The Default (empty) config is no awareness — every bracket counts, the original purely-structural behavior, at zero added cost.
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.