Skip to content

Commit bd47355

Browse files
committed
fix: include compress state in style cache key
Fixes #945
1 parent 1235bbf commit bd47355

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

formatters/html/html.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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...
226226
func (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).
420420
func (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

567567
type 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

572573
type 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
}

formatters/html/html_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ func TestTableLinkeableLineNumbers(t *testing.T) {
222222

223223
assert.Contains(t, buf.String(), `id="line1"><a class="lnlinks" href="#line1">1</a>`)
224224
assert.Contains(t, buf.String(), `id="line5"><a class="lnlinks" href="#line5">5</a>`)
225-
assert.Contains(t, buf.String(), `/* LineLink */ .chroma .lnlinks { outline:none;text-decoration:none;color:inherit }`, buf.String())
225+
assert.Contains(t, buf.String(), `/* LineLink */ .chroma .lnlinks { outline: none; text-decoration: none; color: inherit }`, buf.String())
226226
}
227227

228228
func TestTableLineNumberSpacing(t *testing.T) {

0 commit comments

Comments
 (0)