unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* Disable tree-sitter font-locking for smaller ranges
@ 2022-10-16 10:32 Theodor Thornhill
  2022-10-17  5:04 ` Yuan Fu
  0 siblings, 1 reply; 14+ messages in thread
From: Theodor Thornhill @ 2022-10-16 10:32 UTC (permalink / raw)
  To: Yuan Fu; +Cc: emacs-devel

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


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

Theo



[-- Attachment #2: 2022-10-16_12-26.png --]
[-- Type: image/png, Size: 12890 bytes --]

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

end of thread, other threads:[~2022-10-18 20:44 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
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

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).