emacs-devel
[Top][All Lists]
Advanced

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

Re: Disable tree-sitter font-locking for smaller ranges


From: Yuan Fu
Subject: Re: Disable tree-sitter font-locking for smaller ranges
Date: Sun, 16 Oct 2022 23:01:51 -0700


> On Oct 16, 2022, at 10:49 PM, Theodor Thornhill <theo@thornhill.no> wrote:
> 
> 
> 
> 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. 
> 
Ah, I finally get it. How about:

(template_string) @font-lock-string-face
(template_substitution) @default ; color everything in substitution white
(template_substitution ["${" "}"] @font-lock-constant-face)
;; rest font-lock

Yuan


reply via email to

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