Skip to content
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

Support for inline <script> tags #37

Open
hhofner opened this issue Oct 17, 2023 · 9 comments
Open

Support for inline <script> tags #37

hhofner opened this issue Oct 17, 2023 · 9 comments

Comments

@hhofner
Copy link

hhofner commented Oct 17, 2023

Would be nice to have support for inline script tags (please disregard the gibberish JS)

image

@connorlay
Copy link
Member

I like this idea! I can work on a PR adding JS injections to https://github.com/phoenixframework/tree-sitter-heex/blob/main/queries/injections.scm. Depending on what text editor you use you may need to port those queries to the format your text editor understands. For example, nvim-treesitter uses a different set of captures to handle language injection.

@connorlay
Copy link
Member

Update on this: I believe we need to introduce a new node type for <script> that allows {} characters. Once I have more time for open source I'll get started on that.

@the-mikedavis
Copy link
Member

We could support script (and maybe style too?) elements as separate nodes like tree-sitter-html: https://github.com/tree-sitter/tree-sitter-html/blob/d742025fa2d8e6100f134a6ea990443aa1f074b3/grammar.js#L64-L68. It looks like we have some parsing errors currently where do is mistaken by the error recovery mechanism and I think using a custom element might eliminate that

@connorlay
Copy link
Member

I started working on this last night, see #38. I'm currently struggling to get the contents of a <script> tag to parse correctly. It is running into issues when there are {} characters and I'm not sure how to proceed.

Looking at tree-sitter-html they solve this via an external scanner written in C. Given how similar HEEx is I could copy that over if we are willing to introduce native code into this grammar.

@the-mikedavis If you have any ideas, I'd love to hear them!

@connorlay
Copy link
Member

For posterity, it appears that we do need to implement an external scanner to write a node that captures all text until hitting </script> or </style>: tree-sitter/tree-sitter#1252

fcaldera added a commit to fcaldera/dotfiles that referenced this issue Nov 30, 2024
@kaeedo
Copy link

kaeedo commented Dec 3, 2024

Hello. Could I ask about the status of this?

@the-mikedavis
Copy link
Member

I believe Connor's last comment is still up-to-date. To fix this properly we would probably need to add a scanner like tree-sitter-html's and follow what it's doing for parsing <script> tags.

@fcaldera
Copy link

This is the closest I could get for nvim-treesitter.
The following injections file properly highly JS:

; heex/injections.scm
((tag
  (start_tag
      (tag_name) @tag_name (#eq? @tag_name "script"))
  (text)
  (end_tag)
) @injection.content
 (#offset! @injection.content 1 0 0 -9)
 (#set! injection.language "javascript")
 (#set! injection.include-children)
 (#set! injection.combined))

However, it disables the elixir injections. I'm not sure why since the query seems to match the exact nodes.

image

fcaldera added a commit to fcaldera/tree-sitter-heex that referenced this issue Dec 15, 2024
See: phoenixframework#37
This commit doesn't fix the grammar but add syntax highlight support for
JavaScript and CSS code inside of <script> and <style> tags.
@fcaldera
Copy link

fcaldera commented Dec 15, 2024

I got it working by adding the ;; extends modeline comments to my queries.
Here is a workaround for nvim-treesitter users, while the support is added to the grammar:

File: nvim/queries/heex/injections.scm

;; extends

((tag
  (start_tag
      (tag_name) @tag_name (#eq? @tag_name "script"))
  (text)
  (end_tag)
) @injection.content
 (#offset! @injection.content 1 0 0 -9)
 (#set! injection.language "javascript")
 (#set! injection.include-children)
 (#set! injection.combined)
)

((tag
  (start_tag
      (tag_name) @tag_name (#eq? @tag_name "style"))
  (text)
  (end_tag)
) @injection.content
 (#offset! @injection.content 1 0 0 -9)
 (#set! injection.language "css")
 (#set! injection.include-children)
 (#set! injection.combined)
)

This only adds syntax highlight support.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants