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

Expose syntect theme and syntax sets #2030

Merged

Conversation

dandavison
Copy link
Contributor

Proof-of-principle draft PR allowing delta to replace vendored bat code with bat library usage.

See

#2026
dandavison/delta#903 (companion delta PR)

src/assets.rs Outdated
@@ -15,7 +15,7 @@ use crate::syntax_mapping::ignored_suffixes::IgnoredSuffixes;
use crate::syntax_mapping::MappingTarget;
use crate::{bat_warning, SyntaxMapping};

use lazy_theme_set::LazyThemeSet;
pub use lazy_theme_set::LazyThemeSet;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Spontaneously I would like to have a public API that hides wether or not themes are lazy-loaded. So exporting the name LazyThemeSet should be avoided if possible.

I'm a bit short on time, so maybe you could experiment a bit and see if that can be achieved?

@dandavison
Copy link
Contributor Author

dandavison commented Jan 22, 2022

exporting the name LazyThemeSet should be avoided if possible.

Hi @Enselic, thanks for looking. How about something like 9c2b737? That adds a new public enum to bat named ThemeSet that currently has a single variant only (the LazyThemeSet). The new enum implements the same API as LazyThemeSet.

}

/// An iterator over all theme names
pub fn themes(&self) -> impl Iterator<Item = &str> {
Copy link
Contributor Author

@dandavison dandavison Jan 22, 2022

Choose a reason for hiding this comment

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

I think perhaps this method would be better named theme_names

EDIT I made this change and force-pushed

@dandavison dandavison force-pushed the expose-syntect-theme-and-syntax-sets branch from 2de7e6e to 9c2b737 Compare January 22, 2022 16:26
@dandavison
Copy link
Contributor Author

Since the enum has one variant only currently, I've switched to use a newtype struct in 3d01991. I think that's preferable, but we can go back to the enum in 9c2b737 if not.

@dandavison
Copy link
Contributor Author

Hi @Enselic, if you do get time to look at this again that would be great but no worries there are lots of other issues to attend to currently. It will be nice for delta if it turns out to be possible to go in this direction because I'll be able to delete some vendored code and update highlighting assets simply by bumping the bat library version.

@Enselic
Copy link
Collaborator

Enselic commented Feb 6, 2022

@dandavison Sorry for letting you wait on this. I've had my plate full lately.

I haven't had time to investigate this myself yet, so maybe I should just go ahead and ask instead:

I don't fully understand why you can't use the existing API based around theme names, i.e. Config::theme. Would be great if you could elaborate on that.

@dandavison
Copy link
Contributor Author

Hi @Enselic, no worries!

I don't fully understand why you can't use the existing API based around theme names, i.e. Config::theme. Would be great if you could elaborate on that.

Sure, that's easy to explain. My motivation here is to use the bat library in delta. Delta is, like bat, a Rust application that uses syntect for highlighting. So I need the bat library to give me the syntect theme structs themselves, not just the names.

The connection between delta and bat is:

  1. They both use syntect
  2. Delta uses bat's collection of binary syntect highlighting assets (and also copies bat's pager-spawning code).

The purpose of this PR is to alter the bat library so that a client such as delta can use the bat library for its collection of binary highlighting assets.

@Enselic
Copy link
Collaborator

Enselic commented Feb 6, 2022

Thanks for the explanation! That makes sense. If that is the case, isn't it enough to simply change

pub(crate) fn get_theme(&self, theme: &str) -> &Theme {

to

pub fn get_theme(&self, theme: &str) -> &Theme {

?

That would enable you to get the syntect Theme for a given theme name.

@dandavison
Copy link
Contributor Author

Yes, thank you @Enselic! You're right -- I think because delta had always used syntect's ThemeSet, I had overlooked the possibility of using bat's get_theme and was making this change more complicated than it needed to be. Updated this PR and the companion delta PR.

@sharkdp
Copy link
Owner

sharkdp commented Feb 7, 2022

I'm in favor of this (now minimal) change. Especially since we warn in the documentation that the API of these internal modules is much more likely to change. Some or all of these modules might be removed in the future.

@Enselic
Copy link
Collaborator

Enselic commented Feb 7, 2022

I am also fine with this change as long as the API is only considered semi-stable, but I agree the disclaimer in the docs makes that clear.

I think it would be good to repeat that disclaimer in the docs of the individual changed methods just to make sure people see them though. But it is not a super strong wish.

@dandavison Can you also add an entry in CHANGELOG.md in the "bat as a library" section for this please? More info in https://github.com/sharkdp/bat/blob/master/CONTRIBUTING.md. Thanks!

Apart from the above I consider this PR Approved 🎉

As a side-note, I have played around with the thought of having a bat crate and a bat-impl crate, similarly to the thiserror crate that depends on a thiserror-impl crate.

The bat crate would contain the PrettyPrinter stable API and the bat binary, and PrettyPrinter and the bat binary would depend on bat-impl. The API this PR adds would live in bat-impl. So delta would depend on bat-impl and not bat. Just an idea. Not sure if it is a good idea. Certainly out of scope for this PR.

@dandavison
Copy link
Contributor Author

Thanks @sharkdp and @Enselic, I've add the Changelog entry.

As a side-note, I have played around with the thought of having a bat crate and a bat-impl crate,

What do you think about having a crate dedicated specifically to the collection of highlighting assets and the wrapping API for working with them? It's a very useful collection. (Are there syntect apps other than delta using them?) As a side effect I'm guessing you might appreciate not having all those submodules in your main repo.

@sharkdp
Copy link
Owner

sharkdp commented Feb 7, 2022

What do you think about having a crate dedicated specifically to the collection of highlighting assets and the wrapping API for working with them?

This has been requested a few times, and I agree that this would certainly be very useful. I have written down my thoughts on this here: #919

@Enselic Enselic marked this pull request as ready for review February 8, 2022 06:58
@Enselic Enselic merged commit 4e36a56 into sharkdp:master Feb 8, 2022
@Enselic
Copy link
Collaborator

Enselic commented Feb 8, 2022

@dandavison We are currently experimenting with time based releases, and if everything goes as planned, the next release will be in April. Just to give you a sense of when the API will be available to delta.

@Enselic
Copy link
Collaborator

Enselic commented Feb 24, 2022

@dandavison @michaelblyons Heads-up that we will make a bat v0.19.1 release with this new API in the coming days.

We want to keep projects that depend on bat happy. Waiting until April to make a release seems way too long in this case. :)

@dandavison
Copy link
Contributor Author

Excellent, thanks a lot @Enselic. I'll make a delta release shortly afterwards.

@Enselic
Copy link
Collaborator

Enselic commented Feb 27, 2022

Released in https://crates.io/crates/bat/0.20.0 🎉

(Turned out we had a semver breaking change, so we had to do v0.20.0 rather than v0.19.1)

@epage epage mentioned this pull request Oct 7, 2022
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.

None yet

3 participants