2022-08-04 01:11:51 +02:00
#+title : :tools tree-sitter
#+subtitle : Syntax and parsing, sitting in a tree...
#+created : August 17, 2021
#+since : 22.06.0 (#5401)
* Description :unfold:
This module adds [[https://tree-sitter.github.io/tree-sitter/ ][tree-sitter ]] support to Doom Emacs.
2021-08-21 21:19:27 +01:00
#+begin_quote
2023-09-16 20:19:11 +02:00
Tree sitter is a parser generator tool and an incremental parsing library.
2022-08-04 01:11:51 +02:00
It can build a concrete syntax tree for a source file and efficiently update
the syntax tree as the source file is edited. This allows for features of
the editor 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-08-04 01:11:51 +02:00
- Better syntax highlighting of supported languages.
- 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-08-04 01:11:51 +02:00
- [[doom-user: ][@jeetelongname ]]
2022-06-21 12:38:57 -07:00
2022-08-04 01:11:51 +02:00
[[doom-contrib-maintainer: ][Become a maintainer? ]]
2021-08-21 21:19:27 +01:00
2022-08-04 01:11:51 +02:00
** Module flags
/This module has no flags./
2022-06-21 12:38:57 -07:00
2022-08-04 01:11:51 +02:00
** Packages
2022-09-26 02:19:42 +08:00
- [[doom-package:evil-textobj-tree-sitter ]] if [[doom-module::editor evil +everywhere ]]
- [[doom-package:tree-sitter ]]
- [[doom-package:tree-sitter-langs ]]
2021-08-21 21:19:27 +01:00
2022-08-04 01:11:51 +02:00
** Hacks
/No hacks documented for this module./
2022-06-21 12:38:57 -07:00
2022-08-04 01:11:51 +02:00
** TODO Changelog
# This section will be machine generated. Don't edit it by hand.
/This module does not have a changelog yet./
2021-08-21 21:19:27 +01:00
2022-08-04 01:11:51 +02:00
* Installation
[[id:01cffea4-3329-45e2-a892-95a384ab2338 ][Enable this module in your ~doom!~ block. ]]
2022-06-21 12:38:57 -07:00
2022-08-04 01:11:51 +02:00
This module has no direct requirements, but some languages may have their own to
fulfill before tree-sitter support works for them. Run ~$ doom doctor~ to find
out what you're missing.
2021-08-21 21:19:27 +01:00
2022-08-04 01:11:51 +02:00
* TODO Usage
#+begin_quote
2023-09-16 20:19:11 +02:00
/This module's usage documentation is incomplete./ [[doom-contrib-module: ][Complete it? ]]
2022-08-04 01:11:51 +02:00
#+end_quote
2022-06-21 12:38:57 -07:00
2022-08-04 01:11:51 +02:00
** 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~ .
2022-08-04 01:11:51 +02:00
To enable tree-sitter support for specific languages, add the =+tree-sitter=
flag to their respective Doom modules. 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]].
2022-08-04 01:11:51 +02:00
#+begin_quote
2023-09-16 20:19:11 +02:00
Only languages with parsers in Emacs have text object support at the moment.
2022-08-04 01:11:51 +02:00
#+end_quote
2022-06-21 12:38:57 -07:00
2022-08-04 01:11:51 +02:00
Currently text objects are bound to:
2021-09-21 19:40:00 +01:00
| key | text object |
|-----+---------------------|
2022-08-04 01:11:51 +02:00
| [[kbd:][A]] | parameter list |
| [[kbd:][f]] | function definition |
| [[kbd:][F]] | function call |
| [[kbd:][C]] | class |
| [[kbd:][c]] | comment |
| [[kbd:][v]] | conditional |
| [[kbd:][l]] | loop |
2021-08-20 23:26:12 +01:00
2022-08-04 01:11:51 +02:00
They are used in a container context (not [[kbd: ][vf ]], but [[kbd: ][vaf ]] or [[kbd: ][vif ]]).
2021-08-20 23:26:12 +01:00
2022-01-24 18:38:18 +00:00
** Goto certain nodes
2022-08-04 01:11:51 +02:00
To jump to the next/previous node, type in a buffer by using [[kbd:][[g ]] or [[kbd:][]g]]
2022-06-21 12:38:57 -07:00
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:
| key | text object |
|-----+----------------|
2022-08-04 01:11:51 +02:00
| [[kbd:][a]] | parameter list |
| [[kbd:][f]] | function |
| [[kbd:][F]] | function call |
| [[kbd:][c]] | comment |
| [[kbd:][C]] | class |
| [[kbd:][v]] | conditional |
| [[kbd:][l]] | loop |
* TODO Configuration
#+begin_quote
2023-09-16 20:19:11 +02:00
/This module's configuration documentation is incomplete./ [[doom-contrib-module: ][Complete it? ]]
2022-08-04 01:11:51 +02:00
#+end_quote
2022-06-21 12:38:57 -07:00
2022-08-04 01:11:51 +02:00
** Rebind text objects
Rebinding keys works the same as rebinding any other key, but text-object keys
must be bound on one or both of the ~+tree-sitter-inner-text-object-map~ or
~+tree-sitter-outer-text-object-map~ keymaps:
2022-04-10 18:13:20 +01:00
#+begin_src emacs-lisp
(map! (:map +tree-sitter-outer-text-objects-map
"f" (evil-textobj-tree-sitter-get-textobj "call.inner")
"F" (evil-textobj-tree-sitter-get-textobj "function.inner"))
(:map +tree-sitter-inner-text-objects-map
"f" (evil-textobj-tree-sitter-get-textobj "call.inner")
"F" (evil-textobj-tree-sitter-get-textobj "function.inner")))
#+end_src
2022-08-04 01:11:51 +02:00
** Add your own text objects
To [[https://github.com/meain/evil-textobj-tree-sitter#custom-textobjects ][add your own custom text objects ]], bind them to ~+tree-sitter-{inner,
outer}-text-objects-map~:
2021-12-24 14:13:34 +00:00
#+begin_src emacs-lisp
2022-08-04 01:11:51 +02:00
(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]))))
2021-12-24 14:13:34 +00:00
#+end_src
2022-07-25 00:54:26 +01:00
** Configuring Tree sitter highlighting
Highlighting is controlled by the variable =+tree-sitter-hl-enabled-modes=
This list allows you to whitelist, blacklist, fully enable or fully disable
tree-sitter highlighting for all major modes
To use highlighting in select modes add major-modes to
~+tree-sitter-hl-enabled-modes~ . This disables highlighting in all other modes.
The symbol that should be used is the major mode symbol, *not the package
symbol*.
2021-12-24 14:13:34 +00:00
#+begin_src emacs-lisp
2022-07-25 00:54:26 +01:00
(setq +tree-sitter-hl-enabled-modes '(python-mode go-mode))
2021-12-24 14:13:34 +00:00
#+end_src
2022-07-25 00:54:26 +01:00
If you want to disallow highlighting in certain modes then the car of
~+tree-sitter-hl-enabled-modes~ should be =not= . This enables highlighting in
all modes except the ones disallowed.
2021-12-24 14:13:34 +00:00
#+begin_src emacs-lisp
2022-07-25 00:54:26 +01:00
(setq +tree-sitter-hl-enabled-modes '(not web-mode typescript-tsx-mode))
2021-12-24 14:13:34 +00:00
#+end_src
2022-07-25 00:54:26 +01:00
If ~+tree-sitter-hl-enabled-modes~ is set to ~nil~ or ~t~ it will fully disable
or fully enable highlighting in every tree sitter enabled language respectively.
2021-12-24 14:13:34 +00:00
* Troubleshooting
2022-08-04 01:11:51 +02:00
[[doom-report: ][Report an issue? ]]
2022-06-21 12:38:57 -07:00
2022-08-04 01:11:51 +02:00
** =(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! ]]
2022-08-04 01:11:51 +02:00
* Frequently asked questions
/This module has no FAQs yet./ [[doom-suggest-faq: ][Ask one? ]]
* TODO Appendix
#+begin_quote
2023-09-16 20:19:11 +02:00
This module has no appendix yet. [[doom-contrib-module: ][Write one? ]]
2022-08-04 01:11:51 +02:00
#+end_quote