pub struct AstNode<'r, D>where
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> Node<'r, D>where
D: Doc,
APIs for Node inspection
impl<'r, D> Node<'r, D>where
D: Doc,
APIs for Node inspection
pub const fn get_doc(&self) -> &'r D
pub fn node_id(&self) -> usize
pub fn is_leaf(&self) -> bool
Sourcepub fn is_named_leaf(&self) -> bool
pub fn is_named_leaf(&self) -> bool
if has no named children.
N.B. it is different from is_named && is_leaf
pub fn is_error(&self) -> bool
pub fn kind(&self) -> Cow<'_, str>
pub fn kind_id(&self) -> u16
pub fn is_named(&self) -> bool
pub fn is_missing(&self) -> bool
Sourcepub fn start_pos(&self) -> Position
pub fn start_pos(&self) -> Position
Nodes’ start position in terms of zero-based rows and columns.
pub fn text(&self) -> Cow<'r, str>
pub fn lang(&self) -> &'r <D as Doc>::Lang
Sourcepub fn get_inner_node(&self) -> <D as Doc>::Node<'r>
pub fn get_inner_node(&self) -> <D as Doc>::Node<'r>
the underlying tree-sitter Node
pub const fn root(&self) -> &'r Root<D>
Source§impl<D> Node<'_, D>where
D: Doc,
Corresponds to inside/has/precedes/follows
impl<D> Node<'_, D>where
D: Doc,
Corresponds to inside/has/precedes/follows
pub fn matches<M>(&self, m: M) -> boolwhere
M: Matcher,
pub fn inside<M>(&self, m: M) -> boolwhere
M: Matcher,
pub fn has<M>(&self, m: M) -> boolwhere
M: Matcher,
pub fn precedes<M>(&self, m: M) -> boolwhere
M: Matcher,
pub fn follows<M>(&self, m: M) -> boolwhere
M: Matcher,
Source§impl<'r, D> Node<'r, D>where
D: Doc,
tree traversal API
impl<'r, D> Node<'r, D>where
D: Doc,
tree traversal API
pub fn parent(&self) -> Option<Node<'r, D>>
pub fn children(&self) -> impl ExactSizeIterator
pub fn child(&self, nth: usize) -> Option<Node<'r, D>>
pub fn field(&self, name: &str) -> Option<Node<'r, D>>
pub fn child_by_field_id(&self, field_id: u16) -> Option<Node<'r, D>>
pub fn field_children(&self, name: &str) -> impl Iterator<Item = Node<'r, D>>
Sourcepub fn ancestors(&self) -> impl Iterator<Item = Node<'r, D>>
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.
pub fn next(&self) -> Option<Node<'r, D>>
Sourcepub fn next_all(&self) -> impl Iterator<Item = Node<'r, D>>
pub fn next_all(&self) -> impl Iterator<Item = Node<'r, D>>
Returns all sibling nodes next to self.
pub fn prev(&self) -> Option<Node<'r, D>>
pub fn prev_all(&self) -> impl Iterator<Item = Node<'r, D>>
pub fn dfs<'s>(&'s self) -> impl Iterator<Item = Node<'r, D>> + 's
pub fn find<M>(&self, pat: M) -> Option<NodeMatch<'r, D>>where
M: Matcher,
pub fn find_all<'s, M>(
&'s self,
pat: M,
) -> impl Iterator<Item = NodeMatch<'r, D>> + 'swhere
M: Matcher + 's,
Source§impl<D> Node<'_, D>where
D: Doc,
Tree manipulation API
impl<D> Node<'_, D>where
D: Doc,
Tree manipulation API
pub fn replace<M, R>( &self, matcher: M, replacer: R, ) -> Option<Edit<<D as Doc>::Source>>
pub fn after(&self) -> Edit<<D as Doc>::Source>
pub fn before(&self) -> Edit<<D as Doc>::Source>
pub fn append(&self) -> Edit<<D as Doc>::Source>
pub fn prepend(&self) -> Edit<<D as Doc>::Source>
Trait Implementations§
Source§impl<'tree, D> Borrow<Node<'tree, D>> for NodeMatch<'tree, D>where
D: Doc,
NodeMatch is an immutable view to Node
impl<'tree, D> Borrow<Node<'tree, D>> for NodeMatch<'tree, D>where
D: Doc,
NodeMatch is an immutable view to Node
Source§impl<'r, D> From<Node<'r, D>> for PatternNodewhere
D: Doc,
impl<'r, D> From<Node<'r, D>> for PatternNodewhere
D: Doc,
Source§fn from(node: Node<'r, D>) -> PatternNode
fn from(node: Node<'r, D>) -> PatternNode
Source§impl<'tree, D> From<NodeMatch<'tree, D>> for Node<'tree, D>where
D: Doc,
NodeMatch is an immutable view to Node
impl<'tree, D> From<NodeMatch<'tree, D>> for Node<'tree, D>where
D: Doc,
NodeMatch is an immutable view to Node
Auto Trait Implementations§
impl<'r, D> Freeze for Node<'r, D>
impl<'r, D> RefUnwindSafe for Node<'r, D>
impl<'r, D> Send for Node<'r, D>
impl<'r, D> Sync for Node<'r, D>
impl<'r, D> Unpin for Node<'r, D>
impl<'r, D> UnsafeUnpin for Node<'r, D>
impl<'r, D> UnwindSafe for Node<'r, D>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more