brackets

package
v1.4.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 10, 2026 License: MIT Imports: 9 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package brackets finds bracket pairs, matches them and numbers their nesting for colorization (SPEC §8.5).

An Index reads the brackets of each line of a document from its tokens (a bracket in a comment, a string or a regular expression is text), pairs them as VS Code's bracket pair parser does, and answers the questions of the editor: the pairs and brackets in a range with their nesting levels, the bracket that matches the one at a position, the pair around a position, and the closing brackets that close nothing. Languages come from their configurations ("brackets" and "colorizedBracketPairs").

Edits and new tokens invalidate the lines they touch; the pairs are made again, lazily, when the next question comes.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bracket

type Bracket struct {
	// Text is the text of the bracket, in lower case.
	Text string
	// Opening is true for an opening bracket.
	Opening bool
	// contains filtered or unexported fields
}

Bracket is one bracket text of a language, an opening or a closing one. The texts are in lower case: brackets are found without regard to case, as VS Code does. A text that two pairs use, as the closing bracket of both, is one Bracket (`endcase` closes `case` and `casez`), and a text that is the opening of one pair and the closing of another is a closing bracket.

func (*Bracket) Closes

func (b *Bracket) Closes(opening *Bracket) bool

Closes reports whether b is a closing bracket of the pair that opening begins.

func (*Bracket) ClosesColorized

func (b *Bracket) ClosesColorized(opening *Bracket) bool

ClosesColorized reports whether b closes opening in a pair whose levels are colored.

func (*Bracket) Language

func (b *Bracket) Language() *Language

Language returns the language the bracket belongs to.

func (*Bracket) Openings

func (b *Bracket) Openings() []*Bracket

Openings returns the opening brackets that b, a closing bracket, closes.

type BracketInfo

type BracketInfo struct {
	Range buffer.Range
	// Level is the number of pairs around the bracket, or one less than the number of pairs it
	// stands in when it is a closing bracket that closes nothing. EqualLevel counts the pairs
	// around it that open with the same bracket.
	Level, EqualLevel int
	// Invalid is true for an opening bracket that is never closed and for a closing bracket that
	// closes nothing.
	Invalid bool
	Bracket *Bracket
}

