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

Twig component html syntax #17

Open
fbuchlak opened this issue Oct 14, 2024 · 5 comments
Open

Twig component html syntax #17

fbuchlak opened this issue Oct 14, 2024 · 5 comments

Comments

@fbuchlak
Copy link

Hi, are there any plans to support Component HTML Syntax?
Specifically "injecting" output_directive in quoted html attributes for something like this <twig:foo :bar="variable|filter1|filter2" />.

@gbprod
Copy link
Owner

gbprod commented Oct 15, 2024

Interesting, I'll try to implement this soon

@gbprod
Copy link
Owner

gbprod commented Oct 22, 2024

I had a look on this and... it's really not easy.
The main idea would be to split this parser in 3 different grammars (twig, twig_statement and twig_output) like we have in the php tree-sitter parser with php and php_only. With this split, it will be possible to inject twig_output grammar in html filetypes.
This is a hard job for low gain... I think I will not do this soon 😅

@fbuchlak
Copy link
Author

Sure, nevermind. I've tried to create html injections for this, but without split grammar it seemed impossible.

My initial idea was to modify attribute value in injection by adding curly braces around that statement, but didn't find any solution.
So <twig:foo :value="something" /> would "internally" become <twig:foo :value="{{ something }}" /> for treesitter and highlighting would be done with offset (but that's probably not possible)

@fbuchlak
Copy link
Author

I've ended up with injecting javascript for those component attributes. It's not perfect and might be confusing, but it's better than nothing. At least for basic highlighting.

"solution"

; after/queries/html/injections.scm
;; extends

(element
  (_ (tag_name) @_tag
      (#lua-match? @_tag "^twig:%a")
  (attribute
    (attribute_name) @_attr
      (#lua-match? @_attr "^:%a")
    (quoted_attribute_value
      (attribute_value) @injection.content)
        (#set! injection.language "javascript")
    )))

and result looks like this
2024-10-22-15:39:45

@gbprod
Copy link
Owner

gbprod commented Oct 22, 2024

Interesting workaround!

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

2 participants