1
0
mirror of https://github.com/alecthomas/chroma.git synced 2025-01-12 01:22:30 +02:00
chroma/coalesce.go

36 lines
780 B
Go
Raw Normal View History

2017-06-01 16:17:21 +02:00
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} }
2017-06-01 16:17:21 +02:00
type coalescer struct{ Lexer }
2017-06-01 16:17:21 +02:00
func (d *coalescer) Tokenise(options *TokeniseOptions, text string) (Iterator, error) {
var prev Token
it, err := d.Lexer.Tokenise(options, text)
if err != nil {
return nil, err
}
return func() Token {
for token := it(); token != (EOF); token = it() {
2018-03-19 01:32:36 +02:00
if len(token.Value) == 0 {
continue
}
if prev == EOF {
prev = token
} else {
if prev.Type == token.Type && len(prev.Value) < 8192 {
prev.Value += token.Value
} else {
out := prev
prev = token
return out
}
}
2017-06-01 16:17:21 +02:00
}
out := prev
prev = EOF
return out
}, nil
2017-06-01 16:17:21 +02:00
}