emacs-devel
[Top][All Lists]
Advanced

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

Re: Tree-sitter api


From: Yuan Fu
Subject: Re: Tree-sitter api
Date: Sun, 8 Aug 2021 19:06:55 -0500


> On Aug 8, 2021, at 6:24 PM, Stefan Monnier <monnier@iro.umontreal.ca> wrote:
> 
> Yuan Fu [2021-08-08 17:56:33] wrote:
>>> On Aug 7, 2021, at 10:47 AM, Stefan Monnier <monnier@iro.umontreal.ca> 
>>> wrote:
>>>> We should have a user option to control tree-sitter on major mode
>>>> level. Maybe an alist where each car is a major node symbol and each cdr is
>>>> a Boolean value toggling tree-sitter for that node.
>>> The more traditional approach is to use a buffer-local var set by the
>>> major mode or set via (add-hook '<MODE>-hook ...).
>> The major-mode would setup things like font-lock-defaults and
>> tree-sitter-defaults before major-mode-hook runs, so I think
>> enabling/disabling tree-sitter in the hook is too late, no?
> 
> I don't see why.  Presumably the major mode would set some vars relevant
> to the tree-sitter support, but then whether those vars are used will
> depend on the buffer-local boolean var (let's call it `tree-sitter-mode`).
> 
> I'm sure there will be issues w.r.t initialization order, e.g. in case
> `font-lock-mode` is enabled before `tree-sitter-mode`, but that doesn't
> seem very serious (`font-lock-mode` doesn't do much anyway, since the
> real work is postponed until the next redisplay by jit-lock, so we could
> "refresh" font-lock settings fairly cheaply within `tree-sitter-mode`).

Instead of a tree-sitter-mode, I made font-lock use tree-sitter features in 
addition to using keywords. Basically I added another fontification pass 
(tree-sitter pass) in addition to the current two, syntactic pass and regex 
pass. 
(Syntactic pass is probably unnecessary if tree-sitter is enabled, tho). This 
way someone can still add regexp-based fontification even he uses tree-sitter 
for “standard” fontification.

And under this scheme, a major-mode would want something like this in the 
major-mode definition:

(if (tree-sitter-should-enable-p)
      (progn (setq-local font-lock-tree-sitter-defaults
                         '((ts-c-tree-sitter-settings-1)))
             (setq-local font-lock-defaults
                         (ignore t nil nil nil)))
    (setq-local font-lock-defaults
                '((c-font-lock-keywords
                   c-font-lock-keywords-1
                   c-font-lock-keywords-2
                   c-font-lock-keywords-3)
                  nil nil
                  ((95 . "w")
                   (36 . "w"))
                  c-beginning-of-syntax
                  (font-lock-mark-block-function . c-mark-function))))

In this scheme, changing whether to enable tree-sitter is too late in 
major-mode-hook (not impossible, of course).

Yuan


reply via email to

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