What is Tree-sitter?

Published Mon Jan 29 2024

What is Tree-sitter? For me tree-sitter was the program that fixed ugly jsx syntax highlighting. The library is self described as a “parser generator tool and an incremental parsing library” and has had a huge impact on all text editors.

Originally implemented in Atom, tree-sitter is now widely used by most editors. The most noted improvement made is to syntax highlighting. Before tree-sitter syntax highlighting was managed by large sets of regex patterns. Since regex cannot become structurally aware of code syntax highlighting was fragile.

Tree-sitter also has performance benefits when replacing regex. This is primarily because tree-sitter incrementally parses. When an edit is made to a document only the updated part of the document must be parsed. I see this as similar to the virtual DOM pattern employed by many JavaScript frameworks.

Looking beyond the surface-level benefits, tree-sitter offers a common interface for parsed code. This has lead to improved developer tooling, but can also be leveraged in day to day programming as well. With tree-sitter you can navigate based on code concepts: (function, class) and make text selections using tree-sitter nodes!

Note: nvim-treesitter has a handy incremental node selection bind available.