Skip to content

feat(formatter): add writeCSSComments option #1112

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 25, 2025

Conversation

JockeM
Copy link
Contributor

@JockeM JockeM commented Jul 25, 2025

Currently when writing the css file comments before each class is present.

/* Background */ .bg { color: #cdd6f4; background-color: #1e1e2e;-moz-tab-size: 2; -o-tab-size: 2; tab-size: 2; }
/* PreWrapper */ .chroma { color: #cdd6f4; background-color: #1e1e2e;-moz-tab-size: 2; -o-tab-size: 2; tab-size: 2; }
/* Error */ .chroma .err { color: #f38ba8 }
/* LineLink */ .chroma .lnlinks { outline: none; text-decoration: none; color: inherit }
/* LineTableTD */ .chroma .lntd { vertical-align: top; padding: 0; margin: 0; border: 0; }
/* LineTable */ .chroma .lntable { border-spacing: 0; padding: 0; margin: 0; border: 0; }
/* LineHighlight */ .chroma .hl { background-color: #45475a }
...

(Also why is there a .bg class, feels like that could collide)

This is probably a micro-optimization but felt like the comments didn't serve any purpose except clarity and debug value.

Also skipped writing empty css classes.

Before and after writing css file for style "catppuccin-mocha"

> ls -l -B
.rw-r--r-- 4,934 jocke 25 Jul 01:51 with.css
.rw-r--r-- 3,337 jocke 25 Jul 01:50 without.css

I didn't wanna make any changes to the output without changes to the options.

Maybe we could just remove the comments all together and skip this extra logic.

@@ -419,26 +424,47 @@ func (f *Formatter) tabWidthStyle() string {
// WriteCSS writes CSS style definitions (without any surrounding HTML).
func (f *Formatter) WriteCSS(w io.Writer, style *chroma.Style) error {
css := f.styleCache.get(style, false)

// Helper to write a CSS rule if styles is non-empty
writeRule := func(comment string, selector string, styles string) error {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be a method?

Copy link
Owner

@alecthomas alecthomas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems reasonable, thanks!

@alecthomas
Copy link
Owner

I'm honestly no sure why .bg is global, it seems likely that it was just an omission.

@alecthomas alecthomas merged commit 5b2a4c5 into alecthomas:master Jul 25, 2025
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants