mirror of
https://github.com/alecthomas/chroma.git
synced 2025-01-12 01:22:30 +02:00
28 lines
600 B
Go
28 lines
600 B
Go
package chroma
|
|
|
|
// Coalesce is a Lexer interceptor that collapses runs of common types into a single token.
|
|
func Coalesce(lexer Lexer) Lexer {
|
|
return &coalescer{lexer}
|
|
}
|
|
|
|
type coalescer struct {
|
|
Lexer
|
|
}
|
|
|
|
func (d *coalescer) Tokenise(options *TokeniseOptions, text string, out func(*Token)) error {
|
|
var last *Token
|
|
defer func() { out(last) }()
|
|
return d.Lexer.Tokenise(options, text, func(token *Token) {
|
|
if last == nil {
|
|
last = token
|
|
} else {
|
|
if last.Type == token.Type && len(last.Value) < 8192 {
|
|
last.Value += token.Value
|
|
} else {
|
|
out(last)
|
|
last = token
|
|
}
|
|
}
|
|
})
|
|
}
|