> Since Emacs 29, I see many people ask about how to configure
> tree-sitter modes by setting some variable.
This is not specific to tree-sitter.
> It seems that people much prefer setting a variable than adding
> a major mode hook that calls some functions.
But that boxes them into the subset of possibilities that have been
pre-imagined by those who designed the set of variables.
> 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)))
> )))
Sounds to me like this is inventing a new programming language, just one
that's a lot more restrictive than ELisp.
Is it really better than ELisp which could look like:
(add-hook 'c-ts-mode-hook #'my-c-ts-mode-preferences)
(defun my-c-ts-mode-preferences ()
(setq-local treesit-font-lock-level 4)
(treesit-outline-mode -1)
(treesit-font-lock-enable '(function property variable))
(treesit-font-lock-disable '(definition))
(treesit-font-lock-add-rules
'( :feature 'my-rules
:language 'c
((some_query) @some-face)))
(treesit-indent-add-rules
'((c
(matcher anchor offset))
(d
(matcher anchor offset)))
))
Admittedly, the add-hook/defun dance could be improved, and that would
benefit more than just tree-sitter. E.g. a new macro like
(defmacro custom-set-hook (hook &rest body)
(declare (indent 1) (debug (sexp def-body)))
(let ((funname (intern (format "custom-set-hook--%s" hook))))
`(progn (add-hook ',hook #',funnmame)
(defun ,funname () ,@body))))
> 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.
I don't really understand what you're referring to, but for
`treesit-font-lock-add-rules` would could try and auto-add the `:language`?
Stefan