Neovim Treesitter integration
What is Treesitter and how does it relate to Neovim?
Treesitter is a parser generating tool and a parsing library.
Parsers for any given language in the Treesitter ecosystem are called grammars.
For example, there's an official TypeScript grammar.
Treesitter is integrated into Neovim without any plugin requirements.
There is nvim-treesitter, but that's just a grammar installer.
By default, Neovim comes bundled with grammars for:
- C
- Lua
- Markdown
- Vimscript
- Vimdoc
Treesitter module
Neovim comes bundled with a Lua module that exposes functions to interact with the inbuilt Treesitter.
The first of these to note is vim.treesitter.inspect_tree()
.
You can call this either in Lua or by using :InspectTree
.
This will open up a separate buffer with the parsed tree for your current file. Hovering over any of the nodes will highlight them in your original buffer.
Getting nodes from the tree
To get the TSNode the cursor is hovering over, it's:
-- This could either be a TSNode or nil
local node = vim.treesitter.get_node()
if node == nil then
-- Do some check to make sure it's not nil
end