1
0
mirror of https://github.com/alecthomas/chroma.git synced 2025-11-27 22:38:42 +02:00

Synthesise meta style-entries on demand.

This includes line highlighting, numbers, etc.

Fixes #211.
This commit is contained in:
Alec Thomas
2018-12-31 11:33:36 +11:00
parent 69c9a262c3
commit 2a59c2c77e
3 changed files with 46 additions and 33 deletions

View File

@@ -247,15 +247,7 @@ func (s *Style) Builder() *StyleBuilder {
//
// This is distinct from Get() which will merge parent tokens.
func (s *Style) Has(ttype TokenType) bool {
return !s.get(ttype).IsZero()
}
func (s *Style) get(ttype TokenType) StyleEntry {
out := s.entries[ttype]
if out.IsZero() && s.parent != nil {
return s.parent.get(ttype)
}
return out
return !s.get(ttype).IsZero() || s.synthesisable(ttype)
}
// Get a style entry. Will try sub-category or category if an exact match is not found, and
@@ -268,6 +260,38 @@ func (s *Style) Get(ttype TokenType) StyleEntry {
s.get(ttype.SubCategory()))
}
func (s *Style) get(ttype TokenType) StyleEntry {
out := s.entries[ttype]
if out.IsZero() && s.synthesisable(ttype) {
out = s.synthesise(ttype)
}
if out.IsZero() && s.parent != nil {
return s.parent.get(ttype)
}
return out
}
func (s *Style) synthesise(ttype TokenType) StyleEntry {
bg := s.get(Background)
text := StyleEntry{Colour: bg.Colour}
text.Colour = text.Colour.BrightenOrDarken(0.5)
switch ttype {
// If we don't have a line highlight colour, make one that is 10% brighter/darker than the background.
case LineHighlight:
return StyleEntry{Background: bg.Background.BrightenOrDarken(0.1)}
// If we don't have line numbers, use the text colour but 20% brighter/darker
case LineNumbers, LineNumbersTable:
return text
}
return StyleEntry{}
}
func (s *Style) synthesisable(ttype TokenType) bool {
return ttype == LineHighlight || ttype == LineNumbers || ttype == LineNumbersTable
}
// ParseStyleEntry parses a Pygments style entry.
func ParseStyleEntry(entry string) (StyleEntry, error) { // nolint: gocyclo
out := StyleEntry{}