all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* How to properly use treesit-range-rules ?
@ 2023-09-18 20:58 Vincenzo Pupillo
  2023-09-19  4:11 ` Yuan Fu
  0 siblings, 1 reply; 3+ messages in thread
From: Vincenzo Pupillo @ 2023-09-18 20:58 UTC (permalink / raw)
  To: emacs-devel

[-- Attachment #1: Type: text/plain, Size: 1339 bytes --]

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

Vincenzo


[-- Attachment #2: with_html_etc.png --]
[-- Type: image/png, Size: 16941 bytes --]

[-- Attachment #3: without_html_etc.png --]
[-- Type: image/png, Size: 16810 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: How to properly use treesit-range-rules ?
  2023-09-18 20:58 How to properly use treesit-range-rules ? Vincenzo Pupillo
@ 2023-09-19  4:11 ` Yuan Fu
  2023-09-19 13:09   ` Vincenzo Pupillo
  0 siblings, 1 reply; 3+ messages in thread
From: Yuan Fu @ 2023-09-19  4:11 UTC (permalink / raw)
  To: Vincenzo Pupillo; +Cc: emacs-devel



> 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


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: How to properly use treesit-range-rules ?
  2023-09-19  4:11 ` Yuan Fu
@ 2023-09-19 13:09   ` Vincenzo Pupillo
  0 siblings, 0 replies; 3+ messages in thread
From: Vincenzo Pupillo @ 2023-09-19 13:09 UTC (permalink / raw)
  To: Yuan Fu; +Cc: emacs-devel

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
> 






^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2023-09-19 13:09 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-09-18 20:58 How to properly use treesit-range-rules ? Vincenzo Pupillo
2023-09-19  4:11 ` Yuan Fu
2023-09-19 13:09   ` Vincenzo Pupillo

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.