Use existing *-ts-mode
package
First, install packages in package.el
.
If package depends on treesit
instead of tree-sitter
, it’s required to copy and rename parser library files.
Using pre-compiled language grammars
You can find them on his Github releases page. You can also download the
tree-sitter-langs
package from MELPA, but I recommend you just download the shared libs directly instead, as you’ll in any event have to rename them and place the grammar libraries somewhere else.The names of the files are
<LANGUAGE>.so
(or with your platform’s equivalent extension) which is not in keeping with the expected naming style in Emacs. You must first rename them so they’re namedlibtree-sitter-<LANGUAGE>.so
. This is as good a time as any to learn how to bulk rename them with Emacs’sM-x dired
and the editable dired buffers feature.
Define your own new major mode for highlighting
Supported languages: https://github.com/emacs-tree-sitter/tree-sitter-langs/tree/master/queries
- Ensure
(package! tree-sitter-langs ...)
pins to the right commit (or a later commit) inpackages.el
and tree-sitter module is enabled ininit.el
. - Run
doom sync
if pin is updated, and verify that updates are applied to local Straight directories. - Define the major mode in
config.el
, or a custom Doom module withdefine-derived-mode
split intoautoload.el
.
To enable LSP instead, replace #'tree-sitter!
with #'lsp!
.
For Elisp gurus: Doom provides MAJOR-MODE-local-vars-hook, and we use it instead of MAJOR-MODE-hook because it runs later in the mode’s startup process (giving other functionality or packages — like
direnv
— time to configure the LSP client).
Lazy loading
Check if tsc-dyn-get-ensure
is defined on start to verify lazy loading.
Linux AArch64 support
See also https://github.com/emacs-tree-sitter/elisp-tree-sitter/issues/166#issuecomment-1522766887.
Add the following to config.el
, which is equivalent to use-package
’s :init
.
;; No binaries are provided for tree-sitter on AArch64 Linux
(setq tsc-dyn-get-from '(:compilation))
This does not fix missing binaries of tree-sitter-langs, so you need to build them yourself.