@@ -224,7 +224,7 @@ func (f *Formatter) Format(w io.Writer, style *chroma.Style, iterator chroma.Ite
224224//
225225// OTOH we need to be super careful about correct escaping...
226226func (f * Formatter ) writeHTML (w io.Writer , style * chroma.Style , tokens []chroma.Token ) (err error ) { // nolint: gocyclo
227- css := f .styleCache .get (style )
227+ css := f .styleCache .get (style , true )
228228 if f .standalone {
229229 fmt .Fprint (w , "<html>\n " )
230230 if f .Classes {
@@ -418,7 +418,7 @@ func (f *Formatter) tabWidthStyle() string {
418418
419419// WriteCSS writes CSS style definitions (without any surrounding HTML).
420420func (f * Formatter ) WriteCSS (w io.Writer , style * chroma.Style ) error {
421- css := f .styleCache .get (style )
421+ css := f .styleCache .get (style , false )
422422 // Special-case background as it is mapped to the outer ".chroma" class.
423423 if _ , err := fmt .Fprintf (w , "/* %s */ .%sbg { %s }\n " , chroma .Background , f .prefix , css [chroma .Background ]); err != nil {
424424 return err
@@ -562,11 +562,12 @@ func compressStyle(s string) string {
562562 return strings .Join (out , ";" )
563563}
564564
565- const styleCacheLimit = 16
565+ const styleCacheLimit = 32
566566
567567type styleCacheEntry struct {
568- style * chroma.Style
569- cache map [chroma.TokenType ]string
568+ style * chroma.Style
569+ compressed bool
570+ cache map [chroma.TokenType ]string
570571}
571572
572573type styleCache struct {
@@ -582,14 +583,14 @@ func newStyleCache(f *Formatter) *styleCache {
582583 return & styleCache {f : f }
583584}
584585
585- func (l * styleCache ) get (style * chroma.Style ) map [chroma.TokenType ]string {
586+ func (l * styleCache ) get (style * chroma.Style , compress bool ) map [chroma.TokenType ]string {
586587 l .mu .Lock ()
587588 defer l .mu .Unlock ()
588589
589590 // Look for an existing entry.
590591 for i := len (l .cache ) - 1 ; i >= 0 ; i -- {
591592 entry := l .cache [i ]
592- if entry .style == style {
593+ if entry .style == style && entry . compressed == compress {
593594 // Top of the cache, no need to adjust the order.
594595 if i == len (l .cache )- 1 {
595596 return entry .cache
@@ -608,13 +609,15 @@ func (l *styleCache) get(style *chroma.Style) map[chroma.TokenType]string {
608609 cached [t ] = compressStyle (style )
609610 }
610611 }
611- for t , style := range cached {
612- cached [t ] = compressStyle (style )
612+ if compress {
613+ for t , style := range cached {
614+ cached [t ] = compressStyle (style )
615+ }
613616 }
614617 // Evict the oldest entry.
615618 if len (l .cache ) >= styleCacheLimit {
616619 l .cache = l .cache [0 :copy (l .cache , l .cache [1 :])]
617620 }
618- l .cache = append (l .cache , styleCacheEntry {style : style , cache : cached })
621+ l .cache = append (l .cache , styleCacheEntry {style : style , cache : cached , compressed : compress })
619622 return cached
620623}
0 commit comments