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 ¶
- type Bracket
- type BracketInfo
- type Edited
- type Found
- type Index
- func (ix *Index) Apply(changes []Edited)
- func (ix *Index) Brackets(r buffer.Range, onlyColorized bool) []BracketInfo
- func (ix *Index) Complete() bool
- func (ix *Index) Enabled() bool
- func (ix *Index) Enclosing(pos buffer.Position) (open, closing buffer.Range, ok bool)
- func (ix *Index) HasUnmatchedClosingAfter(pos buffer.Position, opening string) bool
- func (ix *Index) Invalidate(from, to int)
- func (ix *Index) Match(pos buffer.Position) (open, closing buffer.Range, ok bool)
- func (ix *Index) MatchingUp(text string, pos buffer.Position) (buffer.Range, bool)
- func (ix *Index) Next(pos buffer.Position) (Found, bool)
- func (ix *Index) OnChange(fn func(from int)) (unsubscribe func())
- func (ix *Index) Pairs(r buffer.Range) []PairInfo
- func (ix *Index) Prev(pos buffer.Position) (Found, bool)
- func (ix *Index) Reset()
- func (ix *Index) Splice(from, to, count int)
- type Language
- type Languages
- type Pair
- type PairInfo
- type RegistryLanguages
- type Source
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 ¶
Closes reports whether b is a closing bracket of the pair that opening begins.
func (*Bracket) ClosesColorized ¶
ClosesColorized reports whether b closes opening in a pair whose levels are colored.
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 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 ¶
New makes the index of a source. A disabled index (a document in large file mode) finds nothing.
func (*Index) Apply ¶
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 ¶
Complete reports whether the tokens were complete when the pairs were last made.
func (*Index) Enclosing ¶
Enclosing returns the brackets of the innermost closed pair that has the position strictly inside, or false.
func (*Index) HasUnmatchedClosingAfter ¶
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 ¶
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 ¶
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 ¶
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 ¶
Next returns the first bracket that ends after the position (a closing bracket that closes nothing is not one), or false.
func (*Index) OnChange ¶
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 ¶
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 ¶
Prev returns the last bracket that starts before the position (a closing bracket that closes nothing is not one), or false.
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 ¶
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 ¶
Brackets returns all the brackets of the language, in the order in which they are searched for.
type Languages ¶
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 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.