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
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
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
ts_tree_language
Get the language that was used to parse the syntax tree.const TSTree *
The tree instance
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
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
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
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