Member-only story
Neovim 101 — Tree-sitter
The introduction of Lua and Tree-sitter provides us with endless possibilities to customize Neovim.
In this article, we will get into the basics of Tree-sitter and use the Neovim Tree-sitter APIs to develop a function navigator.
This article is part of the Neovim 101 series.
The Neovim configuration files are available in this repository.
Getting Started
Introduction
Tree-sitter is a parser generator tool and an incremental parsing library. For Neovim, we use the nvim-treesitter plugin that provides a simple and easy-to-use interface to interact with Tree-sitter.
Tree-sitter Playground
The Tree-sitter website provides a playground that we can use to experiment with different programming languages without installing any plugins.
E.g., for JavaScript,
E.g, for Python,
Using the playground, we can experiment with the Tree-sitter query.
E.g., In the screenshot below, we use a query to highlight the Python function name
Let’s look at this simple query.
(function_definition
name: (identifier) @func_name
)
function_definition
andidentifier
indicate the node type, and they are enclosed with parentheses. In our case, theidentifier
node is within thefunction_definition
node.name
is a field. We use it to make the pattern more specific. In our case, we want to identify the function name.@func_name
is a…