2021-08-21 21:19:27 +01:00
#+TITLE : tools/tree-sitter
#+DATE : August 17, 2021
#+SINCE : 3.0.0
#+STARTUP : inlineimages nofold
* Table of Contents :TOC_3:noexport:
- [[#description ][Description ]]
- [[#maintainers ][Maintainers ]]
- [[#module-flags ][Module Flags ]]
- [[#plugins ][Plugins ]]
- [[#prerequisites ][Prerequisites ]]
- [[#features ][Features ]]
2021-08-20 23:05:34 +01:00
- [[#language-support ][Language support ]]
- [[#text-objects ][Text Objects ]]
2022-01-24 18:38:18 +00:00
- [[#goto-certain-nodes ][Goto certain nodes ]]
2021-08-21 21:19:27 +01:00
- [[#configuration ][Configuration ]]
2022-04-10 18:13:20 +01:00
- [[#rebinding-text-objects ][Rebinding text objects ]]
2021-12-24 14:13:34 +00:00
- [[#adding-your-own-text-objects ][Adding your own text objects ]]
- [[#disabling-highlighting-for-certain-modes ][Disabling highlighting for certain modes ]]
2021-08-21 21:19:27 +01:00
- [[#troubleshooting ][Troubleshooting ]]
2021-12-24 14:13:34 +00:00
- [[#error-bad-bounding-indices-0-1 ][=(error "Bad bounding indices: 0, 1")= ]]
2021-08-21 21:19:27 +01:00
* Description
2022-06-21 12:38:57 -07:00
This module adds [[https://tree-sitter.github.io/tree-sitter/ ][tree-sitter ]] support to Doom:
2021-08-21 21:19:27 +01:00
#+begin_quote
Tree sitter is a parser generator tool and an incremental parsing library. It
can build a concrete syntax tree for a source file and efficiently update the
2021-12-24 14:13:34 +00:00
syntax tree as the source file is edited. This allows for features of the editor
2022-06-21 12:38:57 -07:00
to become syntax aware.
2021-08-21 21:19:27 +01:00
#+end_quote
2021-08-22 23:13:54 +01:00
It includes:
2022-06-21 12:38:57 -07:00
+ Better syntax highlighting of supported languages.
2021-08-22 23:13:54 +01:00
+ Structural text objects to manipulate functions statements and other code
2022-06-21 12:38:57 -07:00
structures like any other text object.
2021-08-21 21:19:27 +01:00
** Maintainers
2022-06-21 12:38:57 -07:00
+ @jeetelongname
2021-08-21 21:19:27 +01:00
** Module Flags
2022-06-21 12:38:57 -07:00
2021-08-21 21:19:27 +01:00
This module provides no flags.
** Plugins
2022-06-21 12:38:57 -07:00
2021-08-21 21:19:27 +01:00
+ [[https://github.com/emacs-tree-sitter/elisp-tree-sitter ][tree-sitter ]]
+ [[https://github.com/emacs-tree-sitter/tree-sitter-langs ][tree-sitter-langs ]]
+ [[https://github.com/meain/evil-textobj-tree-sitter ][evil-textobj-tree-sitter ]]* (=:editor evil +everywhere= )
* Prerequisites
2022-06-21 12:38:57 -07:00
2022-02-15 19:52:39 +00:00
This module has no prerequisites.
2021-08-21 21:19:27 +01:00
2021-08-20 23:05:34 +01:00
* Features
** Language support
2022-06-21 12:38:57 -07:00
Currently Emacs tree sitter has [[https://github.com/emacs-tree-sitter/tree-sitter-langs/tree/master/repos ][parsers for these languages ]], and syntax
highlighting support for [[https://github.com/emacs-tree-sitter/tree-sitter-langs/tree/master/queries ][these languages ]] as well as ~typescript-tsx-mode~ .
To enable tree sitter for individual languages, add the =+tree-sitter= flag. Check
the module readme of your language for support.
2021-08-20 23:05:34 +01:00
** Text Objects
2022-06-21 12:38:57 -07:00
Not all languages support all text objects (yet). [[https://github.com/nvim-treesitter/nvim-treesitter-textobjects#built-in-textobjects][Here is a table of the text
object languages support]].
Note: Only languages with parsers in Emacs have text object support currently.
2021-08-22 23:13:54 +01:00
Currently text objects are bound to:
2022-06-21 12:38:57 -07:00
2021-09-21 19:40:00 +01:00
| key | text object |
|-----+---------------------|
2022-06-21 12:38:57 -07:00
| =A= | parameter list |
| =f= | function definition |
| =F= | function call |
| =C= | class |
| =c= | comment |
| =v= | conditional |
| =l= | loop |
2021-08-20 23:26:12 +01:00
They are used in a container context (not =vf= but =vaf= or =vif= )
2022-01-24 18:38:18 +00:00
** Goto certain nodes
2022-06-21 12:38:57 -07:00
You can also jump to the next / previous node type in a buffer by using =[g= or =]g=
respectfully, the following key will correspond to the text object you want to
jump to.
2022-05-24 15:38:31 +01:00
Currently keys are bound to:
2022-06-21 12:38:57 -07:00
2022-05-24 15:38:31 +01:00
| key | text object |
|-----+----------------|
2022-06-21 12:38:57 -07:00
| =a= | parameter list |
| =f= | function |
| =F= | function call |
| =c= | comment |
| =C= | class |
| =v= | conditional |
| =l= | loop |
2022-01-24 18:38:18 +00:00
2021-12-19 23:05:15 +00:00
* Configuration
2022-04-10 18:13:20 +01:00
** Rebinding text objects
2022-06-21 12:38:57 -07:00
Rebinding keys is the same as any other key, but do note that they need to be
bound to the keymaps ~+tree-sitter-inner-text-object-map~ or
~+tree-sitter-outer-text-object-map~ .
2022-04-10 18:13:20 +01:00
#+begin_src emacs-lisp
(map! (:map +tree-sitter-outer-text-objects-map
"f" nil
"f" (evil-textobj-tree-sitter-get-textobj "call.inner")
"F" nil
"F" (evil-textobj-tree-sitter-get-textobj "function.inner"))
(:map +tree-sitter-inner-text-objects-map
"f" nil
"f" (evil-textobj-tree-sitter-get-textobj "call.inner")
"F" nil
"F" (evil-textobj-tree-sitter-get-textobj "function.inner")))
#+end_src
2021-12-24 14:13:34 +00:00
** Adding your own text objects
2022-07-18 18:23:53 +01:00
If you wish to [[https://github.com/meain/evil-textobj-tree-sitter#custom-textobjects ][add your own custom text objects ]] then you need to bind them to
2022-06-21 12:38:57 -07:00
~+tree-sitter-{inner, outer}-text-objects-map~ . For example:
2021-12-24 14:13:34 +00:00
#+begin_src emacs-lisp
(map! (:map +tree-sitter-outer-text-objects-map
"m" (evil-textobj-tree-sitter-get-textobj "import"
'((python-mode . [(import_statement) @import])
(rust-mode . [(use_declaration) @import])))))
#+end_src
** Disabling highlighting for certain modes
2022-06-21 12:38:57 -07:00
If you want to disable highlighting by default you can do:
2021-12-24 14:13:34 +00:00
#+begin_src emacs-lisp
(after! MODE-PACKAGE
(tree-sitter-hl-mode -1))
#+end_src
2022-06-21 12:38:57 -07:00
If you only want it for certain modes, then:
2021-12-24 14:13:34 +00:00
#+begin_src emacs-lisp
2022-04-02 02:16:11 +01:00
(remove-hook 'tree-sitter-after-on-hook #'tree-sitter-hl-mode)
2021-12-24 14:13:34 +00:00
2022-04-02 02:16:11 +01:00
(add-hook 'MAJOR-MODE-HOOK #'tree-sitter-hl-mode)
2021-12-24 14:13:34 +00:00
#+end_src
* Troubleshooting
** =(error "Bad bounding indices: 0, 1")=
2022-06-21 12:38:57 -07:00
This means that the text object does not have the underlying query needed. This
can be fixed by either adding in a custom query (which would override the
current key bound) or [[https://github.com/nvim-treesitter/nvim-treesitter-textobjects/ ][contributing upstream! ]]