Skip to main content
The Tree API provides functions for manipulating syntax trees, including copying, editing, and comparing trees.

Creating and deleting trees

Trees are created by parsing source code with a parser. See the Parser API for details.

ts_tree_copy

Create a shallow copy of the syntax tree.
const TSTree *
The tree to copy
Returns: A new tree that shares the underlying data with the original. This operation is very fast because it only increments reference counts. You need to copy a syntax tree to use it on more than one thread at a time, as syntax trees are not thread safe. Example:

ts_tree_delete

Delete the syntax tree, freeing all of the memory that it used.
TSTree *
The tree to delete

Accessing tree content

ts_tree_root_node

Get the root node of the syntax tree.
const TSTree *
The tree instance
Returns: The root node of the tree. Example:

ts_tree_root_node_with_offset

Get the root node of the syntax tree, with its position shifted forward by the given offset.
const TSTree *
The tree instance
uint32_t
Number of bytes to offset
TSPoint
Row and column offset
Returns: The root node with adjusted positions. This is useful when you’ve parsed a fragment of a document and want to access the nodes as if they were positioned within the full document.

ts_tree_language

Get the language that was used to parse the syntax tree.
const TSTree *
The tree instance
Returns: The language used to parse the tree.

ts_tree_included_ranges

Get the array of included ranges that was used to parse the syntax tree.
const TSTree *
The tree instance
uint32_t *
Output parameter for the number of ranges
Returns: Pointer to an array of ranges (caller must free). The returned pointer must be freed by the caller using free.

Editing trees

ts_tree_edit

Edit the syntax tree to keep it in sync with source code that has been edited.
TSTree *
The tree to edit
const TSInputEdit *
Description of the edit
You must describe the edit both in terms of byte offsets and in terms of (row, column) coordinates. This allows Tree-sitter to efficiently update the tree’s internal state. After editing a tree, pass it as the old_tree parameter to ts_parser_parse for efficient incremental parsing. Example:

Comparing trees

ts_tree_get_changed_ranges

Compare an old edited syntax tree to a new syntax tree representing the same document, returning an array of ranges whose syntactic structure has changed.
const TSTree *
The old tree (after editing with ts_tree_edit)
const TSTree *
The new tree (returned from parsing)
uint32_t *
Output parameter for the number of ranges
Returns: Array of changed ranges (caller must free with free). For this to work correctly, the old syntax tree must have been edited such that its ranges match up to the new tree. Generally, you’ll want to call this function right after calling one of the parsing functions. The returned ranges indicate areas where the hierarchical structure of syntax nodes (from root to leaf) has changed between the old and new trees. Characters outside these ranges have identical ancestor nodes in both trees. Note that the returned ranges may be slightly larger than the exact changed areas, but Tree-sitter attempts to make them as small as possible. Example:

Debugging

ts_tree_print_dot_graph

Write a DOT graph describing the syntax tree to the given file.
const TSTree *
The tree to print
int
File descriptor to write to
The output is in the DOT graph description language, which can be rendered with tools like Graphviz. Example: