From: Yuan Fu <casouri@gmail.com>
To: Vincenzo Pupillo <v.pupillo@gmail.com>
Cc: emacs-devel@gnu.org
Subject: Re: How to properly use treesit-range-rules ?
Date: Mon, 18 Sep 2023 21:11:42 -0700 [thread overview]
Message-ID: <769ACC51-F33A-425F-8638-47BFFBD5D62F@gmail.com> (raw)
In-Reply-To: <8289988.T7Z3S40VBb@fedora>
> 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
next prev parent reply other threads:[~2023-09-19 4:11 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-09-18 20:58 How to properly use treesit-range-rules ? Vincenzo Pupillo
2023-09-19 4:11 ` Yuan Fu [this message]
2023-09-19 13:09 ` Vincenzo Pupillo
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=769ACC51-F33A-425F-8638-47BFFBD5D62F@gmail.com \
--to=casouri@gmail.com \
--cc=emacs-devel@gnu.org \
--cc=v.pupillo@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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.