1
0
mirror of https://github.com/alecthomas/chroma.git synced 2025-02-19 19:00:13 +02:00
chroma/coalesce.go
2017-06-07 19:47:59 +10:00

32 lines
603 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() {
if last != nil {
out(*last)
}
}()
return d.Lexer.Tokenise(options, text, func(token Token) {
if last == nil {
last = &token
} else {
if last.Type == token.Type {
last.Value += token.Value
} else {
out(*last)
last = &token
}
}
})
}