BracketInfo is a bracket of the document (VS Code's BracketInfo).

type Edited

type Edited struct {
	From, To, Count int
}

Edited tells that the text changed: each change replaced the old lines from..to with count new ones, the changes in order of position and relative to the text before the batch.

type Found

type Found struct {
	Range   buffer.Range
	Bracket *Bracket
}

Found is a bracket found by Prev and Next.

type Index

type Index struct {
	// contains filtered or unexported fields
}

Index finds the bracket pairs of a document and answers questions about them (SPEC §8.5). It reads the brackets of a line from its tokens (the brackets in comments, strings and regular expressions are not brackets), keeps them per line, forgets the lines an edit or a new token touches, and pairs all the brackets again when it is asked something. VS Code keeps a tree of the pairs and reuses the parts an edit did not touch; here the work that costs, finding the brackets of a line, is kept per line and the pairing is one pass over the brackets.

An Index is safe for use from several goroutines: the tokenizer reports new tokens from its own.

func Attach

func Attach(doc *document.Document, tok *tokens.Tokenizer, langs Languages, languageCode int) (*Index, func())

Attach makes the index of a document with the tokens of tok (which may be nil for a document with no grammar: every line is then code in the language of the document). The index follows the document and the tokenizer until the function it returns is called. A document in large file mode has an index that finds nothing.

func New

func New(src Source, langs Languages, enabled bool) *Index

New makes the index of a source. A disabled index (a document in large file mode) finds nothing.

func (*Index) Apply

func (ix *Index) Apply(changes []Edited)

Apply tells the index of a batch of changes and invalidates the lines they touch. The new line count tells whether the index kept up with the text; if it did not, it starts again.

func (*Index) Brackets

func (ix *Index) Brackets(r buffer.Range, onlyColorized bool) []BracketInfo

Brackets returns the brackets that touch the range, in order. With onlyColorized, the brackets of the pairs whose levels have no colors (`<` `>` unless the configuration says otherwise) are left out, and the levels count only the pairs that have colors.

func (*Index) Complete

func (ix *Index) Complete() bool

Complete reports whether the tokens were complete when the pairs were last made.

func (*Index) Enabled

func (ix *Index) Enabled() bool

Enabled tells whether the index looks for brackets.

func (*Index) Enclosing

func (ix *Index) Enclosing(pos buffer.Position) (open, closing buffer.Range, ok bool)

Enclosing returns the brackets of the innermost closed pair that has the position strictly inside, or false.

func (*Index) HasUnmatchedClosingAfter

func (ix *Index) HasUnmatchedClosingAfter(pos buffer.Position, opening string) bool

HasUnmatchedClosingAfter reports whether a closing bracket that closes nothing and that would close the opening bracket text, in the language at the position, starts at or after the position. It answers false while the tokens are not complete: a closing bracket in a comment that is not tokenized yet is not one.

func (*Index) Invalidate

func (ix *Index) Invalidate(from, to int)

Invalidate tells that the lines from to to (zero-based, inclusive) have other tokens or text.

New tokens of lines whose pairs are made usually leave their brackets as they were: the brackets of a line are found in its text when it has no tokens yet, and the tokens only take away those in strings and comments. Making the pairs again of a whole document (10 ms for 100,000 lines) for every slice of the tokenizer made each frame of a scroll cost that much (step 10.1). So the lines are only noted here, from any goroutine (the document is not read: it belongs to the UI thread), and the next question finds their brackets again and makes the pairs only when some are not the same.

func (*Index) Match

func (ix *Index) Match(pos buffer.Position) (open, closing buffer.Range, ok bool)

Match returns the two brackets of the pair the position is at (in or next to a bracket of a closed pair), or false. Where two pairs touch the position, the one whose bracket starts later wins.

func (*Index) MatchingUp

func (ix *Index) MatchingUp(text string, pos buffer.Position) (buffer.Range, bool)

MatchingUp returns the opening bracket of the innermost pair around the position that the closing bracket text closes, in the language at the position, or false. It finds the bracket even when the pair is not closed.

func (*Index) Next

func (ix *Index) Next(pos buffer.Position) (Found, bool)

Next returns the first bracket that ends after the position (a closing bracket that closes nothing is not one), or false.

func (*Index) OnChange

func (ix *Index) OnChange(fn func(from int)) (unsubscribe func())

OnChange calls fn with the first line from which brackets may have changed, after an edit or new tokens. Pairs that start earlier can end later, so every line below can have other brackets and levels. It gives a function that stops the calls.

func (*Index) Pairs

func (ix *Index) Pairs(r buffer.Range) []PairInfo

Pairs returns the bracket pairs that touch the range (they may share only an end with it), in the order of their opening brackets.

func (*Index) Prev

func (ix *Index) Prev(pos buffer.Position) (Found, bool)

Prev returns the last bracket that starts before the position (a closing bracket that closes nothing is not one), or false.

func (*Index) Reset

func (ix *Index) Reset()

Reset forgets everything, as when the text is replaced or the language changes.

func (*Index) Splice

func (ix *Index) Splice(from, to, count int)

Splice tells that the lines from to to (zero-based, inclusive) were replaced by count new lines.

type Language

type Language struct {
	// contains filtered or unexported fields
}

Language holds the brackets of one language (SPEC §8.5). It is built from a language configuration and is read-only afterwards, so it can be shared.

func FromConfiguration

func FromConfiguration(code int, c *language.Configuration) *Language

FromConfiguration makes the brackets of the language with the given code from its language configuration, which may be nil.

func NewLanguage

func NewLanguage(code int, brackets, colorized []Pair, colorizedDefined bool) *Language

NewLanguage makes the brackets of the language with the given code from the pairs of "brackets" and "colorizedBracketPairs" of its configuration. The colorized pairs are brackets too. When colorizedDefined is false the pairs of "brackets" are colorized, except `<` `>`, which many languages use as a comparison.

func (*Language) Brackets

func (l *Language) Brackets() []*Bracket

Brackets returns all the brackets of the language, in the order in which they are searched for.

func (*Language) Code

func (l *Language) Code() int

Code returns the code of the language.

func (*Language) Empty

func (l *Language) Empty() bool

Empty reports whether the language has no brackets.

func (*Language) Lookup

func (l *Language) Lookup(text string) *Bracket

Lookup returns the bracket with the text (in any case), or nil.

func (*Language) Strip

func (l *Language) Strip(text string) string

Strip returns text without the brackets of the language, found as in a token: the edges of the text count as the edges of a word (VS Code's replace of the global bracket regular expression by nothing).

type Languages

type Languages interface {
	Language(code int) *Language
}

Languages gives the brackets of a language by its code. It must return the same *Language for a code every time, as brackets are told apart by identity.

type Pair

type Pair = language.Pair

Pair is a pair of bracket texts, as a language configuration lists them.

type PairInfo

type PairInfo struct {
	// Range goes from the start of the opening bracket to the end of the closing bracket; when
	// the pair has no closing bracket, to the end of what it holds.
	Range buffer.Range
	// Open is the range of the opening bracket.
	Open buffer.Range
	// Close is the range of the closing bracket, when HasClose is true.
	Close    buffer.Range
	HasClose bool
	// Level is the number of pairs around this one; EqualLevel counts those among them that open
	// with the same bracket.
	Level, EqualLevel int
	// OpenBracket and CloseBracket are the brackets as the language knows them; CloseBracket is
	// nil when the pair is not closed.
	OpenBracket, CloseBracket *Bracket
}

PairInfo is a bracket pair (VS Code's BracketPairInfo).

type RegistryLanguages

type RegistryLanguages struct {
	// contains filtered or unexported fields
}

RegistryLanguages gives the brackets of the languages of a language registry. It makes the brackets of a language once, so a call with the same code gives the same *Language.

func NewRegistryLanguages

func NewRegistryLanguages(reg *language.Registry) *RegistryLanguages

NewRegistryLanguages makes a Languages over a registry. When the language configurations change, make a new one and Reset the indexes.

func (*RegistryLanguages) Language

func (r *RegistryLanguages) Language(code int) *Language

Language implements Languages.

type Source

type Source interface {
	// LineCount is the number of lines (at least one).
	LineCount() int
	// LineContent is the text of a line without its line break.
	LineContent(line int) string
	// LineTokens are the tokens of a line, or nil when the line has none yet; brackets are then
	// looked for in the whole line as if it were code in the language of the document.
	LineTokens(line int) []tokens.Token
	// Language is the code of the language of the document.
	Language() int
	// TokensComplete reports whether every line has its tokens (or the document has no
	// tokenizer), so that what the Index finds is final.
	TokensComplete() bool
}

Source is the text and the tokens that an Index reads. Lines count from zero and the columns are byte offsets.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL