No description
Find a file
Thomas Vigouroux 337756d2f6 docs: add apache Licence
This is to match neovim's licence.
2020-07-01 14:04:18 +02:00
.github/ISSUE_TEMPLATE Update issue templates 2020-06-14 11:57:09 +02:00
autoload feat: add syntax-based folding 2020-05-25 11:19:38 +02:00
doc Rgenerate doc/tags 2020-06-30 17:22:27 +02:00
lua fix ask install 2020-06-30 08:54:32 +02:00
parser add parser installer 2020-04-19 15:13:38 +02:00
plugin Fix #136: Highlight of TSFloat should be Float not TSFloat 2020-07-01 13:22:20 +02:00
queries Java highlights: Add binary integer literals 2020-07-01 13:22:46 +02:00
.editorconfig style: add editorconfig 2020-04-18 17:39:03 +02:00
CONTRIBUTING.md Update CONTRIBUTING.md 2020-06-23 14:30:01 +02:00
LICENSE docs: add apache Licence 2020-07-01 14:04:18 +02:00
README.md refactor(refactor): use higher local apis and some cleanup 2020-06-30 08:21:01 +02:00

Gitter

nvim-treesitter

Treesitter configurations and abstraction layer for Neovim.

Warning: Treesitter and Treesitter highlighting are an experimental feature of nightly versions of Neovim. Please consider the experience with this plug-in as experimental until Neovim 0.5 is released!

Quickstart

Requirements

  • Neovim nightly
  • git in your path.
  • A C compiler in your path.

Installation

You can install nvim-treesitter with your favorite package manager, or using the default pack feature of Neovim!

Using a package manager

Simply add these lines to your init.vim :

Plug 'nvim-treesitter/nvim-treesitter'

Using neovim pack feature

We highly recommend reading :h packages to learn more about this feature, but you can still follow these steps:

$ mkdir -p ~/.local/share/nvim/site/pack/nvim-treesitter/start
$ cd ~/.local/share/nvim/site/pack/nvim-treesitter/start
$ git clone https://github.com/nvim-treesitter/nvim-treesitter.git

Adding parsers

Treesitter is using a different parser for every language. It can be quite a pain to install, but fortunately nvim-treesitter provides two command to tackle this issue:

  • TSInstall to install one or more parser. You can use TSInstall all to download all parsers.
  • TSInstallInfo to know which parser is installed.

Let's say you need parsers for lua, c, and python, this is how you do with these commands:

:TSInstall c
Downloading...
Compiling...
Treesitter parser for c has been installed

:TSInstall lua
Downloading...
Compiling...
Treesitter parser for lua has been installed

:TSInstall python
Downloading...
Compiling...
Treesitter parser for python has been installed

Cool, lets see which parsers are installed:

:TSInstallInfo
csharp     [✗] not installed
html       [✗] not installed
typescript [✗] not installed
c          [✓] installed
swift      [✗] not installed
java       [✗] not installed
python     [] installed
cpp        [✗] not installed
lua        [✓] installed
ruby       [✗] not installed
ocaml      [✗] not installed
go         [✗] not installed
rust       [✗] not installed
json       [✗] not installed
javascript [✗] not installed
css        [✗] not installed
julia      [✗] not installed
php        [✗] not installed
bash       [✗] not installed
tsx        [✗] not installed

And now you should be able to use every functionality nvim-treesitter provides!

Setup

in your init.vim:

