Starting with Emacs 29, we've got native TreeSitter support using
treesit. My question is: how do I know I can use
treesit-* functions in the current buffer? The feature check only checks if Emacs itself has been compiled with treesit-support. Here's what happens when you ask
*scratch* whether it has treesit enabled:
major-mode ; ⇒ lisp-interaction-mode (treesit-available-p) ; ⇒ t
My current workaround is to check if the treesit language at point is defined:
(defun treesit-enabled-p () "Checks if the current buffer has a treesit parser." (and (fboundp 'treesit-available-p) (treesit-available-p) (treesit-language-at (point))))
This yields the correct truth conditions, but it doesn't seem terribly idiomatic to query the language at point. Is there a better way?
treesit-*functions? Technically you can always use those functions. For example, you can just call
(treesit-node-at (point)), and it will return the syntax node if the major modes has setup the parser, etc; if there's not a parser in the buffer, the function simply returns nil.
treesit-*functions! And fall back to using something else otherwise. I can use any of the treesit-functions (that don't throw errors) to accomplish that, but there doesn't seem to be a "canonical" way.
(treesit-parser-list)and see if it's empty. Or you can define a function that uses tree-sitter and call it first, if it returns nil, try the fallback, there's nothing wrong with that.