Skip to main content

Node

Struct Node 

Source
pub struct Node<'r, D: Doc> { /* private fields */ }
Expand description

A single node in an Abstract Syntax Tree.

Node represents a specific element in the parsed AST, such as a function declaration, variable assignment, or expression. Each node knows its position in the source code, its type (kind), and provides methods for navigation and pattern matching.

§Lifetime

The lifetime 'r ties the node to its root AST, ensuring memory safety. Nodes cannot outlive the Root that owns the underlying tree structure.

§Example

let ast = Language::Tsx.ast_grep("function hello() { return 'world'; }");
let root_node = ast.root();

// Check the node type
println!("Root kind: {}", root_node.kind());

// Navigate to children
for child in root_node.children() {
    println!("Child: {} at {}:{}", child.kind(),
        child.start_pos().line(), child.start_pos().column(&child));
}

// Find specific patterns
if let Some(return_stmt) = root_node.find("return $VALUE") {
    let value = return_stmt.get_env().get_match("VALUE").unwrap();
    println!("Returns: {}", value.text());
}

Implementations§

Source§

impl<'r, D: Doc> Node<'r, D>

APIs for Node inspection

Source

pub const fn get_doc(&self) -> &'r D

Source

pub fn node_id(&self) -> usize

Source

pub fn is_leaf(&self) -> bool

Source

pub fn is_named_leaf(&self) -> bool

if has no named children. N.B. it is different from is_named && is_leaf

Source

pub fn is_error(&self) -> bool

Source

pub fn kind(&self) -> Cow<'_, str>

Source

pub fn kind_id(&self) -> u16

Source

pub fn is_named(&self) -> bool

Source

pub fn is_missing(&self) -> bool

Source

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

byte offsets of start and end.

Source

pub fn start_pos(&self) -> Position

Nodes’ start position in terms of zero-based rows and columns.

Source

pub fn end_pos(&self) -> Position

Nodes’ end position in terms of rows and columns.

Source

pub fn text(&self) -> Cow<'r, str>

Source

pub fn lang(&self) -> &'r D::Lang

Source

pub fn get_inner_node(&self) -> D::Node<'r>

the underlying tree-sitter Node

Source

pub const fn root(&self) -> &'r Root<D>

Source§

impl<D: Doc> Node<'_, D>

Corresponds to inside/has/precedes/follows

Source

pub fn matches<M: Matcher>(&self, m: M) -> bool

Source

pub fn inside<M: Matcher>(&self, m: M) -> bool

Source

pub fn has<M: Matcher>(&self, m: M) -> bool

Source

pub fn precedes<M: Matcher>(&self, m: M) -> bool

Source

pub fn follows<M: Matcher>(&self, m: M) -> bool

Source§

impl<'r, D: Doc> Node<'r, D>

tree traversal API

Source

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

Source

pub fn children(&self) -> impl ExactSizeIterator<Item = Node<'r, D>> + '_

Source

pub fn child(&self, nth: usize) -> Option<Self>

Source

pub fn field(&self, name: &str) -> Option<Self>

Source

pub fn child_by_field_id(&self, field_id: u16) -> Option<Self>

Source

pub fn field_children( &self, name: &str, ) -> impl Iterator<Item = Node<'r, D>> + '_

Source

pub fn ancestors(&self) -> impl Iterator<Item = Node<'r, D>> + '_

Returns all ancestors nodes of self. Using cursor is overkill here because adjust cursor is too expensive.

Source

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

Source

pub fn next_all(&self) -> impl Iterator<Item = Node<'r, D>> + '_

Returns all sibling nodes next to self.

Source

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

Source

pub fn prev_all(&self) -> impl Iterator<Item = Node<'r, D>> + '_

Source

pub fn dfs<'s>(&'s self) -> impl Iterator<Item = Node<'r, D>> + 's

Source

pub fn find<M: Matcher>(&self, pat: M) -> Option<NodeMatch<'r, D>>

Source

