Struct rowan::SyntaxNode
source · Expand description
An immutable lazy constructed syntax tree with offsets and parent pointers.
The design is close to https://github.com/apple/swift/tree/bc3189a2d265bf7728ea0cfeb55f032bfe5beaf1/lib/Syntax
SyntaxNode exists in two flavors:
- owned (R = OwnedRoot
) - borrowed (R = RefRoot<’a, T>)
Borrowed SyntaxNode is Copy, but is parametrized over a lifetime,
with a corresponding ergonomics hit.
Owned SyntaxNode is Clone (using Arc::clone under the hood) and
is not parametrized over a lifetime. Note that because of the parent
links SyntaxNode keeps all of its ancestors alive, and not only descendants,
so keep an eye on memory leaks.
Methods like parent or children preserve the flavor (borrowed or owned)
of nodes, but you can switch between them at any time using .borrowed()
and .owned() methods. As a rule of thumb, when processing nodes, use
borrowed version to avoid excessive Arc traffic, and, when storing nodes
in data structures, use owned variant, to avoid dealing with lifetimes.
SyntaxNode have object identity equality and hash semantics.
Implementations
sourceimpl<T: Types> SyntaxNode<T, OwnedRoot<T>>
impl<T: Types> SyntaxNode<T, OwnedRoot<T>>
sourceimpl<'a, T: Types> SyntaxNode<T, RefRoot<'a, T>>
impl<'a, T: Types> SyntaxNode<T, RefRoot<'a, T>>
sourcepub fn ancestors(self) -> impl Iterator<Item = SyntaxNode<T, RefRoot<'a, T>>>
pub fn ancestors(self) -> impl Iterator<Item = SyntaxNode<T, RefRoot<'a, T>>>
All ancestors of the current node, including itself
sourcepub fn preorder(
self
) -> impl Iterator<Item = WalkEvent<SyntaxNode<T, RefRoot<'a, T>>>>
pub fn preorder(
self
) -> impl Iterator<Item = WalkEvent<SyntaxNode<T, RefRoot<'a, T>>>>
Traverse the subtree rooted at the current node in preorder.
sourcepub fn common_ancestor(
self,
other: SyntaxNode<T, RefRoot<'_, T>>
) -> SyntaxNode<T, RefRoot<'a, T>>
pub fn common_ancestor(
self,
other: SyntaxNode<T, RefRoot<'_, T>>
) -> SyntaxNode<T, RefRoot<'a, T>>
Returns common ancestor of the two nodes. Precondition: nodes must be from the same tree.
sourcepub fn leaf_at_offset(
self,
offset: TextUnit
) -> LeafAtOffset<SyntaxNode<T, RefRoot<'a, T>>> ⓘ
pub fn leaf_at_offset(
self,
offset: TextUnit
) -> LeafAtOffset<SyntaxNode<T, RefRoot<'a, T>>> ⓘ
Find a leaf in the subtree corresponding to this node, which covers the offset. Precondition: offset must be withing node’s range.
sourcepub fn covering_node(self, range: TextRange) -> SyntaxNode<T, RefRoot<'a, T>>
pub fn covering_node(self, range: TextRange) -> SyntaxNode<T, RefRoot<'a, T>>
Return the deepest node in the current subtree that fully contains the range. If the range is empty and is contained in two leaf nodes, either one can be returned. Precondition: range must be contained withing the current node
sourceimpl<T: Types, R: TreeRoot<T>> SyntaxNode<T, R>
impl<T: Types, R: TreeRoot<T>> SyntaxNode<T, R>
sourcepub fn borrowed<'a>(&'a self) -> SyntaxNode<T, RefRoot<'a, T>>
pub fn borrowed<'a>(&'a self) -> SyntaxNode<T, RefRoot<'a, T>>
Switch this node to borrowed flavor.
sourcepub fn owned(&self) -> SyntaxNode<T, OwnedRoot<T>>
pub fn owned(&self) -> SyntaxNode<T, OwnedRoot<T>>
Switch this node to owned flavor.
sourcepub fn parent(&self) -> Option<SyntaxNode<T, R>>
pub fn parent(&self) -> Option<SyntaxNode<T, R>>
Get the parent node.
sourcepub fn children(&self) -> SyntaxNodeChildren<T, R> ⓘ
pub fn children(&self) -> SyntaxNodeChildren<T, R> ⓘ
Get iterator over children.
sourcepub fn first_child(&self) -> Option<SyntaxNode<T, R>>
pub fn first_child(&self) -> Option<SyntaxNode<T, R>>
Get first child.
sourcepub fn last_child(&self) -> Option<SyntaxNode<T, R>>
pub fn last_child(&self) -> Option<SyntaxNode<T, R>>
Get last child.
sourcepub fn next_sibling(&self) -> Option<SyntaxNode<T, R>>
pub fn next_sibling(&self) -> Option<SyntaxNode<T, R>>
Get next sibling.
sourcepub fn prev_sibling(&self) -> Option<SyntaxNode<T, R>>
pub fn prev_sibling(&self) -> Option<SyntaxNode<T, R>>
Get previous sibling.
sourcepub fn replace_with(&self, green: GreenNode<T>) -> GreenNode<T>
pub fn replace_with(&self, green: GreenNode<T>) -> GreenNode<T>
Returns a green tree, equal to the green tree this node belongs two, except with this node substitute. The complexity of operation is proportional to the depth of the tree TODO: naming is unfortunate, the return value is not current node, it is the new root node.
sourcepub fn with_children(&self, children: Box<[GreenNode<T>]>) -> GreenNode<T>
pub fn with_children(&self, children: Box<[GreenNode<T>]>) -> GreenNode<T>
Get the root node but with the children replaced. See replace_with.
Trait Implementations
sourceimpl<T: Types, R: TreeRoot<T> + Clone> Clone for SyntaxNode<T, R>
impl<T: Types, R: TreeRoot<T> + Clone> Clone for SyntaxNode<T, R>
sourcefn clone(&self) -> SyntaxNode<T, R>
fn clone(&self) -> SyntaxNode<T, R>
1.0.0 · sourcefn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more