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
impl<'r, D: Doc> Node<'r, D>
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::Lang
Sourcepub fn get_inner_node(&self) -> D::Node<'r>
pub fn get_inner_node(&self) -> D::Node<'r>
the underlying tree-sitter Node
pub const fn root(&self) -> &'r Root<D>
Source§impl<'r, D: Doc> Node<'r, D>
tree traversal API
impl<'r, D: Doc> Node<'r, D>
tree traversal API
pub fn parent(&self) -> Option<Self>
pub fn children(&self) -> impl ExactSizeIterator<Item = Node<'r, D>> + '_
pub fn child(&self, nth: usize) -> Option<Self>
pub fn field(&self, name: &str) -> Option<Self>
pub fn child_by_field_id(&self, field_id: u16) -> Option<Self>
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<Self>
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<Self>
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: Matcher>(&self, pat: M) -> Option<NodeMatch<'r, D>>
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
impl<D: Doc> Node<'_, D>
Tree manipulation API
pub fn replace<M: Matcher, R: Replacer<D>>( &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: Doc> Borrow<Node<'tree, D>> for NodeMatch<'tree, D>
NodeMatch is an immutable view to Node
impl<'tree, D: Doc> Borrow<Node<'tree, D>> for NodeMatch<'tree, D>
NodeMatch is an immutable view to Node
Source§impl<'tree, D: Doc> From<NodeMatch<'tree, D>> for Node<'tree, D>
NodeMatch is an immutable view to Node
impl<'tree, D: Doc> From<NodeMatch<'tree, D>> for Node<'tree, D>
NodeMatch is an immutable view to Node