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>

source

pub const unsafe fn from_raw(raw: TSNode) -> Self

Reconstructs a Node from a raw pointer.

§Safety

ptr must be non-null.

source

pub fn into_raw(self) -> TSNode

Consumes the Node, returning a raw pointer to the underlying C structure.

source§

impl<'tree> Node<'tree>

source

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.

source

pub fn kind_id(&self) -> u16

Get this node’s type as a numerical id.

source

pub fn grammar_id(&self) -> u16

Get the node’s type as a numerical id as it appears in the grammar ignoring aliases.

source

pub fn kind(&self) -> &'static str

Get this node’s type as a string.

source

pub fn grammar_name(&self) -> &'static str

Get this node’s symbol name as it appears in the grammar ignoring aliases as a string.

source

pub fn language(&self) -> LanguageRef<'_>

Get the Language that was used to parse this node’s syntax tree.

source

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.

source

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.

source

pub fn has_changes(&self) -> bool

Check if this node has been edited.

source

pub fn has_error(&self) -> bool

Check if this node represents a syntax error or contains any syntax errors anywhere within it.

source

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.

source

pub fn parse_state(&self) -> u16

Get this node’s parse state.

source

pub fn next_parse_state(&self) -> u16

Get the parse state after this node.

source

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.

source

pub fn start_byte(&self) -> usize

Get the byte offsets where this node starts.

source

pub fn end_byte(&self) -> usize

Get the byte offsets where this node end.

source

pub fn byte_range(&self) -> Range<usize>

Get the byte range of source code that this node represents.

source

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.

source

pub fn start_position(&self) -> Point

Get this node’s start position in terms of rows and columns.

source

pub fn end_position(&self) -> Point

Get this node’s end position in terms of rows and columns.

source

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.

source

pub fn child_count(&self) -> usize

Get this node’s number of children.

source

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.

source

pub fn named_child_count(&self) -> usize

Get this node’s number of named children.

See also Node::is_named.

source

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

source

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.

source

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.

source

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.

source

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.

source

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.

source

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.

source

pub fn parent(&self) -> Option<Self>

Get this node’s immediate parent. Prefer child_containing_descendant for iterating over this node’s ancestors.

source

pub fn child_containing_descendant(&self, descendant: Self) -> Option<Self>

Get this node’s child that contains descendant.

source

pub fn next_sibling(&self) -> Option<Self>

Get this node’s next sibling.

source

pub fn prev_sibling(&self) -> Option<Self>

Get this node’s previous sibling.

source

pub fn next_named_sibling(&self) -> Option<Self>

Get this node’s next named sibling.

source

pub fn prev_named_sibling(&self) -> Option<Self>

Get this node’s previous named sibling.

source

pub fn descendant_count(&self) -> usize

Get the node’s number of descendants, including one for the node itself.

source

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.

source

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.

source

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.

source

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.

source

pub fn to_sexp(&self) -> String

source

pub fn utf8_text<'a>(&self, source: &'a [u8]) -> Result<&'a str, Utf8Error>

source

pub fn utf16_text<'a>(&self, source: &'a [u16]) -> &'a [u16]

source

pub fn walk(&self) -> TreeCursor<'tree>

Create a new TreeCursor starting from this node.

source

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.

Trait Implementations§

source§

impl<'tree> Clone for Node<'tree>

source§

fn clone(&self) -> Node<'tree>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Node<'_>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Display for Node<'_>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Hash for Node<'_>

source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl PartialEq for Node<'_>

source§

fn eq(&self, other: &Self) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<'tree> Copy for Node<'tree>

source§

impl Eq for Node<'_>

source§

impl Send for Node<'_>

source§

impl Sync for Node<'_>

Auto Trait Implementations§

§

impl<'tree> Freeze for Node<'tree>

§

impl<'tree> RefUnwindSafe for Node<'tree>

§

impl<'tree> Unpin for Node<'tree>

§

impl<'tree> UnwindSafe for Node<'tree>

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.