2023-06-12 09:54:30 -06:00
< h1 align = "center" >
< img src = "https://github.com/nvim-treesitter/nvim-treesitter/assets/2361214/0513b223-c902-4f12-92ee-8ac4d8d6f41f" alt = "nvim-treesitter" >
< / h1 >
2020-09-10 13:57:42 -05:00
< div align = "center" >
< p >
2022-12-31 13:44:53 +01:00
< a href = "https://matrix.to/ #/#nvim -treesitter:matrix.org" >
< img alt = "Matrix Chat" src = "https://img.shields.io/matrix/nvim-treesitter:matrix.org" / >
2020-09-10 13:57:42 -05:00
< / a >
< a href = "https://github.com/nvim-treesitter/nvim-treesitter/actions?query=workflow%3A%22Linting+and+style+checking%22+branch%3Amaster" >
< img alt = "Linting and Style" src = "https://github.com/nvim-treesitter/nvim-treesitter/workflows/Linting%20and%20style%20checking/badge.svg" / >
< / a >
< a href = "https://github.com/nvim-treesitter/nvim-treesitter/actions?query=workflow%3A%22Check+loading+of+syntax+files%22+branch%3Amaster" >
< img alt = "Syntax files" src = "https://github.com/nvim-treesitter/nvim-treesitter/workflows/Check%20loading%20of%20syntax%20files/badge.svg" / >
< / a >
< / p >
< / div >
2020-08-10 18:33:34 +02:00
2023-06-12 09:54:30 -06:00
# WARNING
2020-11-21 22:46:24 +01:00
2023-06-12 09:54:30 -06:00
**This branch is a [full, incompatible, rewrite of `nvim-treesitter` ](https://github.com/nvim-treesitter/nvim-treesitter/issues/4767 ) and [work in progress ](TODO.md ). This branch REQUIRES (the latest commit on) Neovim `master` .**
2020-11-21 22:46:24 +01:00
2023-06-12 09:54:30 -06:00
The `nvim-treesitter` plugin provides
1. functions for installing, updating, and removing [**tree-sitter parsers** ](SUPPORTED_LANGUAGES.md );
2. a collection of **queries** for enabling tree-sitter features built into Neovim for these languages.
2020-06-21 17:39:58 +02:00
2020-04-22 17:17:21 +02:00
# Quickstart
## Requirements
2020-07-27 09:23:42 -05:00
2023-06-12 09:54:30 -06:00
- Neovim 0.10.0 or later (nightly)
2021-06-13 00:44:36 +05:30
- `tar` and `curl` in your path (or alternatively `git` )
2023-06-12 09:54:30 -06:00
- a C compiler in your path and libstdc++ installed ([Windows users please read this! ](https://github.com/nvim-treesitter/nvim-treesitter/wiki/Windows-support ))
- optional: `tree-sitter` CLI and `node`
2020-04-22 17:17:21 +02:00
## Installation
2020-11-21 22:07:17 +01:00
You can install `nvim-treesitter` with your favorite package manager (or using the native `package` feature of vim, see `:h packages` ).
2020-04-22 17:17:21 +02:00
2022-01-24 16:14:26 +01:00
**NOTE: This plugin is only guaranteed to work with specific versions of language parsers** (as specified in the `lockfile.json` ). **When upgrading the plugin, you must make sure that all installed parsers are updated to the latest version** via `:TSUpdate` .
2023-06-12 09:54:30 -06:00
It is strongly recommended to automate this; e.g., using [lazy.nvim ](https://github.com/folke/lazy.nvim )
2021-06-30 17:04:24 +05:30
2023-06-12 09:54:30 -06:00
```lua
require('lazy').setup(
{ 'nvim-treesitter/nvim-treesitter', build = ':TSUpdate', lazy = false }
)
2020-11-21 20:32:24 +01:00
```
2023-02-15 14:56:45 +00:00
2023-06-12 09:54:30 -06:00
**NOTE: This plugin does not support lazy-loading.**
2020-04-22 17:17:21 +02:00
2023-06-12 09:54:30 -06:00
## Setup
2020-07-27 09:23:42 -05:00
2023-06-12 09:54:30 -06:00
`nvim-treesitter` can be configured by calling `setup` . The following snippet lists the available options and their default values. **You do not need to call `setup` for `nvim-treesitter` to work using default values.**
2020-04-28 11:56:00 +02:00
2022-02-09 21:54:36 +01:00
```lua
2023-06-12 09:54:30 -06:00
require'nvim-treesitter'.setup {
2023-06-10 17:12:35 +02:00
-- A list of parser names or tiers ('stable', 'core', 'community', 'unsupported')
2023-06-12 09:54:30 -06:00
ensure_install = { },
2021-12-17 20:49:06 -05:00
2023-06-12 09:54:30 -06:00
-- List of parsers to ignore installing
2023-06-10 17:12:35 +02:00
ignore_install = { 'unsupported' },
2023-06-12 09:54:30 -06:00
2022-07-08 09:36:54 +02:00
-- Automatically install missing parsers when entering buffer
2023-06-12 09:54:30 -06:00
auto_install = false,
2020-11-21 22:07:17 +01:00
2023-06-12 09:54:30 -06:00
-- Directory to install parsers and queries to
install_dir = vim.fn.stdpath('data') .. '/site'
}
2020-11-21 22:07:17 +01:00
```
2020-11-21 22:46:24 +01:00
Check [`:h nvim-treesitter-commands` ](doc/nvim-treesitter.txt ) for a list of all available commands.
2020-11-21 22:07:17 +01:00
2020-11-21 22:46:24 +01:00
# Supported languages
2020-11-21 22:07:17 +01:00
2020-11-21 22:46:24 +01:00
For `nvim-treesitter` to support a specific feature for a specific language requires both a parser for that language and an appropriate language-specific query file for that feature.
2020-11-21 22:07:17 +01:00
2023-06-12 09:54:30 -06:00
A list of the currently supported languages can be found [on this page ](SUPPORTED_LANGUAGES.md ).
2021-10-04 19:37:38 +02:00
2022-01-17 15:27:02 +01:00
We are looking for maintainers to add more parsers and to write query files for their languages. Check our [tracking issue ](https://github.com/nvim-treesitter/nvim-treesitter/issues/2282 ) for open language requests.
2020-11-21 22:07:17 +01:00
2022-04-29 12:07:27 +04:30
For related information on the supported languages, including related plugins, see [this wiki page ](https://github.com/nvim-treesitter/nvim-treesitter/wiki/Supported-Languages-Information ).
2020-11-21 22:07:17 +01:00
2023-06-12 09:54:30 -06:00
# Supported features
2020-08-09 11:39:51 -05:00
2023-06-12 09:54:30 -06:00
`nvim-treesitter` provides queries for the following features. **These are not automatically enabled.**
2020-08-09 11:39:51 -05:00
2023-06-12 09:54:30 -06:00
## Highlighting
2020-08-09 11:39:51 -05:00
2023-06-12 09:54:30 -06:00
Treesitter highlighting is provided by Neovim, see `:h treesitter-highlight` . To enable it for a filetype, put `vim.treesitter.start()` in a `ftplugin/<filetype>.lua` in your config directory, or place the following in your `init.lua` :
2020-10-19 21:32:54 +02:00
2022-02-09 21:54:36 +01:00
```lua
2023-06-12 09:54:30 -06:00
vim.api.nvim_create_autocmd('FileType', {
pattern = { '< filetype > ' },
callback = function() vim.treesitter.start() end,
})
2020-10-19 21:32:54 +02:00
```
2023-06-12 09:54:30 -06:00
## Folds
2020-11-21 22:46:24 +01:00
2023-06-12 09:54:30 -06:00
Treesitter-based folding is provided by Neovim. To enable it, put the following in your `ftplugin` or `FileType` autocommand:
2020-08-09 11:39:51 -05:00
2024-07-17 18:03:09 +02:00
```lua
vim.wo.foldexpr = 'v:lua.vim.treesitter.foldexpr()'
2020-08-09 11:39:51 -05:00
```
2023-06-12 09:54:30 -06:00
## Indentation
2020-09-07 11:41:37 -05:00
2023-06-12 09:54:30 -06:00
Treesitter-based indentation is provided by this plugin but considered **experimental** . To enable it, put the following in your `ftplugin` or `FileType` autocommand:
2022-06-20 21:50:07 +01:00
2023-02-15 14:56:45 +00:00
```lua
2023-06-12 09:54:30 -06:00
vim.bo.indentexpr = "v:lua.require'nvim-treesitter'.indentexpr()"
2022-06-20 21:50:07 +01:00
```
2023-06-12 09:54:30 -06:00
(Note the specific quotes used.)
2022-06-20 21:50:07 +01:00
2023-06-12 09:54:30 -06:00
## Injections
2022-06-20 21:50:07 +01:00
2023-06-12 09:54:30 -06:00
Injections are used for multi-language documents, see `:h treesitter-language-injections` . No setup is needed.
2023-02-15 14:56:45 +00:00
2023-05-20 17:29:03 +02:00
## Locals
These queries can be used to look up definitions and references to identifiers in a given scope. They are used, e.g., by the `nvim-treesitter-refactor` plugin.
2023-06-12 09:54:30 -06:00
# Advanced setup
2022-06-20 21:50:07 +01:00
2020-11-21 22:46:24 +01:00
## Adding parsers
2020-11-21 21:00:38 +01:00
2020-11-21 22:46:24 +01:00
If you have a parser that is not on the list of supported languages (either as a repository on Github or in a local directory), you can add it manually for use by `nvim-treesitter` as follows:
2020-11-21 21:00:38 +01:00
2024-04-14 16:25:28 +02:00
1. Add the following snippet in a `User TSUpdate` autocommand:
2020-11-21 21:00:38 +01:00
2022-02-09 21:54:36 +01:00
```lua
2024-04-14 16:25:28 +02:00
vim.api.nvim_create_autocmd('User', { pattern = 'TSUpdate',
callback = function()
require('nvim-treesitter.parsers').zimbu = {
install_info = {
url = 'https://github.com/zimbulang/tree-sitter-zimbu',
files = { 'src/parser.c' }, -- note that some parsers also require src/scanner.c
revision = < sha > , -- commit hash for revision to check out; HEAD if missing
-- optional entries:
branch = 'develop', -- only needed if different from default branch
location = 'parser', -- only needed if the parser is in subdirectory of a "monorepo"
generate = true, -- only needed if repo does not contain pre-generated src/parser.c
generate_from_json = true, -- only needed if parser has npm dependencies
},
}
end})
2020-11-21 21:00:38 +01:00
```
2024-04-14 16:25:28 +02:00
Alternatively, if you have a local checkout, you can instead use
```lua
install_info = {
path = '~/parsers/tree-sitter-zimbu',
files = { 'src/parser.c' }, -- note that some parsers also require src/scanner.c
-- optional entries
location = 'parser', -- only needed if the parser is in subdirectory of a "monorepo"
generate = true, -- only needed if repo does not contain pre-generated src/parser.c
generate_from_json = true, -- only needed if parser has npm dependencies
},
```
This will always use the state of the directory as-is (i.e., `branch` and `revision` will be ignored).
2. If the parser name differs from the filetype(s) used by Neovim, you need to register the parser via
2022-02-06 19:10:46 +01:00
2022-02-09 21:54:36 +01:00
```lua
2023-06-12 09:54:30 -06:00
vim.treesitter.language.register('zimbu', { 'zu' })
2022-02-06 13:34:08 +01:00
```
2023-06-12 09:54:30 -06:00
If Neovim does not detect your language's filetype by default, you can use [Neovim's `vim.filetype.add()` ](<https://neovim.io/doc/user/lua.html#vim.filetype.add( )>) to add a custom detection rule.
2023-02-24 09:49:16 +00:00
2024-04-14 16:25:28 +02:00
3. Start `nvim` and `:TSInstall zimbu` .
2020-11-21 21:00:38 +01:00
2023-06-08 13:25:31 +02:00
**Note:** Parsers using external scanner need to be written in C. C++ scanners are no longer supported.
2021-08-08 05:23:11 +06:00
2024-04-14 16:25:28 +02:00
### Modifying parsers
You can use the same approach for overriding parser information. E.g., if you always want to generate the `lua` parser from grammar, add
```lua
vim.api.nvim_create_autocmd('User', { pattern = 'TSUpdate',
callback = function()
require('nvim-treesitter.parsers').lua.install_info.generate = true
end})
```
2020-11-21 22:46:24 +01:00
## Adding queries
2020-11-21 22:07:17 +01:00
2023-06-12 09:54:30 -06:00
Queries can be placed anywhere in your `runtimepath` under `queries/<language>` , with earlier directories taking precedence unless the queries are marked with `; extends` ; see `:h treesitter-query` .
2020-07-02 10:26:53 -05:00
2023-06-12 09:54:30 -06:00
E.g., to add queries for `zimbu` , put `highlights.scm` etc. under
2022-02-09 21:54:36 +01:00
```lua
2023-06-12 09:54:30 -06:00
vim.fn.stdpath('data') .. 'site/queries/zimbu'
2020-07-02 10:26:53 -05:00
```
2020-08-09 11:39:51 -05:00
# Troubleshooting
2020-07-27 09:23:42 -05:00
2022-04-30 10:39:16 -07:00
Before doing anything, make sure you have the latest version of this plugin and run `:checkhealth nvim-treesitter` .
2020-11-21 22:46:24 +01:00
It can also help to update the parsers via `:TSUpdate` .
2020-04-22 17:17:21 +02:00
2023-06-12 09:54:30 -06:00
#### Feature `{X}` does not work for `{language}`...
2021-07-08 20:31:00 -04:00
2023-06-12 09:54:30 -06:00
1. Check the `nvim-treesitter` section of `:checkhealth` for any warning, and make sure that the query for `{X}` is listed for `{language}` .
2021-07-08 20:31:00 -04:00
2023-06-12 09:54:30 -06:00
2. Ensure that the feature is enabled as explained above.
2020-07-15 09:24:57 +02:00
2023-06-12 09:54:30 -06:00
3. Ensure Neovim is correctly identifying your language's filetype using the `:echo &filetype` command while one of your language's files is open in Neovim.
2020-09-18 17:47:20 -05:00
2020-11-21 22:46:24 +01:00
#### I get `Error detected while processing .../plugin/nvim-treesitter.vim` every time I open Neovim
2020-08-01 15:33:44 -05:00
This is probably due to a change in a parser's grammar or its queries.
Try updating the parser that you suspect has changed (`:TSUpdate {language}` ) or all of them (`:TSUpdate` ).
If the error persists after updating all parsers,
please [open an issue ](https://github.com/nvim-treesitter/nvim-treesitter/issues/new/choose ).
2021-11-16 21:58:58 -05:00
#### I get `query error: invalid node type at position`
This could be due a query file outside this plugin using outdated nodes,
or due to an outdated parser.
- Make sure you have the parsers up to date with `:TSUpdate`
- Make sure you don't have more than one `parser` runtime directory.
2023-06-12 09:54:30 -06:00
You can execute this command `:= vim.api.nvim_get_runtime_file('parser', true)` to find all runtime directories.
2021-11-16 21:58:58 -05:00
If you get more than one path, remove the ones that are outside this plugin (`nvim-treesitter` directory),
so the correct version of the parser is used.
2021-10-01 20:33:55 +02:00
#### I want to use Git instead of curl for downloading the parsers
In your Lua config:
```lua
require("nvim-treesitter.install").prefer_git = true
```
#### I want to use a HTTP proxy for downloading the parsers
You can either configure curl to use additional CLI arguments in your Lua config:
2023-02-15 14:56:45 +00:00
2021-10-01 20:33:55 +02:00
```lua
require("nvim-treesitter.install").command_extra_args = {
curl = { "--proxy", "< proxy url > " },
}
```
2023-02-15 14:56:45 +00:00
2021-10-01 22:07:27 +02:00
or you can configure git via `.gitconfig` and use git instead of curl
2021-10-01 20:33:55 +02:00
```lua
require("nvim-treesitter.install").prefer_git = true
```
2022-02-06 10:52:01 +08:00
#### I want to use a mirror instead of "https://github.com/"
In your Lua config:
```lua
2024-04-14 16:25:28 +02:00
for _, config in pairs(require("nvim-treesitter.parsers")) do
2022-02-06 10:52:01 +08:00
config.install_info.url = config.install_info.url:gsub("https://github.com/", "something else")
end
2023-06-12 09:54:30 -06:00
require'nvim-treesitter'.setup {
2022-02-06 10:52:01 +08:00
--
--
}
```