Skip to main content
A Tree represents the syntactic structure of a source code file. Trees are immutable but can be edited to reflect changes in the source code.

Accessing Nodes

root_node

Get the root node of the syntax tree.

root_node_with_offset

Get the root node of the syntax tree, but with its position shifted forward by the given offset.
usize
required
The byte offset to shift the node’s position
Point
required
The row/column offset to shift the node’s position

Language Information

language

Get the language that was used to parse the syntax tree.

Tree Editing

edit

Edit the syntax tree to keep it in sync with source code that has been edited. You must describe the edit both in terms of byte offsets and in terms of row/column coordinates.
&InputEdit
required
A description of the edit made to the source code

Tree Traversal

walk

Create a new TreeCursor starting from the root of the tree. This is equivalent to calling tree.root_node().walk().

Comparing Trees

changed_ranges

Compare this old edited syntax tree to a new syntax tree representing the same document, returning a sequence of ranges whose syntactic structure has changed. For this to work correctly, this syntax tree must have been edited such that its ranges match up to the new tree. Generally, you’ll want to call this method right after calling one of the Parser::parse functions. Call it on the old tree that was passed to parse, and pass the new tree that was returned from parse.
&Tree
required
The new tree to compare against
Returns: An iterator over the ranges that changed between the two trees.

Range Information

included_ranges

Get the included ranges that were used to parse the syntax tree.

Debugging

Print a graph of the tree to the given file descriptor. The graph is formatted in the DOT language. You may want to pipe this graph directly to a dot(1) process in order to generate SVG output.
&impl AsRawFd
required
The file descriptor to write the graph to
This method is only available on Unix platforms with the std feature enabled.

Cloning

The Tree struct implements Clone, allowing you to create copies of syntax trees:

InputEdit

The InputEdit struct describes a change to a text document:
usize
The byte offset where the edit begins
usize
The byte offset where the edit ends in the old text
usize
The byte offset where the edit ends in the new text
Point
The row/column position where the edit begins
Point
The row/column position where the edit ends in the old text
Point
The row/column position where the edit ends in the new text

edit_point

Edit a point to keep it in-sync with source code that has been edited. This function updates a single point’s byte offset and row/column position based on this edit operation.
&mut Point
required
The point to update
&mut usize
required
The byte offset to update

edit_range

Edit a range to keep it in-sync with source code that has been edited. This function updates a range’s start and end positions based on this edit operation.
&mut Range
required
The range to update

Examples

Basic Tree Access

Incremental Parsing with Edits

Finding Changed Ranges

Tree Walking