From: Theodor Thornhill <theo@thornhill.no>
To: Yuan Fu <casouri@gmail.com>
Cc: emacs-devel@gnu.org
Subject: Re: Disable tree-sitter font-locking for smaller ranges
Date: Mon, 17 Oct 2022 07:49:35 +0200 [thread overview]
Message-ID: <EFF4AD10-76E0-453C-AFC8-3D863E733F52@thornhill.no> (raw)
In-Reply-To: <672FF8D1-E5FB-44D8-8E6D-38EB94843B3B@gmail.com>
On 17 October 2022 07:04:58 CEST, Yuan Fu <casouri@gmail.com> wrote:
>
>
>> On Oct 16, 2022, at 3:32 AM, Theodor Thornhill <theo@thornhill.no> wrote:
>>
>>
>> Hi Yuan!
>>
>> I've been trying to tweak the font-locking for tree-sitter in
>> js/ts-mode, and I'm still struggling with the template strings. Is
>> there a way to _disable_ font-locking for smaller ranges?
>>
>> Let's say you have this string:
>> ```
>> `Some string with ${5+5} many template substitutions ${foo().bar().baz()}`;
>> ```
>>
>> This string will match something like:
>> ```
>> (template_string (template_substitution) :*)
>> ```
>>
>> If you use this as a query:
>> ```
>> (template_string) @font-lock-string-face
>> ```
>>
>> Everything inside the string is in string-face.
>>
>> If you add
>> ```
>> (template_substitution
>> "${" @font-lock-constant-face
>> (_)
>> "}" @font-lock-constant-face)
>> ```
>>
>> You get font-locking in the ranges inside the squigglies, as expected.
>> However, if there isn't defined any rules for say, "(), . []" etc, the
>> template_string capture will bleed into the substitution, because its
>> range suggests it should. It would be nice to say something like:
>>
>> ```
>> (defvar fonts ()
>> (treesit-font-lock-rules
>> :language 'tsx
>> :override t
>> :feature 'basic
>> '((template_string (_):* @disabled) @font-lock-string-face
>> (template_substitution ["${" "}"] @font-lock-constant-face)))
>> ```
>>
>> to ensure that whatever is inside the wildcard match will _not_ be
>> considered for the string face, but whatever is still inside the bigger
>> range will. Is this currently possible? If not, is
>> it possible to add? I guess I could make a function that would remove
>> the range, but that seems fiddly and error prone.
>>
>> I've tried fiddling with the :feature flag in 'treesit-font-lock-rules',
>> but I never found a combination that did what I wanted.
>>
>> Adding a small image of a stupid snipped just to make the point more visual
>
>The image you attached looks perfectly fine to me. Do you not want to font-lock what’s inside a substitution? I’m not exactly sure what result you want to archive.
>
All the parens, braces, equal, semicolons should be white, as they would in the source file outside of the template string. But the string outside of the ${...} should be string colored.
>If you don’t want font-lock in substitutions, you can put
>
>> '((template_string) @font-lock-string-face
>> (template_substitution ["${" "}"] @font-lock-constant-face))
>
>
>After all other queries, and mark these two with :override t. That should do it.
>
Ill try that!
>Also, when I was browsing tsx’s grammar file, I didn’t find definition of template_substitution at all. That means at least some part of js and tsx’s grammar are not interchangeable.
>
>Yuan
They do some inheritance shenanigans in their files. Most of the grammar is from tree-sitter-Javascript.
Theo
next prev parent reply other threads:[~2022-10-17 5:49 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-10-16 10:32 Disable tree-sitter font-locking for smaller ranges Theodor Thornhill
2022-10-17 5:04 ` Yuan Fu
2022-10-17 5:49 ` Theodor Thornhill [this message]
2022-10-17 6:01 ` Yuan Fu
2022-10-17 6:33 ` Theodor Thornhill
2022-10-17 9:00 ` Yuan Fu
2022-10-17 9:41 ` Theodor Thornhill
2022-10-17 11:02 ` Theodor Thornhill via Emacs development discussions.
2022-10-18 0:20 ` Yuan Fu
2022-10-18 5:04 ` Theodor Thornhill
2022-10-18 20:07 ` Theodor Thornhill
2022-10-18 20:44 ` Yuan Fu
2022-10-18 5:06 ` Yuan Fu
2022-10-18 1:23 ` Trey Peacock
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
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=EFF4AD10-76E0-453C-AFC8-3D863E733F52@thornhill.no \
--to=theo@thornhill.no \
--cc=casouri@gmail.com \
--cc=emacs-devel@gnu.org \
/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 public inbox
https://git.savannah.gnu.org/cgit/emacs.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).