Member-only story

Neovim 101 — Tree-sitter

Learn the magic of Lua and Tree-sitter.

--

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,

JavaScript Syntax Tree

E.g, for Python,

Python Syntax Tree

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

Tree-sitter Query

Let’s look at this simple query.

(function_definition
name: (identifier) @func_name
)
  • function_definition and identifier indicate the node type, and they are enclosed with parentheses. In our case, the identifier node is within the function_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…

--

--

alpha2phi
alpha2phi

Written by alpha2phi

Software engineer, Data Science and ML practitioner.

No responses yet