[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Tree sitter: issue embedding HTML, CSS, JavaScript within a new php-

From: Simon Pugnet
Subject: Re: Tree sitter: issue embedding HTML, CSS, JavaScript within a new php-ts-mode
Date: Fri, 10 Feb 2023 11:40:32 +0000

Yuan Fu <casouri@gmail.com> writes:

Hey Simon,

Thanks for trying this out! Feedbacks like this are very welcome.

Hi Yuan, you're very welcome! I'm glad to be able to help.

On Feb 9, 2023, at 4:45 AM, Simon Pugnet <simon@polaris64.net> wrote:

1. Based on my rules for embedding ='html= within ='php= above, should I expect ~(treesit-language-at (point))~ to return ='php= when the point is within a PHP region?

Because we don’t have much experience with tree-sitter and its
interfaces, I made treesit-language-at simply delegate work to
treesit-language-at-point-function, which can be an arbitrary
function, giving developers maximum flexibility. You need to set that variables to a function, otherwise treesit-language-at simply returns
the first parser in the parser list.

Ah, that makes sense, thank you. I must have missed that in the documentation as I thought this was just used when overriding the default behaviour.

I've added the following implementation and this appears to work nicely: -

 (defun php-ts--language-at-point (point)
"Return the language at POINT, used to determine which tree sitter parser to use." (let* ((php-node-at-point (treesit-node-at point 'php))
          (parent-node (treesit-node-parent php-node-at-point)))
(if (and (string-equal "text" (treesit-node-type php-node-at-point)) (or (string-equal "program" (treesit-node-type parent-node)) (string-equal "text_interpolation" (treesit-node-type parent-node))))

The next step will be to run further tests on the current node in cases where the language is 'html in order to determine if the actual language is 'css or 'javascript.

2. Is my goal of embedding HTML within PHP, then embedding CSS and JavaScript/TypeScript within HTML feasible and if so am I going about this in the right way?

It should be. Although I didn’t thought of having multiple layers of
embedded language (in this case PHP embedding HTML embedding
CSS/Javascript), if you order the entries in treesit-range-rules like
you do now (outer most host language, then embedded language, then
embedded embedded language), it should work. Try setting
treesit-language-at-point-function and it should work right. If not…
then we need to look into it.

I'll try this next and I'll be sure to let you know how it goes.

Thanks for your advice, and kind regards,

Simon Pugnet

Attachment: signature.asc
Description: PGP signature

reply via email to

[Prev in Thread] Current Thread [Next in Thread]