-
-
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
inject language based on file extension & shebang #3970
Conversation
I think I would prefer |
good point, I'll push a fix |
b1876f9
to
996a43c
Compare
Updated to "injection.filename" and also addressed the clippy lints |
65bc9da
to
0daa8d4
Compare
There appears to be one little bug I would like to get to the bottom of before merging. Mainly, if the capture happens more than once in a file, then the last value of injection.filename overrides all the other string contents highlighting. Not sure why this isn't an issue with To illustrate: {
f = writeText "foo.sh" ''
echo $bar
'';
g = writeText "foo.py" ''
def foo():
print("hello")
'';
} Will highlight both strings as python, however, if I change "foo.py" to just "foo" then both are highlighted as haskell. So essentially, the value is being applied to every string captured globally. This appears to have something to do with "injection.combined" since if I turn it off, this doesn't occur, but the downside is that highlighting after interpolation isn't correct either. edit Also, if we really wanted to, we could make the implementation even simpler by just allowing "injection.language" to fallback to filetype detection if language name detection fails with this simple diff: diff --git a/helix-core/src/syntax.rs b/helix-core/src/syntax.rs
index e0a984d2..f7fb8e7a 100644
--- a/helix-core/src/syntax.rs
+++ b/helix-core/src/syntax.rs
@@ -539,6 +539,12 @@ pub fn language_configuration_for_injection_string(
let configuration = &self.language_configs[i];
return Some(configuration.clone());
}
+
+ let path = Path::new(string);
+
+ if let Some(config) = self.language_config_for_file_name(path) {
+ return Some(config);
+ }
None
} And that would be all that is required. |
I think |
Ah sorry, reading is hard 😅 To cover this you most likely want to use |
0daa8d4
to
27ae3da
Compare
@the-mikedavis, thanks for the feedback, I'll try to fix the queries and then it should be ready. Also, fyi I changed the implementation just a little bit. I think it's definitely better. It should also be more efficient since we don't have to traverse the languages once to get the name and then again to get the config. Instead I introduced a new enum edit ''
echo "${bar}"
echo baz
'' The second echo will not highlight correctly, because the result is not combined, even with Perhaps we need a new capture, something like "injection.combine-siblings", that does the samething as "injection.combined" but not globally, only within the scope of the current query. Or maybe "injection.combined" could just be modified to do the same, if it doesn't break anybodies use-case. I'm not sure where a global capture like that would be useful where a scoped one would not, but maybe there is. Either way though, it might be best to implement in a seperate PR. I think it's rare enough to define two scripts of a diffferent language in one file that merging this wouldn't be horrible in its current state. |
27ae3da
to
92537a2
Compare
My last refactor made it fairly trivial to add another commit to highlight based on a captured shebang. In addition, I wrote up a brief doc explaining language injection queries. |
e31afbd
to
5229fa8
Compare
b33d5a9
to
0deaa68
Compare
@the-mikedavis, I address your comments and cleaned up the commit history. Also, the PRs I submitted to upstream tree-sitter-nix were just merged today, so I added a small commit to bump it and included some small query fixes. |
b1affae
to
b751a21
Compare
Nodes can now be captured with "injection.filename". If this capture contains a valid file extension known to Helix, then the content will be highlighted as that language.
Nodes can now be captured with "injection.shebang". If this capture contains a valid shebang line known to Helix, then the content will be highlighted as the language the shebang calls for.
9d08bd3
to
2ac8c03
Compare
Thanks for the thorough review! I addressed all of your comments, and hopefully it is ready now 🤞 |
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.
LGTM now
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.
There are few spelling and typing errors I noticed.
The `@` is now highlighted properly on either side of the function arg. Also, extending the phases with `buildPhase = prev.buildPhase + ''''` is now highlighted properly. Fix highlighting of `''$` style escapes (requires tree-sitter-nix bump) Fix `inherit` highlighting.
Split out injection pair logic into its own method to make the overall flow easier to follow. Also transform the top-level function into a method on a HighlightConfiguration.
2ac8c03
to
aa0a4fb
Compare
Thank you @David-Else. Looks like we really need a spellchecker for Helix as well 😅 |
You can use https://github.com/helix-editor/helix/wiki/How-to-install-the-default-language-servers#ltex-ls , for Markdown, it is excellent! |
* inject language based on file extension Nodes can now be captured with "injection.filename". If this capture contains a valid file extension known to Helix, then the content will be highlighted as that language. * inject language by shebang Nodes can now be captured with "injection.shebang". If this capture contains a valid shebang line known to Helix, then the content will be highlighted as the language the shebang calls for. * add documentation for language injection * nix: fix highlights The `@` is now highlighted properly on either side of the function arg. Also, extending the phases with `buildPhase = prev.buildPhase + ''''` is now highlighted properly. Fix highlighting of `''$` style escapes (requires tree-sitter-nix bump) Fix `inherit` highlighting. * simplify injection_for_match Split out injection pair logic into its own method to make the overall flow easier to follow. Also transform the top-level function into a method on a HighlightConfiguration. * markdown: add shebang injection query
* inject language based on file extension Nodes can now be captured with "injection.filename". If this capture contains a valid file extension known to Helix, then the content will be highlighted as that language. * inject language by shebang Nodes can now be captured with "injection.shebang". If this capture contains a valid shebang line known to Helix, then the content will be highlighted as the language the shebang calls for. * add documentation for language injection * nix: fix highlights The `@` is now highlighted properly on either side of the function arg. Also, extending the phases with `buildPhase = prev.buildPhase + ''''` is now highlighted properly. Fix highlighting of `''$` style escapes (requires tree-sitter-nix bump) Fix `inherit` highlighting. * simplify injection_for_match Split out injection pair logic into its own method to make the overall flow easier to follow. Also transform the top-level function into a method on a HighlightConfiguration. * markdown: add shebang injection query
* inject language based on file extension Nodes can now be captured with "injection.filename". If this capture contains a valid file extension known to Helix, then the content will be highlighted as that language. * inject language by shebang Nodes can now be captured with "injection.shebang". If this capture contains a valid shebang line known to Helix, then the content will be highlighted as the language the shebang calls for. * add documentation for language injection * nix: fix highlights The `@` is now highlighted properly on either side of the function arg. Also, extending the phases with `buildPhase = prev.buildPhase + ''''` is now highlighted properly. Fix highlighting of `''$` style escapes (requires tree-sitter-nix bump) Fix `inherit` highlighting. * simplify injection_for_match Split out injection pair logic into its own method to make the overall flow easier to follow. Also transform the top-level function into a method on a HighlightConfiguration. * markdown: add shebang injection query
Nodes can now be captured with "injection.filename". If this capture contains a valid file extension known to Helix, then the content will be highlighted as that language. In addition, a new "injection.shebang" is added which injects based on a valid shebang.
For consistency, both implementations use the same logic as used to detect the document filetype as a whole.
I'm not sure if the following is the most efficient way to capture the built in configuration, or if it can be passed in from somewhere else:https://github.com/nrdxp/helix/blob/7f61de137f07cf5897d5a3240b1ece98febc0eb0/helix-core/src/syntax.rs#L1836
Also, it would probably be nice to extend this to user defined languages as well.65bc9daAlso includes an additional query for Nix so one can test this behavior themselves. A few screens with that query:
This PR also includes a new document briefly describing language injeciton.