ts::Tree class

A syntax tree.

This also contains a copy of the source code to allow the nodes to refer to the text they were created from.

Constructors, destructors, conversion operators

Tree(TSTree* tree, std::string source, const Parser& parser) explicit
Create a new tree from the raw Tree-Sitter tree.
Tree(const Tree&)
Copy constructor.
Tree(Tree&& other) defaulted noexcept
Move constructor.

Public functions

auto operator=(const Tree&) -> Tree&
Copy assignment operator.
auto operator=(Tree&& other) -> Tree& defaulted noexcept
Move assignment operator.
auto raw() const -> const TSTree*
Returns the raw Tree-Sitter tree.
auto source() const -> const std::string&
The source code the tree was created from.
auto parser() const -> const Parser&
The used parser.
auto root_node() const -> Node
The root node of the tree.
auto language() const -> Language
The language that was used to parse the syntax tree.
auto edit(std::vector<Edit>) -> EditResult
Edit the syntax tree and source code and return the changed ranges.
void print_dot_graph(std::string_view file) const
Print a dot graph to the given file.

Friends

void swap(Tree& self, Tree& other) noexcept
Swap function.

Function documentation

ts::Tree::Tree(TSTree* tree, std::string source, const Parser& parser) explicit

Create a new tree from the raw Tree-Sitter tree.

ts::Tree::Tree(const Tree&)

Copy constructor.

TSTree* can be safely (and fast) copied using ts_tree_copy.

const TSTree* ts::Tree::raw() const

Returns the raw Tree-Sitter tree.

Node ts::Tree::root_node() const

The root node of the tree.

The returned node is only valid as long as this tree is not destructed.

EditResult ts::Tree::edit(std::vector<Edit>)

Edit the syntax tree and source code and return the changed ranges.

You need to specify all edits you want to apply to the syntax tree in one call. Because this method changes both the syntax tree and source code string any other Edits will be invalid and trying to apply them is undefined behaviour.

The edits can't be duplicate or overlapping. Multiline edits are also currently not supported.

The returned result contains information about the raw string ranges that changed and it also contains the adjusted location of the edits that can e.g. be used for highlighting in an editor.

Any previously retrieved nodes will become (silently) invalid.

void ts::Tree::print_dot_graph(std::string_view file) const

Print a dot graph to the given file.

file has to be a null-terminated string (e.g. from a std::string).