Struct tree_sitter::Node

source ·
#[repr(transparent)]
pub struct Node<'a>(_, _);
Expand description

A single node within a syntax Tree.

Implementations§

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 kind(&self) -> &'static str

Get this node’s type as a string.

source

pub fn language(&self) -> Language

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 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 you 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<'a>(&'a 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 you 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<'a>( &self, cursor: &'a mut TreeCursor<'tree> ) -> impl ExactSizeIterator<Item = Node<'tree>> + 'a

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<'a>( &self, cursor: &'a mut TreeCursor<'tree> ) -> impl ExactSizeIterator<Item = Node<'tree>> + 'a

Iterate over this node’s named children.

See also Node::children.

source

pub fn children_by_field_name<'a>( &self, field_name: &str, cursor: &'a mut TreeCursor<'tree> ) -> impl Iterator<Item = Node<'tree>> + 'a

Iterate over this node’s children with a given field name.

See also Node::children.

source

pub fn children_by_field_id<'a>( &self, field_id: u16, cursor: &'a mut TreeCursor<'tree> ) -> impl Iterator<Item = Node<'tree>> + 'a

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.

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_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<'a> Clone for Node<'a>

source§

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

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<'a> Debug for Node<'a>

source§

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

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

impl<'a> Hash for Node<'a>

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<'a> PartialEq<Node<'a>> for Node<'a>

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<'a> Copy for Node<'a>

source§

impl<'a> Eq for Node<'a>

Auto Trait Implementations§

§

impl<'a> RefUnwindSafe for Node<'a>

§

impl<'a> !Send for Node<'a>

§

impl<'a> !Sync for Node<'a>

§

impl<'a> Unpin for Node<'a>

§

impl<'a> UnwindSafe for Node<'a>

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

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

const: unstable · 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 Twhere 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, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
source§

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

§

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

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.