0

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?

  • Feels a bit like a xy-question, what are you trying to accomplish once you know you can use `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. – Yuan Fu Mar 22 '23 at 08:05
  • Well, use those `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. – Aleksandar Dimitrov Mar 22 '23 at 12:10
  • The most "canonical" would be to check `(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. – Yuan Fu Mar 24 '23 at 01:20

2 Answers2

0

I needed to check if treesit is used at point (I'm using polymode):

(defun treesit-show-parser-used-at-point ()
  "Shows treesit parser used at point."
  (interactive)
  (if (and (fboundp 'treesit-available-p)
           (treesit-available-p))
      (message (format "%s" (treesit-language-at (point))))
    (message "treesit is not available")
  )
)
Adobe
  • 1,859
  • 13
  • 27
-1

From the information here, I would guess that

(alist-get major-mode tree-sitter-major-mode-language-alist)

should do the trick.

That list is customizable, so, if there existed an entry for 'emacs-lisp-mode' then I guess you could also add lisp-interaction-mode to the list. However, I don't see emacs-lisp included as a tree-sitter language name.

dalanicolai
  • 6,108
  • 7
  • 23
  • That is different to the integration built into Emacs 29. See also the notice at https://emacs-tree-sitter.github.io/ – phils Mar 21 '23 at 12:36
  • Ah thanks, clearly I missed that. Unfortunately, I am unable to build Emacs with tree-sitter support. I am on Fedora and installed libtree-sitter-devel 0.20.7 (and also tree-sitter-cli 0.20.7), but still Emacs does not detect it when running `.configure`. Luckily, I am not in a hurry to test it... – dalanicolai Mar 21 '23 at 17:43
  • I should've perhaps made more explicit that the first example serves to show that `treesit-available-p` is `t` *even though* the current major mode is `lisp-interaction-mode`, which is not a treesit buffer (and probably never going to be) – Aleksandar Dimitrov Mar 21 '23 at 22:10