Treesitter¶
See also
- How To Get Started with Tree Sitter
Article on Matering Emacs
- Combobulate: Structured Movement and Editing with Tree-Sitter
Another introducing the
combobulatepackage
As of version 29.1 Emacs comes with support out of the box (assuming it has been compiled with the --with-tree-sitter flag)
init.el(use-package alc-treesitter
:load-path "lisp"
:if (treesit-available-p))
Installing Grammars¶
While Emacs supports the tree-sitter library itself, it does not come with any grammars which need to be installed separately. Thankfully, Emacs does provide a few utilities to help with this
lisp/alc-treesitter.el(setq treesit-language-source-alist
'((python "https://github.com/tree-sitter/tree-sitter-python" "v0.20.4")
(typescript "https://github.com/tree-sitter/tree-sitter-typescript" "v0.20.3" "typescript/src")))
The treesit-language-source-alist variable tells Emacs where it can find a given grammar and what language it is for.
Once defined (and assuming you have the necessary dependencies) you can use M-x treesit-install-language-grammar command to install one of the grammars you defined.
Alternatively this snippet will install them all in one go
(mapc #'treesit-install-language-grammar (mapcar #'car treesit-language-source-alist))
Major Modes¶
Switching to tree-sitter powered major modes by default would not be backwards compatible, so if we actually want to make use of the tree-sitter support we need to opt into it.
lisp/alc-treesitter.el(if (treesit-language-available-p 'python)
(add-to-list 'major-mode-remap-alist '(python-mode . python-ts-mode)))
Combobulate¶
mickeynp/combobulate provides a nice set of movement and editing commands that leverage the parse tree generated by tree sitter.
lisp/alc-treesitter.el (use-package combobulate
:vc (:url "https://github.com/mickeynp/combobulate" :rev "master")
:hook ((python-ts-mode . combobulate-mode)
(typescript-ts-mode . combobulate-mode)))