Tree sitter very beginner guide to implement and use queries

March 6, 2024

In the realm of code editing and syntax highlighting, Tree-sitter stands out as a groundbreaking tool that offers a robust solution for understanding the structure of source code in various programming languages. It's a parser generator tool and an incremental parsing library that allows developers to maintain high performance and precise syntax highlighting, code formatting, and more. This post delves into the core aspect of Tree-sitter that makes these features possible: Tree-sitter queries.

Have you not set up tree-sitter on your machine yet? Check this post - Tree-sitter 101.

What are Tree-sitter Queries?

Tree-sitter queries are a powerful mechanism for extracting information and patterns from the syntax tree generated by Tree-sitter. They allow developers to specify patterns in a language-agnostic way, enabling actions like syntax highlighting, code navigation, and other forms of static analysis across many programming languages with minimal effort.

A Tree-sitter query consists of a series of patterns that match nodes in the syntax tree based on their type, content, and relationships to other nodes.
These queries can be written in a S-expression syntax, making them both readable and expressive.

Why Use Tree-sitter Queries?

Writing Your First Tree-sitter Query

To get started with Tree-sitter queries, you first need to have Tree-sitter installed and set up for your target language. Once you have a parsed syntax tree, you can begin writing queries.

Here's a simple example to highlight function definitions in a JavaScript file:

Code Loading...

This query has two patterns: one for matching named function declarations and another for matching variables that are initialized with a function. The @function.name part is a capture name, which is used to refer to the matched nodes in the actions that follow (in this case, highlighting with the tag "function.name").