trishume / syntect

Rust library for syntax highlighting using Sublime Text syntax definitions.
https://docs.rs/syntect
MIT License
1.85k stars 130 forks source link

Rust: Weird rendering of simple println!() #532

Closed Arrekin closed 3 months ago

Arrekin commented 3 months ago

Hey, I wanted to add to my page written using Sycamore code blocks with syntax coloring. Generally, everything worked well until I tried to add snippet containing the following line: println!("{}", best_value);

The HTML result shows like this(note the "{}" in the center of the aberration): image

The generated spans look like this:

<span class="source rust">
    <span class="support macro rust">println!</span>
    <span class="meta group rust">
        <span class="punctuation section group begin rust">(</span>
    </span>
    <span class="meta group rust">
        <span class="string quoted double rust">
            <span class="punctuation definition string begin rust">&quot;</span>
            <span class="constant other placeholder rust">{}</span>
            <span class="punctuation definition string end rust">&quot;</span>
        </span>
    </span>
    <span class="meta group rust">
         <span class="punctuation separator rust">,</span>
         best_value
         <span class="punctuation section group end rust">)</span>
     </span>
     <span class="punctuation terminator rust">;</span>
</span>

Lib version: syntect = { version = "5.2.0", features = ["default-fancy", "default-themes"], default-features = false } CSS auto-generated theme: base16-mocha.dark System: Run inside WSL(Ubuntu) on Windows 10 Rust Version: rustc 1.77.2 (nightly)

I tested a few cases. println!("something"); - works fine println!("{0}", best_value) - the same issue, shows {0} in the center println!("{bw}", bw=best_value) - the same issue, shows {bw} in the center println!("{}, {}", best_value, worst_value) - the same issue, there is that gray block for each occurence of {}

And here is how I am using Sytect to generate spans:

pub struct RustSyntaxSet {
    syntax_set: SyntaxSet,
    syntax_reference: SyntaxReference,
}
impl RustSyntaxSet {
    fn new() -> Self {
        let syntax_set = SyntaxSet::load_defaults_newlines();
        let syntax_reference = syntax_set.find_syntax_by_extension("rs").unwrap().clone();
        Self { syntax_set, syntax_reference }
    }
    pub fn highlight<T: AsRef<str>>(&self, code: T) -> String {
        let mut rs_html_generator =
            ClassedHTMLGenerator::new_with_class_style(&self.syntax_reference, &self.syntax_set, ClassStyle::Spaced);
        for line in LinesWithEndings::from(code.as_ref()) {
            rs_html_generator
                .parse_html_for_line_which_includes_newline(line)
                .unwrap();
        }
        rs_html_generator.finalize()
    }
}
Arrekin commented 3 months ago

Right after I wrote this issue I realized what it may be :d I am using Cirrus-ui for styling my page and there is placeholder widget which most likely is applied to this span.

Arrekin commented 3 months ago

I came up with this workaround that is sufficient for me for now:

.rust.placeholder {
    all: initial;
}

I put it in my own .css file that is included after cirruss css and before base16-mocha.dark css. It clears any already applied styling.

Thank you for being my rubber duck today. I spent the whole day yesterday wondering what in tarnation is happening here and it took me 2 minutes to realize what it may be after writing all that lengthy issue here.