pub fn find_all<'s, M: Matcher + 's>( &'s self, pat: M, ) -> impl Iterator<Item = NodeMatch<'r, D>> + 's

Source§

impl<D: Doc> Node<'_, D>

Tree manipulation API

Source

pub fn replace<M: Matcher, R: Replacer<D>>( &self, matcher: M, replacer: R, ) -> Option<Edit<<D as Doc>::Source>>

Source

pub fn after(&self) -> Edit<<D as Doc>::Source>

Source

pub fn before(&self) -> Edit<<D as Doc>::Source>

Source

pub fn append(&self) -> Edit<<D as Doc>::Source>

Source

pub fn prepend(&self) -> Edit<<D as Doc>::Source>

Source

pub fn empty(&self) -> Option<Edit<<D as Doc>::Source>>

Empty children. Remove all child node

Source

pub fn remove(&self) -> Edit<<D as Doc>::Source>

Remove the node itself

Source§

impl<'r, L: LanguageExt> Node<'r, StrDoc<L>>

these methods are only for StrDoc

Source

pub fn replace_all<M: Matcher, R: Replacer<StrDoc<L>>>( &self, matcher: M, replacer: R, ) -> Vec<Edit<String>>

Trait Implementations§

Source§

impl<'tree, D: Doc> Borrow<Node<'tree, D>> for NodeMatch<'tree, D>

NodeMatch is an immutable view to Node

Source§

fn borrow(&self) -> &Node<'tree, D>

Immutably borrows from an owned value. Read more
Source§

impl<'r, D: Clone + Doc> Clone for Node<'r, D>
where D::Node<'r>: Clone,

Source§

fn clone(&self) -> Node<'r, D>

Returns a duplicate 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<'r, D: Debug + Doc> Debug for Node<'r, D>
where D::Node<'r>: Debug,

Source§

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

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

impl<'r, D: Doc> From<Node<'r, D>> for Pattern

Source§

fn from(node: Node<'r, D>) -> Self

Converts to this type from the input type.
Source§

impl<'r, D: Doc> From<Node<'r, D>> for PatternNode

Source§

fn from(node: Node<'r, D>) -> Self

Converts to this type from the input type.
Source§

impl<'tree, D: Doc> From<Node<'tree, D>> for NodeMatch<'tree, D>

Source§

fn from(node: Node<'tree, D>) -> Self

Converts to this type from the input type.
Source§

impl<'tree, D: Doc> From<NodeMatch<'tree, D>> for Node<'tree, D>

NodeMatch is an immutable view to Node

Source§

fn from(node_match: NodeMatch<'tree, D>) -> Self

Converts to this type from the input type.
Source§

impl<D: Doc> Replacer<D> for Node<'_, D>

Source§

fn generate_replacement(&self, _nm: &NodeMatch<'_, D>) -> Underlying<D>

Generate replacement content for a matched node. Read more
Source§

fn get_replaced_range( &self, nm: &NodeMatch<'_, D>, matcher: impl Matcher, ) -> Range<usize>

Determine the exact range of source code to replace. Read more

Auto Trait Implementations§

§

impl<'r, D> Freeze for Node<'r, D>
where <D as Doc>::Node<'r>: Freeze,

§

impl<'r, D> RefUnwindSafe for Node<'r, D>
where <D as Doc>::Node<'r>: RefUnwindSafe, D: RefUnwindSafe,

§

impl<'r, D> Send for Node<'r, D>

§

impl<'r, D> Sync for Node<'r, D>

§

impl<'r, D> Unpin for Node<'r, D>
where <D as Doc>::Node<'r>: Unpin,

§

impl<'r, D> UnsafeUnpin for Node<'r, D>
where <D as Doc>::Node<'r>: UnsafeUnpin,

§

impl<'r, D> UnwindSafe for Node<'r, D>
where <D as Doc>::Node<'r>: UnwindSafe, D: RefUnwindSafe,

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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,

Source§

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 T
where U: Into<T>,

Source§

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>,

Source§

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.