lua <<EOF
require'nvim-treesitter.configs'.setup {
    highlight = {
        enable = true,                    -- false will disable the whole extension
        disable = { 'c', 'rust' },        -- list of language that will be disabled
    },
    incremental_selection = {
        enable = true,
        disable = { 'cpp', 'lua' },
        keymaps = {                       -- mappings for incremental selection (visual mappings)
          init_selection = 'gnn',         -- maps in normal mode to init the node/scope selection
          node_incremental = "grn",       -- increment to the upper named parent
          scope_incremental = "grc",      -- increment to the upper scope (as defined in locals.scm)
          node_decremental = "grm",       -- decrement to the previous node
        }
    },
    refactor = {
      highlight_defintions = {
        enable = true
      },
      smart_rename = {
        enable = true,
        smart_rename = "grr"              -- mapping to rename reference under cursor
      },
      navigation = {
        enable = true,
        goto_definition = "gnd",          -- mapping to go to definition of symbol under cursor
        list_definitions = "gnD"          -- mapping to list all definitions in current file
      }
    },
    ensure_installed = 'all' -- one of 'all', 'language', or a list of languages
}
EOF

Commands

Each feature can be enabled or disabled by different means:

:TSBufEnable {module} " enable module on current buffer
:TSBufDisable {module} " disable module on current buffer
:TSEnableAll {module} [{ft}] " enable module on every buffer. If filetype is specified, enable only for this filetype.
:TSDisableAll {module} [{ft}] " disable module on every buffer. If filetype is specified, disable only for this filetype.
:TSModuleInfo [{module}] " list information about modules state for each filetype

Features and Roadmap

The goal of nvim-treesitter is both to provide a simple and easy way to use the interface for Treesitter in Neovim, but also to add some functionalities to it: Some of these features are :

  • Incremental selection
  • Syntax based code folding (set foldmethod=expr foldexpr=nvim_treesitter#foldexpr())
  • Consistent syntax highlighting (the api is not quite stable yet)
  • Statusline indicator (require'nvim-treesitter'.statusline(size))

You can find the roadmap here. The roadmap and all features of this plugin are open to change, and any suggestion will be highly appreciated!

Available Modules

  • highlight: Consistent syntax highlighting.
  • incremental_selection: Syntax based selection.
  • refactor.highlight_definitions: Syntax based definition and usage highlighting.
  • refactor.smart_rename: Syntax based definition and usage renaming.
  • refactor.navigation: Syntax based definition listing and navigation.
    • List all definitions
    • Go to definition

Utils

you can get some utility functions with

local ts_utils = require 'nvim-treesitter.ts_utils'

More information is available in the help file (:help nvim-treesitter-utils).

Supported Languages

For treesitter to work, we need to use query files such as those you can find in queries/{lang}/{locals,highlights}.scm

We are looking for maintainers to write query files for their languages.

List of currently supported languages:

  • lua (maintained by @vigoux)
  • ruby (maintained by @TravonteD)
  • c (maintained by @vigoux)
  • go (maintained by @theHamsta)
  • cpp (maintained by @theHamsta, extends C queries)
  • rust
  • python (maintained by @theHamsta)
  • javascript (maintained by @steelsojka)
  • typescript (maintained by @steelsojka)
  • tsx
  • json (maintained by @steelsojka)
  • html (maintained by @TravonteD)
  • csharp
  • swift
  • java
  • ocaml
  • css (maintained by @TravonteD)
  • julia
  • php
  • bash (maintained by @TravonteD)
  • scala
  • haskell
  • toml
  • vue
  • elm
  • yaml
  • nix
  • markdown
  • regex (maintained by @theHamsta)

User Query Extensions

You can add your own query files by placing a query file in vim's runtime path after nvim-treesitter is sourced. If the language has a built in query file, that file will be appended to or it will be used (useful for languages not yet supported). For example, you can add files to <vim-config-dir>/after/queries/lua/highlights.scm to add more queries to lua highlights. You can also manually add query paths to the runtime path by adding this to your vim config set rtp+='path/to/queries'.

Troubleshooting

Before doing anything run :checkhealth nvim_treesitter. This will help you find where the bug might come from.

Feature X does not work for language Y...

First, check the ## Y parser healthcheck section of :checkhealth if you have any warning. If you do, it's highly possible that this is the cause of the problem. If everything is okay, then it might be an actual error.

In both cases, feel free to open an issue here.