emacs-devel
[Top][All Lists]
Advanced

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

Tree-sitter central configuration variable


From: Yuan Fu
Subject: Tree-sitter central configuration variable
Date: Thu, 28 Nov 2024 21:49:33 -0800


> On Nov 23, 2024, at 4:20 AM, Eli Zaretskii <eliz@gnu.org> wrote:
> 
>> From: Yuan Fu <casouri@gmail.com>
>> Date: Sun, 10 Nov 2024 00:04:00 -0800
>> Cc: Vincenzo Pupillo <v.pupillo@gmail.com>,
>> emacs-devel@gnu.org
>> 
>> 
>> 
>>> On Nov 9, 2024, at 12:54 AM, Eli Zaretskii <eliz@gnu.org> wrote:
>>> 
>>>> From: Yuan Fu <casouri@gmail.com>
>>>> Date: Sat, 9 Nov 2024 00:36:31 -0800
>>>> Cc: Eli Zaretskii <eliz@gnu.org>,
>>>> emacs-devel@gnu.org
>>>> 
>>>>>> Also, we could support a value of that variable which is a list, like
>>>>>> we do with font-lock-maximum-decoration.  This will be more useful
>>>>>> than making the variable buffer-local, since presumably your
>>>>>> preferences are per-mode, not per-buffer.  
>>>>> Yes, this would be a better solution.
>>>>> 
>>>>>> Yuan, WDYT?
>>>>>> 
>>>> 
>>>> Perhaps even per-language, for multi-language modes. I think it’s a valid 
>>>> use case. IMO, specify the level by language is better than mode. For 
>>>> single language modes, using the language is equivalent to using the mode; 
>>>> for multi-language modes, using the language allows more flexibility. 
>>> 
>>> Patches welcome.
>> 
>> I’ll prepare one soon.
> 
> Did you have time to work on this?

Actually, I want to expand this to something that allows users to configure 
tree-sitter modes and toggle on/off tree-sitter features.

Since Emacs 29, I see many people ask about how to configure tree-sitter modes 
by setting some variable. It seems that people much prefer setting a variable 
than adding a major mode hook that calls some functions. Also, admittedly 
adding custom font-lock or indent rules aren’t very straightforward for users.

In Emacs 29, we went with the major-mode hook approach for customization since 
the major mode inheritance situation wasn’t yet clear, the hook approach 
provides most flexibility, and we don’t really know what we want. But at this 
point I think we can add another layer of convenience. As long as this 
convenience layer handles 90% of the use-cases and doesn’t add any confusion, 
it’ll be a net-gain.

What do you guys think about something like this:

(setq treesit-global-configuration
      '((c-ts-mode
         ;; Set treesit-font-lock-level to 4
         (font-lock-level . 4)
         ;; Disable tree-sitter’s outline support
         (outline . disable)
         ;; Enable these features on top of the default ones.
         (font-lock-enable . (function property variable))
         ;; Disable these features.
         (font-lock-disable . (definition))
         ;; Add extra font-lock rules
         (font-lock-extra-rules
          ( :feature 'my-rules
            :language 'c
            ((some_query) @some-face)))
         (simple-indent-extra-rules
          (c
           (matcher anchor offset))
          (d
           (matcher anchor offset)))
         )))

This config will apply to c-ts-mode or its derived mode. Users can add extra 
font-lock and indent rules by setting this variable.

One thing I don’t like is how it handles languages. In this POC 
language-specific settings are nested under the mode. I’m ok with mode-language 
hierarchy, but the nesting adds a lot of nesting levels to the variable. And 
the language nesting isn’t consistent, some settings have language nesting, 
some don’t.

Implementation wise, it shouldn’t be difficult. We just make 
treesit-major-mode-setup aware of this variable and use it to override stuff 
when setting things up.

Yuan


reply via email to

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