Skip to content
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

Can't get syntect-highlighter to work #337

Open
froth opened this issue Feb 6, 2024 · 6 comments · May be fixed by #424
Open

Can't get syntect-highlighter to work #337

froth opened this issue Feb 6, 2024 · 6 comments · May be fixed by #424
Labels
help wanted Extra attention is needed question Further information is requested

Comments

@froth
Copy link

froth commented Feb 6, 2024

I tried to get syntax highlighting to work according to the new Readme section, but could not succeed. I think the problem is somewhere on my side but I can't figure it out.

I created this minimal project https://github.com/froth/miette-highlighting/blob/main/src/main.rs and it does not highlight on my machine.
Screenshot from 2024-02-06 09-41-05

The same code is highlighted correctly using cargo run --example syncat test.rs in https://github.com/trishume/syntect/.

@zkat zkat added help wanted Extra attention is needed question Further information is requested labels Feb 6, 2024
@zkat
Copy link
Owner

zkat commented Feb 6, 2024

cc @kallisti-dev

@erratic-pattern
Copy link
Contributor

@froth I was able to reproduce this issue with the provided example crate just a month ago, but now it's working... exact same miette version.

So I am wondering if this is somehow related to terminal color detection. Maybe either a bugfix in the supports-color crate resolved the issue or maybe my local terminal emulator patched something (I'm running Wezterm with tmux on Mac OS). Just throwing some ideas out there but nothing is confirmed at this point.

Which terminal emulators/multiplexers are you using when running this? Can you do a clean build (rm -rf target) on the example crate and verify that you are still having this issue?

@froth
Copy link
Author

froth commented Mar 24, 2024

Thanks for having a look @erratic-pattern!

rm -rf target did not change anything for me.

I also tried cargo upgrade and removing Cargo.lock. Both not successful for me.

I am using zsh in tmux in gnome-terminal. Additionally I tried xterm with bash without tmux.

Very weird that it somehow fixed itself for you.

@ArchieAtkinson
Copy link

Was anyone able to get to the bottom of this? I'm struggling to get it working on a basic example but using cargo run --example syncat test.rs from https://github.com/trishume/syntect/ does work.

Also, a small side note the provided example languages from the SpanContent::language documentation are:
"Examples: Rust, TOML, C"

It doesn't seem like TOML is available by default with syntect.

@glevco
Copy link

glevco commented Feb 17, 2025

Coincidentally I also just stumbled upon this issue. I did some investigation and I couldn't find any version where the example provided by @froth worked as-is. It looks like it's an issue of how configuration and defaults are handled, and how the docs reflect that. Here are some currently available workarounds:

Manually set miette options

Manually enabling syntax highlighting in the miette handler supersedes the rgb_colors attribute's default value, which is what's causing it not to work:

#[derive(Error, Debug, Diagnostic)]
#[error("MyBad")]
struct MyBad {
    #[source_code]
    src: NamedSource<String>,
    #[label("This bit here")]
    bad_bit: SourceSpan,
}

fn main() -> miette::Result<()> {
    miette::set_hook(Box::new(|_| {
        Box::new(miette::MietteHandlerOpts::default()
            .with_syntax_highlighting(SyntectHighlighter::default())
            .build())
    }))?;

    let src = "fn main() { println!(\"this is a string\"); }".to_string();
    let report: Report = MyBad {
        src: NamedSource::new("bad_file.rs", src).with_language("Rust"),
        bad_bit: (9, 4).into(),
    }.into();

    println!("{:?}", report);
    Ok(())
}

Alternatively you can set .rgb_colors(RgbColors::Always) instead of .with_syntax_highlighting(...), it has the same effect (although with RGB instead of ANSI) and shows how these options are currently entangled.

Manually use a GraphicalReportHandler

You can also manually use a GraphicalReportHandler to generate your outputs. This is what the syntax highlight feature test does — it doesn't test what the default behavior should be.

fn main() -> std::fmt::Result {
    let src = "fn main() { println!(\"this is a string\"); }".to_string();
    let report = MyBad {
        src: NamedSource::new("bad_file.rs", src).with_language("Rust"),
        bad_bit: (9, 4).into(),
    };

    let mut out = String::new();
    GraphicalReportHandler::new_themed(GraphicalTheme::unicode())
        .render_report(&mut out, &report)?;

    println!("{}", out);
    Ok(())
}

cc @ArchieAtkinson


I intend to create a PR today with improvements in that regard.

@glevco
Copy link

glevco commented Feb 19, 2025

A bit delayed, but here it is: #424

@zkat could you please take a look at it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed question Further information is requested
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants