-
-
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
Inconsistent syntax highlighting for tagged templates in JavaScript #11952
Comments
The 3 magic characters happen to correspond to 3 popular single-character programming languages: Helix supports tree sitter language injections, using a different parser for a part of a file (here - a template literal). There's no reliable way to tell what language a string represents, so it uses a simple heuristic, matching the template tag against a list of known injection regexes. For example, the Line 2579 in 38faf74
So, long story short, that's just a negative side effect of supporting injections in the first place. DisclaimerI'm not an expert. I'm not even a contributor. I was just hoping to pick up a simple task to contribute, but there's nothing to do here :( |
Ah! Thanks for pointing that out. Perhaps these patterns could be a bit more strict, as a single letter which matches in any string including them seems far from ideal. [[language]]
name = "c"
injection-regex = "^c$"
[[language]]
name = "c"
injection-regex = "^d$"
[[language]]
name = "c"
injection-regex = "^r$" This is how I'm working around it now anyway. I'm also not really convinced that template strings should be language injection sites. Unlike for example Markdown code blocks where the intention is clear, in JavaScript it's just a function name which could mean anything. Either way though, I think the Edit: and this doesn't just apply to single letter patterns. Should the |
I'd leave it open until a real maintainer shares their thoughts. I agree it's a bit confusing, though. |
Fwiw this is a very popular feature request on Zed editor's issue board atm - people definitely want this, at least for applications such as with This is where this injection is performed: helix/runtime/queries/ecma/injections.scm Lines 1 to 10 in e1d1a5c
And my suggestion above can easily be implemented with something like: ; Parse the contents of tagged template literals using
; a language inferred from the tag.
(call_expression
function: [
(identifier) @injection.language
(member_expression
property: (property_identifier) @injection.language)
]
arguments: (template_string) @injection.content
+ (#match? @injection.language "^(html|css)$")) You can see this working here: `foo.js` tagged template sample code
Happy to open a pull request adding this line if y'all agree :) |
I think that's a great idea |
Summary
Tagged template strings in JavaScript have strange, inconsistent highlighting. It appears that with certain characters in the function name, the entire template string content is highlighted as regular JavaScript.
The characters 'r', 'd', and 'c' seem to change the highlighting. Strangely, the function names starting with 'd' and 'c' also change the colour of the single quoted string 'Baz', but function names starting with 'r' do not (even if they're not .
Edit: Just to be clear, the
qwetyuiopasfghjklzxvbnm
tagged templates in the screenshot have the correct highlighting (so named because they don't include any of the characters which cause the highlighting to change).Reproduction Steps
The source code above:
Helix log
No response
Platform
Linux
Terminal Emulator
alacritty 0.14.0 (22a44757)
Installation Method
source
Helix Version
helix 24.7 (101a74b)
The text was updated successfully, but these errors were encountered: