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

feat(highlighting): implement syntax highlighting for unnamed source codes #425

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

glevco
Copy link
Contributor

@glevco glevco commented Feb 19, 2025

Currently, both name and language are part of the SpanContents abstraction, which comes from the NamedSource struct. It would make more semantic sense if those were part of the SourceCode abstraction instead. This would allow us to use syntax highlighting in unnamed source codes, which is not currently possible. This PR proposes this.

Changes

  • Rename NamedSource to MietteSourceCode, adding language to it.
  • Move name() and language() from SpanContents trait to SourceCode trait.
  • Move name and language from MietteSpanContents struct to MietteSourceCode struct.

This is a breaking change because it refactors public APIs.

Before

let src = NamedSource::new("bad_file", "fn hello() {}").with_language("Rust");
let report = miette!("oops!").with_source_code(src);

After

let src = MietteSourceCode::new("fn hello() {}").with_name("bad_file").with_language("Rust");
let report = miette!("oops!").with_source_code(src);

// unnamed with language, not possible before!
let src = MietteSourceCode::new("fn hello() {}").with_language("Rust");
let report = miette!("oops!").with_source_code(src);

@zkat zkat added the breaking A semver-major breaking change label Mar 2, 2025
Copy link
Owner

@zkat zkat left a comment

Choose a reason for hiding this comment

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

I generally like this, but I'm gonna have to think more about moving those specific things around. I think there might be other things to be done about the struct to-be-formerly-known-as-NamedSource but I'll try and remember what else I thought might be nice.

I also don't know when I'll want to have a breaking release of miette. I try to keep them very very sparse, so if you can think of a way to do this without the breaking change, even if we eventually do the "nice" version, then that'll get unnamed source code highlighting working sooner for the community.

@glevco
Copy link
Contributor Author

glevco commented Mar 5, 2025

@zkat sure, I can do that. I'll simply create the MietteSourceCode like in this PR, but as a new struct instead of replacing the NamedSource with it. Then you can deprecate NamedSource and eventually remove it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking A semver-major breaking change
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants