2023-06-12 09:54:30 -06:00
|
|
|
*nvim-treesitter.txt* Treesitter parser and query installer for Neovim
|
2020-04-28 12:22:11 +02:00
|
|
|
|
2021-07-05 12:04:53 -05:00
|
|
|
Authors:
|
2021-04-15 16:51:36 +02:00
|
|
|
Kiyan Yazdani <yazdani.kiyan@protonmail.com>
|
|
|
|
|
Thomas Vigouroux <tomvig38@gmail.com>
|
|
|
|
|
Stephan Seitz <stephan.seitz@fau.de>
|
|
|
|
|
Steven Sojka <Steven.Sojka@tdameritrade.com>
|
|
|
|
|
Santos Gallegos <stsewd@protonmail.com>
|
|
|
|
|
https://github.com/nvim-treesitter/nvim-treesitter/graphs/contributors
|
2020-08-09 11:39:51 -05:00
|
|
|
|
|
|
|
|
Type |gO| to see the table of contents.
|
2020-04-28 12:22:11 +02:00
|
|
|
|
|
|
|
|
==============================================================================
|
2020-08-09 11:39:51 -05:00
|
|
|
INTRODUCTION *nvim-treesitter-intro*
|
2020-04-28 12:22:11 +02:00
|
|
|
|
2023-06-12 09:54:30 -06:00
|
|
|
Nvim-treesitter provides functionalities for managing treesitter parsers and
|
|
|
|
|
compatible queries for core features (highlighting, injections, fold, indent).
|
|
|
|
|
|
|
|
|
|
WARNING: This is work in progress and requires the latest commit on Neovim
|
|
|
|
|
`master`.
|
2020-04-28 12:22:11 +02:00
|
|
|
|
|
|
|
|
==============================================================================
|
2020-08-09 11:39:51 -05:00
|
|
|
QUICK START *nvim-treesitter-quickstart*
|
2020-04-28 12:22:11 +02:00
|
|
|
|
2025-04-27 13:02:32 +02:00
|
|
|
To configure `nvim-treesitter`, put this in your `init.lua` file:
|
2023-06-12 09:54:30 -06:00
|
|
|
>lua
|
2025-04-27 13:02:32 +02:00
|
|
|
require'nvim-treesitter'.setup {
|
2023-05-22 14:35:25 +01:00
|
|
|
-- A directory to install the parsers and queries to.
|
2023-06-12 09:54:30 -06:00
|
|
|
-- Defaults to the `stdpath('data')/site` dir.
|
|
|
|
|
install_dir = "/some/path/to/store/parsers",
|
2025-02-09 13:19:34 +01:00
|
|
|
-- List of parsers to ignore installing (for "stable" etc.)
|
2025-04-27 13:02:32 +02:00
|
|
|
ignore_install = { "some_parser" },
|
2023-06-06 11:02:28 +02:00
|
|
|
}
|
2021-12-17 20:49:06 -05:00
|
|
|
|
2025-04-27 13:02:32 +02:00
|
|
|
NOTE: You do not need to call `setup` to use this plugin with the default
|
|
|
|
|
settings!
|
|
|
|
|
|
|
|
|
|
Parsers and queries can then be installed with >lua
|
|
|
|
|
require'nvim-treesitter'.install { 'rust', 'javascript', 'zig' }
|
2020-08-09 11:39:51 -05:00
|
|
|
<
|
2025-04-27 13:02:32 +02:00
|
|
|
(This is a no-op if the parsers are already installed.)
|
|
|
|
|
|
2023-06-06 11:02:28 +02:00
|
|
|
To check installed parsers and queries, use `:checkhealth nvim-treesitter`.
|
2020-08-09 11:39:51 -05:00
|
|
|
|
|
|
|
|
==============================================================================
|
2023-06-12 09:54:30 -06:00
|
|
|
COMMANDS *nvim-treesitter-commands*
|
2020-08-09 11:39:51 -05:00
|
|
|
|
2023-06-12 09:54:30 -06:00
|
|
|
*:TSInstall*
|
|
|
|
|
:TSInstall {language} ... ~
|
2020-08-09 11:39:51 -05:00
|
|
|
|
2023-06-12 09:54:30 -06:00
|
|
|
Install one or more treesitter parsers.
|
|
|
|
|
You can use |:TSInstall| `all` to install all parsers. Use |:TSInstall!| to
|
|
|
|
|
force the reinstallation of already installed parsers.
|
2020-08-09 11:39:51 -05:00
|
|
|
|
2023-06-12 09:54:30 -06:00
|
|
|
*:TSUpdate*
|
|
|
|
|
:TSUpdate {language} ... ~
|
2021-11-20 22:31:22 +01:00
|
|
|
|
2023-06-12 09:54:30 -06:00
|
|
|
Update the installed parser for one more {language} or all installed parsers
|
|
|
|
|
if {language} is omitted. The specified parser is installed if it is not already
|
|
|
|
|
installed.
|
2021-11-20 22:31:22 +01:00
|
|
|
|
2023-06-12 09:54:30 -06:00
|
|
|
*:TSUninstall*
|
|
|
|
|
:TSUninstall {language} ... ~
|
2020-08-09 11:39:51 -05:00
|
|
|
|
2023-06-12 09:54:30 -06:00
|
|
|
Deletes the parser for one or more {language}. You can use 'all' for language
|
|
|
|
|
to uninstall all parsers.
|
2020-08-09 11:39:51 -05:00
|
|
|
|
2023-06-12 09:54:30 -06:00
|
|
|
==============================================================================
|
|
|
|
|
INDENTATION *nvim-treesitter-indentation*
|
2020-08-09 11:39:51 -05:00
|
|
|
|
2023-06-12 09:54:30 -06:00
|
|
|
Indentation based on treesitter for the |=| operator.
|
|
|
|
|
NOTE: this is an experimental feature and will be upstreamed to Neovim when
|
|
|
|
|
stable.
|
2020-08-09 11:39:51 -05:00
|
|
|
|
2023-06-12 09:54:30 -06:00
|
|
|
To enable it for a supported parser, add the following to a corresponding
|
|
|
|
|
`FileType` autocommand or `ftplugin/<lang>.lua`: >lua
|
2022-02-06 15:15:42 +01:00
|
|
|
|
2023-06-12 09:54:30 -06:00
|
|
|
vim.bo.indentexpr = 'v.lua:require'nvim-treesitter'.indentexpr()'
|
2020-08-09 11:39:51 -05:00
|
|
|
|
|
|
|
|
<
|
|
|
|
|
|
2023-06-12 09:54:30 -06:00
|
|
|
Indentation for a language is controlled by `indents.scm` queries. The
|
|
|
|
|
following captures are supported:
|
2020-08-09 11:39:51 -05:00
|
|
|
|
2023-03-19 18:09:18 -07:00
|
|
|
|
|
|
|
|
`@indent` *nvim-treesitter-indentation-queries*
|
2023-03-20 14:44:39 -07:00
|
|
|
Queries can use the following captures: `@indent.begin` and `@indent.dedent`,
|
|
|
|
|
`@indent.branch`, `@indent.end` or `@indent.align`. An `@indent.ignore` capture tells
|
|
|
|
|
treesitter to ignore indentation and a `@indent.zero` capture sets
|
2023-03-19 18:09:18 -07:00
|
|
|
the indentation to 0.
|
|
|
|
|
|
2023-03-20 14:44:39 -07:00
|
|
|
`@indent.begin` *nvim-treesitter-indentation-indent.begin*
|
|
|
|
|
The `@indent.begin` specifies that the next line should be indented. Multiple
|
2023-03-19 18:09:18 -07:00
|
|
|
indents on the same line get collapsed. Eg.
|
|
|
|
|
|
2023-06-12 09:54:30 -06:00
|
|
|
>query
|
2023-03-19 18:09:18 -07:00
|
|
|
(
|
|
|
|
|
(if_statement)
|
2023-03-20 14:44:39 -07:00
|
|
|
(ERROR "else") @indent.begin
|
2023-03-19 18:09:18 -07:00
|
|
|
)
|
2020-04-28 12:22:11 +02:00
|
|
|
<
|
2023-03-20 14:44:39 -07:00
|
|
|
Indent can also have `indent.immediate` set using a `#set!` directive, which
|
2023-03-19 18:09:18 -07:00
|
|
|
permits the next line to indent even when the block intended to be indented
|
|
|
|
|
has no content yet, improving interactive typing.
|
|
|
|
|
|
|
|
|
|
eg for python:
|
2023-06-12 09:54:30 -06:00
|
|
|
>query
|
2023-03-20 14:44:39 -07:00
|
|
|
((if_statement) @indent.begin
|
|
|
|
|
(#set! indent.immediate 1))
|
2023-03-19 18:09:18 -07:00
|
|
|
<
|
|
|
|
|
|
|
|
|
|
Will allow:
|
2023-06-12 09:54:30 -06:00
|
|
|
>python
|
2023-03-19 18:09:18 -07:00
|
|
|
if True:<CR>
|
|
|
|
|
# Auto indent to here
|
|
|
|
|
|
2023-03-20 14:44:39 -07:00
|
|
|
`@indent.end` *nvim-treesitter-indentation-indent.end*
|
|
|
|
|
An `@indent.end` capture is used to specify that the indented region ends and
|
2023-03-19 18:09:18 -07:00
|
|
|
any text subsequent to the capture should be dedented.
|
|
|
|
|
|
2023-03-20 14:44:39 -07:00
|
|
|
`@indent.branch` *nvim-treesitter-indentation-indent.branch*
|
|
|
|
|
An `@indent.branch` capture is used to specify that a dedented region starts
|
2023-03-19 18:09:18 -07:00
|
|
|
at the line including the captured nodes.
|
|
|
|
|
|
2023-03-20 14:44:39 -07:00
|
|
|
`@indent.dedent` *nvim-treesitter-indentation-indent.dedent*
|
|
|
|
|
A `@indent.dedent` capture specifies dedenting starting on the next line.
|
2023-03-19 18:09:18 -07:00
|
|
|
>
|
2023-03-20 14:44:39 -07:00
|
|
|
`@indent.align` *nvim-treesitter-indentation-aligned_indent.align*
|
|
|
|
|
Aligned indent blocks may be specified with the `@indent.align` capture.
|
2023-03-19 18:09:18 -07:00
|
|
|
This permits
|
|
|
|
|
|
|
|
|
|
>
|
|
|
|
|
foo(a,
|
|
|
|
|
b,
|
|
|
|
|
c)
|
|
|
|
|
<
|
|
|
|
|
As well as
|
|
|
|
|
>
|
|
|
|
|
foo(
|
|
|
|
|
a,
|
|
|
|
|
b,
|
|
|
|
|
c)
|
|
|
|
|
<
|
|
|
|
|
and finally
|
|
|
|
|
>
|
|
|
|
|
foo(
|
|
|
|
|
a,
|
|
|
|
|
b,
|
|
|
|
|
c
|
|
|
|
|
)
|
|
|
|
|
<
|
2023-03-20 14:44:39 -07:00
|
|
|
To specify the delimiters to use `indent.open_delimiter` and
|
|
|
|
|
`indent.close_delimiter` should be used. Eg.
|
2023-06-12 09:54:30 -06:00
|
|
|
>query
|
2023-03-20 14:44:39 -07:00
|
|
|
((argument_list) @indent.align
|
|
|
|
|
(#set! indent.open_delimiter "(")
|
|
|
|
|
(#set! indent.close_delimiter ")"))
|
2023-03-19 18:09:18 -07:00
|
|
|
<
|
|
|
|
|
|
2023-03-20 14:44:39 -07:00
|
|
|
For some languages the last line of an `indent.align` block must not be
|
2023-03-19 18:09:18 -07:00
|
|
|
the same indent as the natural next line.
|
|
|
|
|
|
|
|
|
|
For example in python:
|
|
|
|
|
|
2023-06-12 09:54:30 -06:00
|
|
|
>python
|
2023-03-19 18:09:18 -07:00
|
|
|
if (a > b and
|
|
|
|
|
c < d):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
Is not correct, whereas
|
2023-06-12 09:54:30 -06:00
|
|
|
>python
|
2023-03-19 18:09:18 -07:00
|
|
|
if (a > b and
|
|
|
|
|
c < d):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
Would be correctly indented. This behavior may be chosen using
|
2023-03-20 14:44:39 -07:00
|
|
|
`indent.avoid_last_matching_next`. Eg.
|
2023-03-19 18:09:18 -07:00
|
|
|
|
2023-06-12 09:54:30 -06:00
|
|
|
>query
|
2023-03-19 18:09:18 -07:00
|
|
|
(if_statement
|
2023-03-20 14:44:39 -07:00
|
|
|
condition: (parenthesized_expression) @indent.align
|
|
|
|
|
(#set! indent.open_delimiter "(")
|
|
|
|
|
(#set! indent.close_delimiter ")")
|
|
|
|
|
(#set! indent.avoid_last_matching_next 1)
|
2023-03-19 18:09:18 -07:00
|
|
|
)
|
|
|
|
|
<
|
2023-03-20 14:44:39 -07:00
|
|
|
Could be used to specify that the last line of an `@indent.align` capture
|
2023-03-19 18:09:18 -07:00
|
|
|
should be additionally indented to avoid clashing with the indent of the first
|
|
|
|
|
line of the block inside an if.
|
|
|
|
|
|
2020-09-02 21:10:42 +00:00
|
|
|
vim:tw=78:ts=8:expandtab:noet:ft=help:norl:
|