-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Rainbow tree-sitter matches 🌈 #2857
base: master
Are you sure you want to change the base?
Rainbow tree-sitter matches 🌈 #2857
Conversation
Surprisingly little code to get this working 👍🏻 |
Would it make sense to use the color palette colors as default values if the rainbow feature is enabled? |
That could work but usually the palette has a bunch of different ui colors too, so some deeply nested brackets could end up using backgrounds / grays which may look bad. I think a default rainbow palette not based on the current theme could work ok |
You could cheat and ignore any scopes that |
I suspect theme colors may not be good for a theme's rainbow because they might blend in and end up looking confusing. I'll give it a try though - that would take all the guesswork out of having to come up with a default that looks good on all themes 😄 |
You could get fancy and calculate the color contrast and only pick colors that are distant enough :D |
This allows one to match on bracket nodes with named nodes in queries. Doing so is useful for tree-sitter based rainbow brackets (helix-editor/helix#2857).
well, I have a snippet to calculate the L* luminance value (as in Lab/LCH "professional" colour systems) from an sRGB triple. I use it for exactly that, grading and excluding colours with weak contrast in a CLI app 👍 if interested Helix would be most welcome to it, I can PR it to @the-mikedavis rainbow branch? |
Let's keep this branched focused just on the happy path for now (rainbows enabled and themed). If anything, we might want to merge this once cleaned up without any default behavior - i.e. the theme needs to have a |
The reason I lean towards a hardcoded default that uses red/orange?/yellow/green/blue/purple/etc is that it's easy to get a feel for how things are nested when you can predict the ordering of the bracket colors. If I know blue comes after green, I know where a blue tuple stands inside a huge green map or list literal. I don't want to discourage discussion on automatically determining a good rainbow though, it sounds like there are some cool color math tricks we could use 🙂 |
fc9aad4
to
1e323cf
Compare
This comment was marked as resolved.
This comment was marked as resolved.
1e323cf
to
1445e23
Compare
a78735b
to
6886965
Compare
05bd03c
to
3396f9b
Compare
I found an alternate approach for this that ends up being less complicated. Now it works similarly to the locals tracking system in the syntax highlights iterator. The queries end up being more intuitive to write and more flexible (see the updated PR description). I think this approach is general-purpose enough: I've worked through 27 languages so far with the toughest being HTML. HTML works now so that nested XML elements behave like other nesting scopes rather than just highlighting the |
656e3fd
to
be2e9f4
Compare
Hey @the-mikedavis - any chance this could be merged soon, or put back on a list of milestones? Is there anything blocking it? I keep this merged into my soft fork and it works great. It's indispensable for writing Racket/Scheme, and pretty helpful for all languages in general. And another languishing PR (#4493) is blocked on it. I have a rebased version over at https://github.com/omentic/helix/tree/rainbow-rebase, if needed. |
I don’t understand why this PR has been pending since 2022, especially since it’s fully functional and adds valuable feature. Leaving this to plugins isn’t ideal, as it could hurt performance. VS Code faced a similar issue and ended up integrating support directly. |
For those asking "why not merge dis??", read #2857 (comment) and explore the different iaauea/PRs related to treesitter (that are all somewhat related to one another). |
For some more details on top of that older comment: @pascalkuthe and I are working on new tree-sitter bindings (#10286) and the new highlighter based on those is written a way that makes running a query over injection layers very easy. (This feature uses that so that rainbow brackets are highlighted even within code fences in markdown, for example, or within JS in As for the feature itself: I still use this in my driver branch and have been keeping it more-or-less up to date. I'll give this PR a proper rebase so it's easier for others to use too. I'm not totally sure where other maintainers stand on the feature but I remember @archseer considering it after working on a bunch of scheme code. Even if it's something we don't pull in as a core feature I'd be interested in maintaining a plugin for it officially or personally. |
35d707d
to
2dc56f0
Compare
This change adds a field to the schema of themes which takes a list of styles. rainbow = ["red", "orange", "yellow", { modifiers = ["reversed"] }] [palette] red = "#ff0000" orange = "#ffa500" yellow = "#fff000" Normal style rules apply for each element in `rainbows`: you can use definitions from the palette and the full fg/bg/modifiers notation. Themes written with `rainbow` keys are not backwards compatible. Parsing errors will be generated for older versions of Helix attempting to use themes with `rainbow` keys. A default rainbow is provided with base16 colors. This change is made with rainbow pair characters (parens, brackets, etc.) in mind but it could also be used for other rainbow cosmetic elements like rainbow indent-guides.
This option is similar to the `rulers` config: it can be set no the editor key in config and also overridden for specific languages, so you could enable rainbow brackets for lisps for example without enabling it globally.
This can be used to calculate rainbow highlights (see the child commit) or indents or textobjects and be accurate to the injected content rather than just the root layer. This is useful for languages which use injections heavily like Vue or JavaScript within HTML but are also useful in common scenarios like within codeblocks in Markdown. This iterator shares some code with the HighlightIter and HighlightIterLayer but that iterator emits HighlightEvents, so it cares about the beginnings and ends of highlight events rather than captures.
This is an example usage of the query_iter introduced in the parent commit: captures are returned in order across language layers. We can use this iterator and a stack for the rainbow scopes to calculate highlight spans that can be merged into the syntax highlights using syntax::merge.
We call the rainbow_spans function introduced in the parent commits over the largest node that contains the current viewport: we need to reach far enough back in the document that we find the absolute beginning for brackets. If we run rainbow_spans only over the current viewport, we get a bug where the color of rainbow brackets changes as we move the viewport.
Co-authored-by: SoraTenshi <[email protected]>
This is brings the fix from d5f17d3 to the QueryIter layers. A trait for getting the cursor and sort-key from each layer helps cut down on code duplicated between these iterators.
The code in the `sort_layers` function was fully duplicated between the HighlightIter and the QueryIter. This change adds a common `sort_layers` function that accepts a layer Vec from both.
This deduplicates some somewhat complex code between the highlight_iter and query_iter.
It's easy to mistakenly use-after-free the cursor and captures iterator here because of the transmute. Ideally this could be fixed upstream in tree-sitter by introducing an API with lifetimes/types that reflect the lifetimes of the underlying data. Co-authored-by: Pascal Kuthe <[email protected]>
2dc56f0
to
1a5eb55
Compare
Configurable, themable, nesting rainbow highlights:
This approach uses the syntax tree to guide nesting levels and highlights so it ends up being much more flexible than a naive pair-matching implementation. For example, we can highlight
<
/>
when used in Rust type parameters likestruct MyStruct<'a> { .. }
and not mistakenly highlight<
/>
comparison operators. We can also capture multiple nodes, for example the#
,[
and]
characters in a Rust attribute like#[cfg(windows)]
and none of these characters are required to be adjacent.The cherry on top is that we can highlight according to the syntax tree's nesting information rather than whether characters surround each other. For example, in this TOML table,
characters
is a sub-table ofeditor.whitespace
and the rainbow highlights reflect that fact.It also works across injected languages like javascript injected into HTML script tags.
(You can see each of these things in action in the gif above.)
How does it work?...
This implementation leverages tree-sitter queries. We use a
QueryCaptures
Iterator very similar to the existing one for syntax highlights to query the parsed syntax tree for newrainbows.scm
patterns.Two new captures -
@rainbow.scope
and@rainbow.bracket
- track nesting level and capture syntax nodes for highlighting with rainbows. Specifically:@rainbow.scope
pushes aRainbowScope
on a stack. When@rainbow.bracket
matches, we test that the captured node is a direct descendant of the current scope. If it is, we emit highlights for it according to the scope's level of nesting.Configuration...
In the theme we add a
rainbow
key that takes a list ofStyle
s. You can have however many you want and you can re-use colors from the palette or even throw modifiers in there:A default rainbow using the red, yellow, green, blue, cyan, and magenta terminal colors is used as a fallback.
Enable the rendering of rainbow brackets in your
config.toml
Or enable rendering per-language in
languages.toml
(this overrides theconfig.toml
setting for any languages that set this value):When the
rainbow-brackets
option is disabled, rainbow brackets are not rendered or computed.Status...
The current implementation is working well. Take it for a spin and let me know if you find any bugs!