-
-
Notifications
You must be signed in to change notification settings - Fork 118
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
Syntax highlighting support #67
Comments
If you're looking for something a little less regexpy (TextMate grammars are basically large regexp soups if I recall correctly), you may want to look at https://lib.rs/crates/tree-sitter - I've had good experiences with it, it was just missing a couple grammars so I rolled my own crate to build them: https://github.com/fasterthanlime/tree-sitter-collection (Hopefully that's useful input, if not feel free to collapse that comment!) |
Oh hey no this is actually really cool and relevant! I wonder what the story would be with people writing their own custom ones 🤔 |
I wonder if this is easy to be benchmarked. That might be really helpful |
@zkat I made a very rough draft attempt at writing a custom SourceCode for integrating syntect, but I've been running into issues with getting the labels to render on the correct columns. I basically adapted the My current test example looks like this: Code is here: https://github.com/kallisti-dev/miette-syntect/blob/40f1a63b398e9c9cbda964076cd1d11e7b538c56/src/highlighted_source.rs#L195 I'm likely just doing something wrong here, but I'm not sure what. I might try this as a PR to miette instead (with everything behind a feature flag) since that would make it easier to ensure it's doing the right things with labels, and also makes it possible to only run the highlighter on the actual part that's being rendered. my version highlights the whole source because the As a side note, you'd probably also want a smarter renderer because the default |
Some more thoughts on this. My initial idea was to try this as a custom I suppose an idealized API would be that you set the Maybe the simpler approach in my example could work, where the |
I tried several approaches to only highlight the A change to get this working would be a slight modification to the Alternatively, changing ...almost. The |
My thoughts on implementing this:
tl;dr I think all the actual highlighting logic should live entirely in |
Quick update on this. I have a rough draft of this working locally. Using a similar approach to what you outlined above and keeping all the mutable state in I'll submit a draft PR once I clean it up a bit. Next steps are to design the API and document. It would be nice if the configuration API could wrap everything in syntect so the user doesn't need to include a syntect dependency for SyntaxReference or Theme, but I'm not sure what would be a sensible way to do this. Making the choice of highlighter customizable with a trait would be nice as well, for tree-sitter support or for a more specialized highlighter. For my own use case, I really only need Rust highlighting so I would opt for a specialized parser/highlighter if I had the option to use one. |
@kallisti-dev omg that looks amazing |
Draft PR at #313 It ended up being easier to create a new global for the highlighter than it was to cram it into This adds a new There is also a generic To make language detection easier, I added a I am also considering adding a I think implementing language detection for tree-sitter might be a bit more of a challenge because it uses an opaque FFI type that you import from a language-specific crate (e.g. |
Here's what https://gist.github.com/kallisti-dev/d43f4feb726ee15d50f7a15e7fb8c9d4 Maybe we could apply settings from |
https://gist.github.com/kallisti-dev/95ca7db90e6c635e0c3a67b858f39509 For anyone who wants to see what the default themes look like on their local setup, I've made a quick script to render a Rust hello world with all of them. You can run this locally on my branch to see how they render with your terminal settings. Here's what it looks like on my screen, using Fira Code with VS Code terminal I think a bolder color palette like |
@kallisti-dev I used a script to take screenshots of the example program with some different light and dark background themes: IMO none of the syntect themes are readable across all the terminal themes I tested, with solarized probably being the closest. I'm thinking the best thing to do might be to set our own rgb background color when using syntect. We'd also probably want to use a rgb theme for the rest of miette in this case, since the user's terminal theme isn't guaranteed to work with our background. It isn't great, but is probably the least-bad compromise until we get 16-color ANSI support with syntect. theme testing scriptThis is very unlikely to work on somebody else's setup, but could probably be adapted to any linux wm without too much effort. #!/usr/bin/env bash
set -e -u -o pipefail
out="$1"
themes=(
ayu-light
github-dark
github-light
gruvbox-dark
gruvbox-light-medium
ibm3270
mellow-purple
red-sands
solarized-dark
solarized-light
argonaut
ocean
)
mkdir -p $out
for theme in ${themes[@]}; do
nix run nixpkgs#theme-sh "$theme"
grim -g "$(swaymsg -t get_tree | jq -j '.. | select(.type?) | select(.focused).rect | "\(.x),\(.y) \(.width)x\(.height)"')" "$out/$theme.png"
done
montage "./$out/*" -mode Concatenate "$out.png" |
Add support for syntax highlighting through syntect.
I figure this can be done by having the graphical handler take an optional SyntaxSet, and have an optional identifier token method as part of
SpanContents
, so the renderer can color code accordingly.The biggest challenge here is designing things such that things are appropriately feature flagged and don't grow binaries too much?
The text was updated successfully, but these errors were encountered: