Struct cstree::syntax::SyntaxNode
source · #[repr(transparent)]pub struct SyntaxNode<S: Syntax, D: 'static = ()> { /* private fields */ }
Expand description
Inner syntax tree node. Syntax nodes can be shared between threads. Every syntax tree is reference counted as a whole and nodes are pointer-sized, so copying individual nodes is relatively cheap.
Implementations§
source§impl<S: Syntax, D> SyntaxNode<S, D>
impl<S: Syntax, D> SyntaxNode<S, D>
sourcepub fn write_debug<R>(
&self,
resolver: &R,
target: &mut impl Write,
recursive: bool
) -> Resultwhere
R: Resolver<TokenKey> + ?Sized,
pub fn write_debug<R>( &self, resolver: &R, target: &mut impl Write, recursive: bool ) -> Resultwhere R: Resolver<TokenKey> + ?Sized,
Writes this node’s Debug
representation into the given target
.
If recursive
is true
, prints the entire subtree rooted in this node.
Otherwise, only this node’s kind and range are written.
sourcepub fn debug<R>(&self, resolver: &R, recursive: bool) -> Stringwhere
R: Resolver<TokenKey> + ?Sized,
pub fn debug<R>(&self, resolver: &R, recursive: bool) -> Stringwhere R: Resolver<TokenKey> + ?Sized,
Returns this node’s Debug
representation as a string.
If recursive
is true
, prints the entire subtree rooted in this node.
Otherwise, only this node’s kind and range are written.
To avoid allocating for every node, see write_debug
.
sourcepub fn write_display<R>(&self, resolver: &R, target: &mut impl Write) -> Resultwhere
R: Resolver<TokenKey> + ?Sized,
pub fn write_display<R>(&self, resolver: &R, target: &mut impl Write) -> Resultwhere R: Resolver<TokenKey> + ?Sized,
Writes this node’s Display
representation into the given target
.
sourcepub fn display<R>(&self, resolver: &R) -> Stringwhere
R: Resolver<TokenKey> + ?Sized,
pub fn display<R>(&self, resolver: &R) -> Stringwhere R: Resolver<TokenKey> + ?Sized,
Returns this node’s Display
representation as a string.
To avoid allocating for every node, see write_display
.
sourcepub fn resolver(&self) -> Option<&StdArc<dyn Resolver<TokenKey>>>
pub fn resolver(&self) -> Option<&StdArc<dyn Resolver<TokenKey>>>
If there is a resolver associated with this tree, returns it.
sourcepub fn try_resolved(&self) -> Option<&ResolvedNode<S, D>>
pub fn try_resolved(&self) -> Option<&ResolvedNode<S, D>>
Turns this node into a ResolvedNode
, but only if there is a resolver associated
with this tree.
sourcepub fn resolved(&self) -> &ResolvedNode<S, D>
pub fn resolved(&self) -> &ResolvedNode<S, D>
source§impl<S: Syntax, D> SyntaxNode<S, D>
impl<S: Syntax, D> SyntaxNode<S, D>
sourcepub fn root(&self) -> &SyntaxNode<S, D>
pub fn root(&self) -> &SyntaxNode<S, D>
The root of the tree this node belongs to.
If this node is the root, returns self
.
source§impl<S: Syntax, D> SyntaxNode<S, D>
impl<S: Syntax, D> SyntaxNode<S, D>
sourcepub fn new_root(green: GreenNode) -> Self
pub fn new_root(green: GreenNode) -> Self
Build a new syntax tree on top of a green tree.
Example
let root: SyntaxNode<MySyntax> = SyntaxNode::new_root(green_root);
assert_eq!(root.kind(), Root);
sourcepub fn new_root_with_resolver(
green: GreenNode,
resolver: impl Resolver<TokenKey> + 'static
) -> ResolvedNode<S, D>
pub fn new_root_with_resolver( green: GreenNode, resolver: impl Resolver<TokenKey> + 'static ) -> ResolvedNode<S, D>
Build a new syntax tree on top of a green tree and associate a resolver with the tree to resolve interned Strings.
Example
use cstree::syntax::ResolvedNode;
let mut builder: GreenNodeBuilder<MySyntax> = GreenNodeBuilder::new();
builder.start_node(Root);
builder.token(Identifier, "content");
builder.finish_node();
let (green, cache) = builder.finish();
// We are safe to use `unwrap` here because we created the builder with `new`.
// This created a new interner and cache for us owned by the builder,
// and `finish` always returns these.
let interner = cache.unwrap().into_interner().unwrap();
let root: ResolvedNode<MySyntax> = SyntaxNode::new_root_with_resolver(green, interner);
assert_eq!(root.text(), "content");
sourcepub fn set_data(&self, data: D) -> Arc<D>
pub fn set_data(&self, data: D) -> Arc<D>
Stores custom data for this node. If there was previous data associated with this node, it will be replaced.
sourcepub fn try_set_data(&self, data: D) -> Result<Arc<D>, D>
pub fn try_set_data(&self, data: D) -> Result<Arc<D>, D>
Stores custom data for this node, but only if no data was previously set. If it was, the given data is returned unchanged.
sourcepub fn clear_data(&self)
pub fn clear_data(&self)
Removes the data associated with this node.
sourcepub fn replace_with(&self, replacement: GreenNode) -> GreenNode
pub fn replace_with(&self, replacement: GreenNode) -> GreenNode
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
sourcepub fn syntax_kind(&self) -> RawSyntaxKind
pub fn syntax_kind(&self) -> RawSyntaxKind
The internal representation of the kind of this node.
sourcepub fn text_range(&self) -> TextRange
pub fn text_range(&self) -> TextRange
The range this node covers in the source text, in bytes.
sourcepub fn resolve_text<'n, 'i, I>(
&'n self,
resolver: &'i I
) -> SyntaxText<'n, 'i, I, S, D>where
I: Resolver<TokenKey> + ?Sized,
pub fn resolve_text<'n, 'i, I>( &'n self, resolver: &'i I ) -> SyntaxText<'n, 'i, I, S, D>where I: Resolver<TokenKey> + ?Sized,
Uses the provided resolver to return an efficient representation of all source text covered by this node, i.e. the combined text of all token leafs of the subtree originating in this node.
sourcepub fn parent(&self) -> Option<&SyntaxNode<S, D>>
pub fn parent(&self) -> Option<&SyntaxNode<S, D>>
The parent node of this node, except if this node is the root.
sourcepub fn arity(&self) -> usize
pub fn arity(&self) -> usize
The number of child nodes (!) of this node.
If you want to also consider leafs, see arity_with_tokens
.
sourcepub fn arity_with_tokens(&self) -> usize
pub fn arity_with_tokens(&self) -> usize
The number of children of this node.
sourcepub fn ancestors(&self) -> impl Iterator<Item = &SyntaxNode<S, D>>
pub fn ancestors(&self) -> impl Iterator<Item = &SyntaxNode<S, D>>
Returns an iterator along the chain of parents of this node.
sourcepub fn children(&self) -> SyntaxNodeChildren<'_, S, D> ⓘ
pub fn children(&self) -> SyntaxNodeChildren<'_, S, D> ⓘ
Returns an iterator over all nodes that are children of this node.
If you want to also consider leafs, see children_with_tokens
.
sourcepub fn children_with_tokens(&self) -> SyntaxElementChildren<'_, S, D> ⓘ
pub fn children_with_tokens(&self) -> SyntaxElementChildren<'_, S, D> ⓘ
Returns an iterator over child elements of this node, including tokens.
sourcepub fn first_child(&self) -> Option<&SyntaxNode<S, D>>
pub fn first_child(&self) -> Option<&SyntaxNode<S, D>>
The first child node of this node, if any.
If you want to also consider leafs, see first_child_or_token
.
sourcepub fn first_child_or_token(&self) -> Option<SyntaxElementRef<'_, S, D>>
pub fn first_child_or_token(&self) -> Option<SyntaxElementRef<'_, S, D>>
The first child element of this node, if any, including tokens.
sourcepub fn last_child(&self) -> Option<&SyntaxNode<S, D>>
pub fn last_child(&self) -> Option<&SyntaxNode<S, D>>
The last child node of this node, if any.
If you want to also consider leafs, see last_child_or_token
.
sourcepub fn last_child_or_token(&self) -> Option<SyntaxElementRef<'_, S, D>>
pub fn last_child_or_token(&self) -> Option<SyntaxElementRef<'_, S, D>>
The last child element of this node, if any, including tokens.
sourcepub fn next_child_after(
&self,
n: usize,
offset: TextSize
) -> Option<&SyntaxNode<S, D>>
pub fn next_child_after( &self, n: usize, offset: TextSize ) -> Option<&SyntaxNode<S, D>>
The first child node of this node starting at the (n + 1)-st, if any.
Note that even if this method returns Some
, the contained node may not actually be the (n +
1)-st child, but the next child from there that is a node.
If you want to also consider leafs, see next_child_or_token_after
.
sourcepub fn next_child_or_token_after(
&self,
n: usize,
offset: TextSize
) -> Option<SyntaxElementRef<'_, S, D>>
pub fn next_child_or_token_after( &self, n: usize, offset: TextSize ) -> Option<SyntaxElementRef<'_, S, D>>
The first child element of this node starting at the (n + 1)-st, if any.
If this method returns Some
, the contained node is the (n + 1)-st child of this node.
sourcepub fn prev_child_before(
&self,
n: usize,
offset: TextSize
) -> Option<&SyntaxNode<S, D>>
pub fn prev_child_before( &self, n: usize, offset: TextSize ) -> Option<&SyntaxNode<S, D>>
The last child node of this node up to the nth, if any.
Note that even if this method returns Some
, the contained node may not actually be the (n -
1)-st child, but the previous child from there that is a node.
If you want to also consider leafs, see prev_child_or_token_before
.
sourcepub fn prev_child_or_token_before(
&self,
n: usize,
offset: TextSize
) -> Option<SyntaxElementRef<'_, S, D>>
pub fn prev_child_or_token_before( &self, n: usize, offset: TextSize ) -> Option<SyntaxElementRef<'_, S, D>>
The last child node of this node up to the nth, if any.
If this method returns Some
, the contained node is the (n - 1)-st child.
sourcepub fn next_sibling(&self) -> Option<&SyntaxNode<S, D>>
pub fn next_sibling(&self) -> Option<&SyntaxNode<S, D>>
The node to the right of this one, i.e. the next child node (!) of this node’s parent after this node.
If you want to also consider leafs, see next_sibling_or_token
.
sourcepub fn next_sibling_or_token(&self) -> Option<SyntaxElementRef<'_, S, D>>
pub fn next_sibling_or_token(&self) -> Option<SyntaxElementRef<'_, S, D>>
The tree element to the right of this one, i.e. the next child of this node’s parent after this node.
sourcepub fn prev_sibling(&self) -> Option<&SyntaxNode<S, D>>
pub fn prev_sibling(&self) -> Option<&SyntaxNode<S, D>>
The node to the left of this one, i.e. the previous child node (!) of this node’s parent before this node.
If you want to also consider leafs, see prev_sibling_or_token
.
sourcepub fn prev_sibling_or_token(&self) -> Option<SyntaxElementRef<'_, S, D>>
pub fn prev_sibling_or_token(&self) -> Option<SyntaxElementRef<'_, S, D>>
The tree element to the left of this one, i.e. the previous child of this node’s parent before this node.
sourcepub fn first_token(&self) -> Option<&SyntaxToken<S, D>>
pub fn first_token(&self) -> Option<&SyntaxToken<S, D>>
Return the leftmost token in the subtree of this node
sourcepub fn last_token(&self) -> Option<&SyntaxToken<S, D>>
pub fn last_token(&self) -> Option<&SyntaxToken<S, D>>
Return the rightmost token in the subtree of this node
sourcepub fn siblings(
&self,
direction: Direction
) -> impl Iterator<Item = &SyntaxNode<S, D>>
pub fn siblings( &self, direction: Direction ) -> impl Iterator<Item = &SyntaxNode<S, D>>
Returns an iterator over all sibling nodes of this node in the given direction
, i.e. all of
this node’s parent’s child nodes (!) from this node on to the left or the right. The first
item in the iterator will always be this node.
If you want to also consider leafs, see siblings_with_tokens
.
sourcepub fn siblings_with_tokens(
&self,
direction: Direction
) -> impl Iterator<Item = SyntaxElementRef<'_, S, D>>
pub fn siblings_with_tokens( &self, direction: Direction ) -> impl Iterator<Item = SyntaxElementRef<'_, S, D>>
Returns an iterator over all siblings of this node in the given direction
, i.e. all of this
node’s parent’s children from this node on to the left or the right.
The first item in the iterator will always be this node.
sourcepub fn descendants(&self) -> impl Iterator<Item = &SyntaxNode<S, D>>
pub fn descendants(&self) -> impl Iterator<Item = &SyntaxNode<S, D>>
Returns an iterator over all nodes (!) in the subtree starting at this node, including this node.
If you want to also consider leafs, see descendants_with_tokens
.
sourcepub fn descendants_with_tokens(
&self
) -> impl Iterator<Item = SyntaxElementRef<'_, S, D>>
pub fn descendants_with_tokens( &self ) -> impl Iterator<Item = SyntaxElementRef<'_, S, D>>
Returns an iterator over all elements in the subtree starting at this node, including this node.
sourcepub fn preorder(&self) -> impl Iterator<Item = WalkEvent<&SyntaxNode<S, D>>>
pub fn preorder(&self) -> impl Iterator<Item = WalkEvent<&SyntaxNode<S, D>>>
Traverse the subtree rooted at the current node (including the current node) in preorder, excluding tokens.
sourcepub fn preorder_with_tokens(
&self
) -> impl Iterator<Item = WalkEvent<SyntaxElementRef<'_, S, D>>>
pub fn preorder_with_tokens( &self ) -> impl Iterator<Item = WalkEvent<SyntaxElementRef<'_, S, D>>>
Traverse the subtree rooted at the current node (including the current node) in preorder, including tokens.
sourcepub fn token_at_offset(
&self,
offset: TextSize
) -> TokenAtOffset<SyntaxToken<S, D>> ⓘ
pub fn token_at_offset( &self, offset: TextSize ) -> TokenAtOffset<SyntaxToken<S, D>> ⓘ
Find a token in the subtree corresponding to this node, which covers the offset. Precondition: offset must be withing node’s range.
sourcepub fn covering_element(&self, range: TextRange) -> SyntaxElementRef<'_, S, D>
pub fn covering_element(&self, range: TextRange) -> SyntaxElementRef<'_, S, D>
Return the deepest node or token 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
source§impl<S, D> SyntaxNode<S, D>where
S: Syntax,
impl<S, D> SyntaxNode<S, D>where S: Syntax,
sourcepub fn as_serialize_with_data_with_resolver<'node>(
&'node self,
resolver: &'node impl Resolver<TokenKey>
) -> impl Serialize + 'nodewhere
D: Serialize,
pub fn as_serialize_with_data_with_resolver<'node>( &'node self, resolver: &'node impl Resolver<TokenKey> ) -> impl Serialize + 'nodewhere D: Serialize,
Return an anonymous object that can be used to serialize this node, including the data and by using an external resolver.
sourcepub fn as_serialize_with_resolver<'node>(
&'node self,
resolver: &'node impl Resolver<TokenKey>
) -> impl Serialize + 'node
pub fn as_serialize_with_resolver<'node>( &'node self, resolver: &'node impl Resolver<TokenKey> ) -> impl Serialize + 'node
Return an anonymous object that can be used to serialize this node, which uses the given resolver instead of the resolver inside the tree.
Trait Implementations§
source§impl<S: Syntax, D> Clone for SyntaxNode<S, D>
impl<S: Syntax, D> Clone for SyntaxNode<S, D>
source§impl<S: Syntax, D> Drop for SyntaxNode<S, D>
impl<S: Syntax, D> Drop for SyntaxNode<S, D>
source§impl<'a, S: Syntax, D> From<&'a SyntaxNode<S, D>> for SyntaxElementRef<'a, S, D>
impl<'a, S: Syntax, D> From<&'a SyntaxNode<S, D>> for SyntaxElementRef<'a, S, D>
source§fn from(node: &'a SyntaxNode<S, D>) -> Self
fn from(node: &'a SyntaxNode<S, D>) -> Self
source§impl<S: Syntax, D> From<SyntaxNode<S, D>> for SyntaxElement<S, D>
impl<S: Syntax, D> From<SyntaxNode<S, D>> for SyntaxElement<S, D>
source§fn from(node: SyntaxNode<S, D>) -> SyntaxElement<S, D>
fn from(node: SyntaxNode<S, D>) -> SyntaxElement<S, D>
source§impl<S: Syntax, D> Hash for SyntaxNode<S, D>
impl<S: Syntax, D> Hash for SyntaxNode<S, D>
source§impl<S: Syntax, D> PartialEq<SyntaxNode<S, D>> for SyntaxNode<S, D>
impl<S: Syntax, D> PartialEq<SyntaxNode<S, D>> for SyntaxNode<S, D>
source§fn eq(&self, other: &SyntaxNode<S, D>) -> bool
fn eq(&self, other: &SyntaxNode<S, D>) -> bool
self
and other
values to be equal, and is used
by ==
.