Struct tree_sitter::Node
source · pub struct Node<'tree>(/* private fields */);
Expand description
A single node within a syntax Tree
.
Implementations§
source§impl<'tree> Node<'tree>
impl<'tree> Node<'tree>
sourcepub fn id(&self) -> usize
pub fn id(&self) -> usize
Get a numeric id for this node that is unique.
Within a given syntax tree, no two nodes have the same id. However, if a new tree is created based on an older tree, and a node from the old tree is reused in the process, then that node will have the same id in both trees.
sourcepub fn grammar_id(&self) -> u16
pub fn grammar_id(&self) -> u16
Get the node’s type as a numerical id as it appears in the grammar ignoring aliases.
sourcepub fn grammar_name(&self) -> &'static str
pub fn grammar_name(&self) -> &'static str
Get this node’s symbol name as it appears in the grammar ignoring aliases as a string.
sourcepub fn language(&self) -> LanguageRef<'_>
pub fn language(&self) -> LanguageRef<'_>
Get the Language
that was used to parse this node’s syntax tree.
sourcepub fn is_named(&self) -> bool
pub fn is_named(&self) -> bool
Check if this node is named.
Named nodes correspond to named rules in the grammar, whereas anonymous nodes correspond to string literals in the grammar.
sourcepub fn is_extra(&self) -> bool
pub fn is_extra(&self) -> bool
Check if this node is extra.
Extra nodes represent things like comments, which are not required the grammar, but can appear anywhere.
sourcepub fn has_changes(&self) -> bool
pub fn has_changes(&self) -> bool
Check if this node has been edited.
sourcepub fn has_error(&self) -> bool
pub fn has_error(&self) -> bool
Check if this node represents a syntax error or contains any syntax errors anywhere within it.
sourcepub fn is_error(&self) -> bool
pub fn is_error(&self) -> bool
Check if this node represents a syntax error.
Syntax errors represent parts of the code that could not be incorporated into a valid syntax tree.
sourcepub fn parse_state(&self) -> u16
pub fn parse_state(&self) -> u16
Get this node’s parse state.
sourcepub fn next_parse_state(&self) -> u16
pub fn next_parse_state(&self) -> u16
Get the parse state after this node.
sourcepub fn is_missing(&self) -> bool
pub fn is_missing(&self) -> bool
Check if this node is missing.
Missing nodes are inserted by the parser in order to recover from certain kinds of syntax errors.
sourcepub fn start_byte(&self) -> usize
pub fn start_byte(&self) -> usize
Get the byte offsets where this node starts.
sourcepub fn byte_range(&self) -> Range<usize>
pub fn byte_range(&self) -> Range<usize>
Get the byte range of source code that this node represents.
sourcepub fn range(&self) -> Range
pub fn range(&self) -> Range
Get the range of source code that this node represents, both in terms of raw bytes and of row/column coordinates.
sourcepub fn start_position(&self) -> Point
pub fn start_position(&self) -> Point
Get this node’s start position in terms of rows and columns.
sourcepub fn end_position(&self) -> Point
pub fn end_position(&self) -> Point
Get this node’s end position in terms of rows and columns.
sourcepub fn child(&self, i: usize) -> Option<Self>
pub fn child(&self, i: usize) -> Option<Self>
Get the node’s child at the given index, where zero represents the first child.
This method is fairly fast, but its cost is technically log(i), so if
you might be iterating over a long list of children, you should use
Node::children
instead.
sourcepub fn child_count(&self) -> usize
pub fn child_count(&self) -> usize
Get this node’s number of children.
sourcepub fn named_child(&self, i: usize) -> Option<Self>
pub fn named_child(&self, i: usize) -> Option<Self>
Get this node’s named child at the given index.
See also Node::is_named
.
This method is fairly fast, but its cost is technically log(i), so if
you might be iterating over a long list of children, you should use
Node::named_children
instead.
sourcepub fn named_child_count(&self) -> usize
pub fn named_child_count(&self) -> usize
Get this node’s number of named children.
See also Node::is_named
.
sourcepub fn child_by_field_name(&self, field_name: impl AsRef<[u8]>) -> Option<Self>
pub fn child_by_field_name(&self, field_name: impl AsRef<[u8]>) -> Option<Self>
Get the first child with the given field name.
If multiple children may have the same field name, access them using
children_by_field_name
sourcepub fn child_by_field_id(&self, field_id: u16) -> Option<Self>
pub fn child_by_field_id(&self, field_id: u16) -> Option<Self>
Get this node’s child with the given numerical field id.
See also child_by_field_name
. You can
convert a field name to an id using Language::field_id_for_name
.
sourcepub fn field_name_for_child(&self, child_index: u32) -> Option<&'static str>
pub fn field_name_for_child(&self, child_index: u32) -> Option<&'static str>
Get the field name of this node’s child at the given index.
sourcepub fn children<'cursor>(
&self,
cursor: &'cursor mut TreeCursor<'tree>
) -> impl ExactSizeIterator<Item = Node<'tree>> + 'cursor
pub fn children<'cursor>( &self, cursor: &'cursor mut TreeCursor<'tree> ) -> impl ExactSizeIterator<Item = Node<'tree>> + 'cursor
Iterate over this node’s children.
A TreeCursor
is used to retrieve the children efficiently. Obtain
a TreeCursor
by calling Tree::walk
or Node::walk
. To avoid
unnecessary allocations, you should reuse the same cursor for
subsequent calls to this method.
If you’re walking the tree recursively, you may want to use the
TreeCursor
APIs directly instead.
sourcepub fn named_children<'cursor>(
&self,
cursor: &'cursor mut TreeCursor<'tree>
) -> impl ExactSizeIterator<Item = Node<'tree>> + 'cursor
pub fn named_children<'cursor>( &self, cursor: &'cursor mut TreeCursor<'tree> ) -> impl ExactSizeIterator<Item = Node<'tree>> + 'cursor
Iterate over this node’s named children.
See also Node::children
.
sourcepub fn children_by_field_name<'cursor>(
&self,
field_name: &str,
cursor: &'cursor mut TreeCursor<'tree>
) -> impl Iterator<Item = Node<'tree>> + 'cursor
pub fn children_by_field_name<'cursor>( &self, field_name: &str, cursor: &'cursor mut TreeCursor<'tree> ) -> impl Iterator<Item = Node<'tree>> + 'cursor
Iterate over this node’s children with a given field name.
See also Node::children
.
sourcepub fn children_by_field_id<'cursor>(
&self,
field_id: NonZeroU16,
cursor: &'cursor mut TreeCursor<'tree>
) -> impl Iterator<Item = Node<'tree>> + 'cursor
pub fn children_by_field_id<'cursor>( &self, field_id: NonZeroU16, cursor: &'cursor mut TreeCursor<'tree> ) -> impl Iterator<Item = Node<'tree>> + 'cursor
Iterate over this node’s children with a given field id.
See also Node::children_by_field_name
.
sourcepub fn parent(&self) -> Option<Self>
pub fn parent(&self) -> Option<Self>
Get this node’s immediate parent.
Prefer child_containing_descendant
for iterating over this node’s ancestors.
sourcepub fn child_containing_descendant(&self, descendant: Self) -> Option<Self>
pub fn child_containing_descendant(&self, descendant: Self) -> Option<Self>
Get this node’s child that contains descendant
.
sourcepub fn next_sibling(&self) -> Option<Self>
pub fn next_sibling(&self) -> Option<Self>
Get this node’s next sibling.
sourcepub fn prev_sibling(&self) -> Option<Self>
pub fn prev_sibling(&self) -> Option<Self>
Get this node’s previous sibling.
sourcepub fn next_named_sibling(&self) -> Option<Self>
pub fn next_named_sibling(&self) -> Option<Self>
Get this node’s next named sibling.
sourcepub fn prev_named_sibling(&self) -> Option<Self>
pub fn prev_named_sibling(&self) -> Option<Self>
Get this node’s previous named sibling.
sourcepub fn descendant_count(&self) -> usize
pub fn descendant_count(&self) -> usize
Get the node’s number of descendants, including one for the node itself.
sourcepub fn descendant_for_byte_range(
&self,
start: usize,
end: usize
) -> Option<Self>
pub fn descendant_for_byte_range( &self, start: usize, end: usize ) -> Option<Self>
Get the smallest node within this node that spans the given range.
sourcepub fn named_descendant_for_byte_range(
&self,
start: usize,
end: usize
) -> Option<Self>
pub fn named_descendant_for_byte_range( &self, start: usize, end: usize ) -> Option<Self>
Get the smallest named node within this node that spans the given range.
sourcepub fn descendant_for_point_range(
&self,
start: Point,
end: Point
) -> Option<Self>
pub fn descendant_for_point_range( &self, start: Point, end: Point ) -> Option<Self>
Get the smallest node within this node that spans the given range.
sourcepub fn named_descendant_for_point_range(
&self,
start: Point,
end: Point
) -> Option<Self>
pub fn named_descendant_for_point_range( &self, start: Point, end: Point ) -> Option<Self>
Get the smallest named node within this node that spans the given range.
pub fn to_sexp(&self) -> String
pub fn utf8_text<'a>(&self, source: &'a [u8]) -> Result<&'a str, Utf8Error>
pub fn utf16_text<'a>(&self, source: &'a [u16]) -> &'a [u16]
sourcepub fn walk(&self) -> TreeCursor<'tree>
pub fn walk(&self) -> TreeCursor<'tree>
Create a new TreeCursor
starting from this node.
sourcepub fn edit(&mut self, edit: &InputEdit)
pub fn edit(&mut self, edit: &InputEdit)
Edit this node to keep it in-sync with source code that has been edited.
This function is only rarely needed. When you edit a syntax tree with
the Tree::edit
method, all of the nodes that you retrieve from
the tree afterward will already reflect the edit. You only need to
use Node::edit
when you have a specific Node
instance that
you want to keep and continue to use after an edit.