Subject: Re: bug#77255: Treesit font-lock override for embed ranges

From: Yuan Fu <[email-masked]>
Date: Thu, 17 Apr 2025 16:40:24 -0700

> On Apr 9, 2025, at 10:31 AM, Juri Linkov <[email-masked]> wrote:
> 
> BTW, for testing multi-parser ranges I used such a trick that
> highlights different ranges using different background colors from hi-lock.
> Maybe something like this could be added to 'treesit-explore-mode'
> or 'treesit-inspect-mode':
> 
> diff --git a/lisp/treesit.el b/lisp/treesit.el
> index 8e57a6dae14..5a2721cdda4 100644
> --- a/lisp/treesit.el
> +++ b/lisp/treesit.el
> @@ -1036,6 +1026,9 @@ treesit--update-ranges-non-local
>                   (overlay-put ov 'treesit-parser embed-parser)
>                   (overlay-put ov 'treesit-parser-local-p nil)
>                   (overlay-put ov 'treesit-host-parser host-parser)
> +                  ;; (overlay-put ov 'font-lock-face (nth embed-level hi-lock-face-defaults))
> +                  (overlay-put ov 'font-lock-face (nth (length (memq embed-parser (treesit-parser-list))) hi-lock-face-defaults))
> +                  (overlay-put ov 'priority (+ 1000 embed-level))
>                   (overlay-put ov 'treesit-parser-ov-timestamp
>                                modified-tick)))))
>           ;; Set ranges for the embed parser.
> @@ -1130,6 +1123,9 @@ treesit--update-ranges-local
>                 (treesit-parser-set-embed-level embedded-parser embed-level)
>                 (overlay-put ov 'treesit-parser embedded-parser)
>                 (overlay-put ov 'treesit-parser-local-p t)
> +                ;; (overlay-put ov 'font-lock-face (nth embed-level hi-lock-face-defaults))
> +                (overlay-put ov 'font-lock-face (nth (length (memq embedded-parser (treesit-parser-list))) hi-lock-face-defaults))
> +                (overlay-put ov 'priority (+ 1000 embed-level))
>                 (overlay-put ov 'treesit-host-parser host-parser)
>                 (overlay-put ov 'treesit-parser-ov-timestamp
>                              modified-tick)

Yes, that’ll be a fantastic addition. I thought about something like this but didn’t have the time to implement it.

Yuan