Editing
In applications like text editors, you often need to re-parse a file after its source code has changed. Tree-sitter is designed to support this use case efficiently through incremental parsing.The Two-Step Process
1
Edit the syntax tree
First, you must edit the syntax tree to adjust the ranges of its nodes so they stay in sync with the code.
2
Re-parse with the old tree
Then, call
ts_parser_parse again, passing in the old tree. This creates a new tree that internally shares structure with the old tree.Complete Example
Editing Stored Nodes
When you edit a syntax tree, the positions of its nodes will change. If you have stored anyTSNode instances outside of the TSTree, you must update them separately:
Multi-language Documents
Sometimes, different parts of a file may be written in different languages. For example, templating languages like EJS and ERB allow you to generate HTML by writing a mixture of HTML and another language like JavaScript or Ruby. Tree-sitter handles these types of documents by allowing you to create a syntax tree based on the text in certain ranges of a file.Setting Included Ranges
Example: Parsing ERB
Consider this ERB document:- An ERB syntax tree
- A Ruby syntax tree (for the
<% %>blocks) - An HTML syntax tree (for the content outside the blocks)
Concurrency
Tree-sitter supports multi-threaded use cases by making syntax trees very cheap to copy.Getting Changed Ranges
When re-parsing after an edit, you can determine which parts of the tree have changed:malloc and must be freed by the caller.
The returned ranges indicate areas where the hierarchical structure of syntax nodes (from root to leaf) has changed. Characters outside these ranges have identical ancestor nodes in both trees. The ranges may be slightly larger than the exact changed areas, but Tree-sitter attempts to make them as small as possible.
Next Steps
Walking Trees
Learn about efficient tree traversal with cursors
Pattern Matching
Query syntax trees with powerful patterns