From: Yuan Fu <casouri@gmail.com>
To: Ihor Radchenko <yantar92@gmail.com>
Cc: emacs-devel <emacs-devel@gnu.org>, Theodor Thornhill <theo@thornhill.no>
Subject: Re: Recent updates to tree-sitter branch
Date: Tue, 27 Sep 2022 15:28:12 -0700 [thread overview]
Message-ID: <C883B674-13D7-4C1E-AE21-31FA768378FD@gmail.com> (raw)
In-Reply-To: <87leq65v3t.fsf@localhost>
> On Sep 26, 2022, at 2:43 AM, Ihor Radchenko <yantar92@gmail.com> wrote:
>
> Yuan Fu <casouri@gmail.com> writes:
>
>>> treesit-font-lock-rules rules take a form of
>>> (MATCHER FACENAME) or (MATCHER FUNCTION)
>>>
>>> where MATCHER can only be a query.
>>>
>>> Is there any reason why MATCHER in treesit-font-lock-rules cannot be a
>>> function with access to the fontified node?
>>
>> Hmm, I’m not sure what do you mean. The whole thing passed to treesit-font-lock-rules is a single query, and we can’t really change the query syntax, that’s defined by tree-sitter. Basically in a query you have patterns paired with capture names, if the pattern matches to a node, that node is returned with corresponding capture name tagged on it. For font-lock, we just use face names as capture names, and when a query returns captured nodes, fontify the node with its capture name, aka a face (or a function).
>
> What I am asking is an extra dynamic condition in addition to the query.
> For example:
> 1. Only apply FACENAME for nodes matching QUERY, but only when Elisp
> variable is non-nil
>
> 2. Only apply FACENAME for nodes matching QUERY, which are in the second
> half of the buffer
>
> 3. Only apply FACENAME for notes matching QUERY, which also have a field
> matching a dynamically assigned regexp.
>
> Essentially any condition that is not covered by the QUERY, but can be
> checked in Elisp given that node object is passed to the test function.
These can be achieved by using a function, no? You do need to declare global functions for them, but it shouldn’t be a problem. Besides, as I said, the query syntax is not something we can change. The freedom we have is how do we use the capture names. We can’t extend the query with arbitrary lisp.
>
>>> Further, can OVERRIDE FLAG of the MATCH-HIGHLIGHT as in
>>> font-lock-keywords be supported?
>>>
>>> "If OVERRIDE is t, existing fontification can be overwritten. If
>>> keep, only parts not already fontified are highlighted. If prepend or
>>> append, existing fontification is merged with the new, in which the new
>>> or existing fontification, respectively, takes precedence.”
>>
>> I can do that, but would it be really useful? Unlike regex font-lock which is used for so many different things, tree-sitter font-lock is, IMO, only used to apply a base layer of language-specific highlight. How would one use the override feature in this scenario?
>
> For example, consider a function definition with docstring field.
> Imagine that you want the function definition to have gray background,
> but the docstring to have yellow background. OVERRIDE t is how this is
> usually implemented in font-lock-keywords.
The pattern that comes after will override patterns that come before. By the nature of parse trees, for any node A and another smaller node B, B is either completely contained in A or completely outside A. So I think the override relationship is enough.
Yuan
next prev parent reply other threads:[~2022-09-27 22:28 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-09-25 4:27 Recent updates to tree-sitter branch Yuan Fu
2022-09-25 6:17 ` Ihor Radchenko
2022-09-26 8:35 ` Yuan Fu
2022-09-26 9:43 ` Ihor Radchenko
2022-09-27 22:28 ` Yuan Fu [this message]
2022-09-29 4:01 ` Ihor Radchenko
2022-09-30 21:03 ` Yuan Fu
2022-10-01 4:20 ` Ihor Radchenko
2022-10-02 3:46 ` Yuan Fu
2022-10-02 7:33 ` Ihor Radchenko
2022-10-02 22:54 ` Yuan Fu
2022-10-03 5:58 ` Ihor Radchenko
2022-10-04 16:58 ` Yuan Fu
2022-09-29 10:13 ` Aurélien Aptel
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=C883B674-13D7-4C1E-AE21-31FA768378FD@gmail.com \
--to=casouri@gmail.com \
--cc=emacs-devel@gnu.org \
--cc=theo@thornhill.no \
--cc=yantar92@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.