-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
nix: add language injections #3594
Conversation
Unfortunately queries don't have a way to express arbitrary nesting. Queries I've seen in neovim handle it similar to this. I'm not sure this will work for many languages out of the box though because some injection regexs are very specific like: Line 373 in 411c5e4
We may want to remove the |
Well I guess that's why I couldn't figure it out then. I wonder if it might be better to try and parse the first line of an indented string and parse the shebang (if one exists). Not sure if there is a way to grab the first line easily though 🤔 |
Sub-selecting in nodes is also not really well supported by tree-sitter queries 😅 For injection in particular it's best to have the language injection token expressed by the parser - for example some node under string or comment that contains the first word. Then you could capture that as |
I'm moving out of draft as I have been able to accomplish injections in many common places in Nix where shellcode is typically expected. The comment based injection still works as well, and if you are okay with me modifying other language injection regex's I can do that to support more languages, but many already work as is. edit ''
"${toString 1}"
'' Then it stops highlighing as bash for the rest of the string. This doesn't happen if there is no edit 2 So I've identified that the issue lies with the the first |
I opened a PR upstream to add these same queries there, so maybe we don't need to merge this, but just uncomment the injections in the upstream binding if it is accepted? |
We don't use rust bindings or inherit the queries from the repo. It's good to upstream the changes so that other tree-sitter consumers can get the benefits too but for helix the queries only need to be updated in-repo |
f74db4b
to
18f603d
Compare
So I've identified the reason for the bash quoting issue described above, and it should have been obvious but I guess I wasn't thinking correctly 😅 . Essentially the two strings on either side of an interpolation are highlighted seperately, so instead of having a balanced quote count, the string on the right side thinks it starts with an opening Also found another issue outline in nix-community/tree-sitter-nix#32 but that will have to be worked out upstream. |
b2f9f88
to
8314a3f
Compare
By simply placing a comment with the name of the desired language just before a multi-line string, that language will be injected. Also, common functions and attributes which are expected to be shell code are automatically injected.
Turns out |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These look super sweet, thanks for working on this!
This is pretty nice, thanks for adding it in. I'm just messing around with this and it seems a little clunky because it seems like the multi-line string needs to be the first non-whitespace chars on the line right? I've tried the following and both work: k = #shell
''exec ${kubectl}/bin/kubectl "$@"''; and k =
#shell
''
exec ${kubectl}/bin/kubectl "$@"
''; It'd be nice if the following worked too #shell
k = ''exec ${kubectl}/bin/kubectl "$@"''; Would that be possible with tree-sitter? (I am not versed in tree-sitter in the slightest :( ). |
Actually I think something might be funky with the current code, I just noticed this when writing out the previous comment: k = #shell
''exec ${kubectl}/bin/kubectl "$@"'';
kk = #shell
''exec ${kubectl}/bin/kubectl "$@"''; the string for |
@mmlb I ran into this myself when writing the queries. Tree-sitter queries do not have a way to express arbitrary nesting, probably because it would lead to many false positives. I tried to work around that by simple mimicing queries multiple times inside a many nested levels and we hit some false positives ourselves that way so we decided to leave it explicit. In order to make this as nice as possible though, there are certain names for |
Ah I see, I figured it would be something along those lines, 👍. Thanks for doing this really loving this! ❤️ |
Glad you find it useful, if we can also get #3970 merged then we can also do highlights based on shebang or even file name in expressions like |
Yep that would be very nice. |
By simply placing a comment with the name of the desired language above a multi-line string, that language will be injected.
Opening as a draft because I'm sure there must be a better way to write this, and hopefully someone with more experience can give me some pointers. Tried using repetition via
*
but I couldn't seem to get it right.