emacs-devel
[Top][All Lists]
Advanced

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

Re: How to properly use treesit-range-rules ?


From: Vincenzo Pupillo
Subject: Re: How to properly use treesit-range-rules ?
Date: Tue, 19 Sep 2023 15:09:53 +0200

Your workaround works very well!
Currently my major mode is not published on Melpa etc. 
I created a public repository two days ago just to make sharing easier.
When it will be decent, I would be happy if it could be integrated into Emacs.

Thank you very much.
Vincenzo

In data martedì 19 settembre 2023 06:11:42 CEST, Yuan Fu ha scritto:
> 
> > On Sep 18, 2023, at 1:58 PM, Vincenzo Pupillo <v.pupillo@gmail.com> wrote:
> > 
> > Hi, 
> > I'm trying to figure out how to properly use treesit-range-rules for my
> > php-ts-mode (you can find it here https://github.com/vpxyz/php-ts-mode, 
> > there 
> > are still several things to fix.).
> > Currently the rules I am using are as follows:
> > (setq-local treesit-range-settings
> >    (treesit-range-rules
> >     :embed 'html
> >     :host 'php
> >     ;;:local t
> >     '((program (text) @cap)
> >       (text_interpolation (text) @cap))
> > 
> >     :embed 'javascript
> >     :host 'html
> >     ;;:local t
> >     '((script_element
> > (start_tag (tag_name))
> > (raw_text) @cap))
> > 
> >     :embed 'css
> >     :host 'html
> >     ;;:local t
> >     '((style_element
> > (start_tag (tag_name))
> > (raw_text) @cap))))
> > 
> > I also tried different combinations, even with the new :local flag. 
> > However, I 
> > couldn't find a way to prevent built-in language parsers from modifying 
> > highlighted syntax outside of the ranges captured by queries.
> > I used php-mode as a comparison. For example you can see how it behaves 
> > with 
> > the two files https://github.com/emacs-php/php-mode/blob/master/tests/8.0/
> > attribute/class.php or https://github.com/emacs-php/php-mode/blob/master/ 
> > tests/issue-66-namespace.php.
> > It is possible to set a php-ts-mode-disable-inject variable to enable or 
> > disable the parsers for html/css/javascript to see the differences.
> > 
> > What am I doing wrong?
> > Thank you
> 
> I haven’t try your mode yet, but the problem you described looks the same as 
> another bug report I just received a few days ago [1]. If they are indeed 
> caused by the same issue, you are not doing anything wrong. Instead, it’s due 
> to a bug in Emacs. 
> 
> Try defining the following workaround function, and add it as a query rule:
> 
> (defun test-php--clean-up-parser-range (&rest _)
>   (dolist (parser (mapcan (lambda (lang)
>                             (treesit-parser-list nil lang))
>                           '(html css javascript)))
>     (when (null (treesit-parser-included-ranges parser))
>       (treesit-parser-set-included-ranges
>        parser `((,(point-min) . ,(point-min)))))))
> 
> Add it like this (make sure it’s the last rule):
> 
> (setq-local treesit-range-settings
>             (treesit-range-rules
>              :embed 'html
>              :host 'php
>              '((text) @capture)
> 
>              :embed 'css
>              :host 'html
>              '((style_element (raw_text) @capture))
> 
>              :embed 'javascript
>              :host 'html
>              '((script_element (raw_text) @capture))
> 
>              #'test-php--clean-up-parser-range))
> 
> [1] https://github.com/casouri/expreg/issues/3
> 
> (Also, it seems that both of you got the idea of creating a php mode, maybe 
> it a good idea for you two to exchange a few ideas and what not.)
> 
> Yuan
> 






reply via email to

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