Struct biome_rowan::syntax::SyntaxNode
source · pub struct SyntaxNode<L: Language> { /* private fields */ }Implementations§
source§impl<L: Language> SyntaxNode<L>
impl<L: Language> SyntaxNode<L>
sourcepub fn new_detached<I>(kind: L::Kind, slots: I) -> SyntaxNode<L>
pub fn new_detached<I>(kind: L::Kind, slots: I) -> SyntaxNode<L>
Create a new detached (root) node from a syntax kind and an iterator of slots
In general this function should not be used directly but through the
type-checked factory function / builders generated from the grammar of
the corresponding language (eg. biome_js_factory::make)
pub fn key(&self) -> SyntaxElementKey
sourcepub fn element_in_slot(&self, slot: u32) -> Option<SyntaxElement<L>>
pub fn element_in_slot(&self, slot: u32) -> Option<SyntaxElement<L>>
pub fn kind(&self) -> L::Kind
sourcepub fn text(&self) -> SyntaxNodeText
pub fn text(&self) -> SyntaxNodeText
Returns the text of all descendants tokens combined, including all trivia.
use biome_rowan::raw_language::{RawLanguage, RawLanguageKind, RawSyntaxTreeBuilder};
use biome_rowan::*;
let mut node = RawSyntaxTreeBuilder::wrap_with_node(RawLanguageKind::ROOT, |builder| {
builder.token_with_trivia(
RawLanguageKind::LET_TOKEN,
"\n\t let \t\t",
&[TriviaPiece::whitespace(3)],
&[TriviaPiece::whitespace(3)],
);
builder.token(RawLanguageKind::STRING_TOKEN, "a");
builder.token_with_trivia(
RawLanguageKind::SEMICOLON_TOKEN,
"; \t\t",
&[TriviaPiece::whitespace(3)],
&[TriviaPiece::whitespace(3)],
);
});
assert_eq!("\n\t let \t\ta; \t\t", node.text());sourcepub fn text_trimmed(&self) -> SyntaxNodeText
pub fn text_trimmed(&self) -> SyntaxNodeText
Returns the text of all descendants tokens combined, excluding the first token leading trivia, and the last token trailing trivia. All other trivia is included.
use biome_rowan::raw_language::{RawLanguage, RawLanguageKind, RawSyntaxTreeBuilder};
use biome_rowan::*;
let mut node = RawSyntaxTreeBuilder::wrap_with_node(RawLanguageKind::ROOT, |builder| {
builder.token_with_trivia(
RawLanguageKind::LET_TOKEN,
"\n\t let \t\t",
&[TriviaPiece::whitespace(3)],
&[TriviaPiece::whitespace(3)],
);
builder.token(RawLanguageKind::STRING_TOKEN, "a");
builder.token_with_trivia(
RawLanguageKind::SEMICOLON_TOKEN,
"; \t\t",
&[],
&[TriviaPiece::whitespace(3)],
);
});
assert_eq!("let \t\ta;", node.text_trimmed());sourcepub fn text_range(&self) -> TextRange
pub fn text_range(&self) -> TextRange
Returns the range corresponding for the text of all descendants tokens combined, including all trivia.
use biome_rowan::raw_language::{RawLanguage, RawLanguageKind, RawSyntaxTreeBuilder};
use biome_rowan::*;
let mut node = RawSyntaxTreeBuilder::wrap_with_node(RawLanguageKind::ROOT, |builder| {
builder.token_with_trivia(
RawLanguageKind::LET_TOKEN,
"\n\t let \t\t",
&[TriviaPiece::whitespace(3)],
&[TriviaPiece::whitespace(3)],
);
builder.token(RawLanguageKind::STRING_TOKEN, "a");
builder.token_with_trivia(
RawLanguageKind::SEMICOLON_TOKEN,
"; \t\t",
&[],
&[TriviaPiece::whitespace(3)],
);
});
let range = node.text_range();
assert_eq!(0u32, u32::from(range.start()));
assert_eq!(14u32, u32::from(range.end()));sourcepub fn text_trimmed_range(&self) -> TextRange
pub fn text_trimmed_range(&self) -> TextRange
Returns the range corresponding for the text of all descendants tokens combined, excluding the first token leading trivia, and the last token trailing trivia. All other trivia is included.
use biome_rowan::raw_language::{RawLanguage, RawLanguageKind, RawSyntaxTreeBuilder};
use biome_rowan::*;
let mut node = RawSyntaxTreeBuilder::wrap_with_node(RawLanguageKind::ROOT, |builder| {
builder.token_with_trivia(
RawLanguageKind::LET_TOKEN,
"\n\t let \t\t",
&[TriviaPiece::whitespace(3)],
&[TriviaPiece::whitespace(3)],
);
builder.token(RawLanguageKind::STRING_TOKEN, "a");
builder.token_with_trivia(
RawLanguageKind::SEMICOLON_TOKEN,
"; \t\t",
&[],
&[TriviaPiece::whitespace(3)],
);
});
let range = node.text_trimmed_range();
assert_eq!(3u32, u32::from(range.start()));
assert_eq!(11u32, u32::from(range.end()));sourcepub fn first_leading_trivia(&self) -> Option<SyntaxTrivia<L>>
pub fn first_leading_trivia(&self) -> Option<SyntaxTrivia<L>>
Returns the leading trivia of the first_token, or None if the node does not have any descendant tokens.
use biome_rowan::raw_language::{RawLanguage, RawLanguageKind, RawSyntaxTreeBuilder};
use biome_rowan::*;
let mut node = RawSyntaxTreeBuilder::wrap_with_node(RawLanguageKind::ROOT, |builder| {
builder.token_with_trivia(
RawLanguageKind::LET_TOKEN,
"\n\t let \t\t",
&[TriviaPiece::whitespace(3)],
&[TriviaPiece::whitespace(3)],
);
builder.token(RawLanguageKind::STRING_TOKEN, "a");
builder.token_with_trivia(
RawLanguageKind::SEMICOLON_TOKEN,
"; \t\t",
&[],
&[TriviaPiece::whitespace(3)],
);
});
let trivia = node.first_leading_trivia();
assert!(trivia.is_some());
assert_eq!("\n\t ", trivia.unwrap().text());
let mut node = RawSyntaxTreeBuilder::wrap_with_node(RawLanguageKind::ROOT, |builder| {});
let trivia = node.first_leading_trivia();
assert!(trivia.is_none());sourcepub fn last_trailing_trivia(&self) -> Option<SyntaxTrivia<L>>
pub fn last_trailing_trivia(&self) -> Option<SyntaxTrivia<L>>
Returns the trailing trivia of the last_token, or None if the node does not have any descendant tokens.
§Examples
use biome_rowan::raw_language::{RawLanguage, RawLanguageKind, RawSyntaxTreeBuilder};
use biome_rowan::*;
let mut node = RawSyntaxTreeBuilder::wrap_with_node(RawLanguageKind::ROOT, |builder| {
builder.token_with_trivia(
RawLanguageKind::LET_TOKEN,
"\n\t let \t\t",
&[TriviaPiece::whitespace(3)],
&[TriviaPiece::whitespace(3)],
);
builder.token(RawLanguageKind::STRING_TOKEN, "a");
builder.token_with_trivia(
RawLanguageKind::SEMICOLON_TOKEN,
"; \t\t",
&[],
&[TriviaPiece::whitespace(3)],
);
});
let trivia = node.last_trailing_trivia();
assert!(trivia.is_some());
assert_eq!(" \t\t", trivia.unwrap().text());
let mut node = RawSyntaxTreeBuilder::wrap_with_node(RawLanguageKind::ROOT, |builder| {});
let trivia = node.last_trailing_trivia();
assert!(trivia.is_none());pub fn parent(&self) -> Option<SyntaxNode<L>>
sourcepub fn grand_parent(&self) -> Option<SyntaxNode<L>>
pub fn grand_parent(&self) -> Option<SyntaxNode<L>>
Returns the grand parent.
pub fn ancestors(&self) -> impl Iterator<Item = SyntaxNode<L>>
pub fn children(&self) -> SyntaxNodeChildren<L> ⓘ
sourcepub fn slots(&self) -> SyntaxSlots<L>
pub fn slots(&self) -> SyntaxSlots<L>
Returns an iterator over all the slots of this syntax node.
pub fn children_with_tokens(&self) -> SyntaxElementChildren<L> ⓘ
pub fn tokens(&self) -> impl DoubleEndedIterator<Item = SyntaxToken<L>> + '_
pub fn first_child(&self) -> Option<SyntaxNode<L>>
pub fn last_child(&self) -> Option<SyntaxNode<L>>
pub fn first_child_or_token(&self) -> Option<SyntaxElement<L>>
pub fn last_child_or_token(&self) -> Option<SyntaxElement<L>>
pub fn next_sibling(&self) -> Option<SyntaxNode<L>>
pub fn prev_sibling(&self) -> Option<SyntaxNode<L>>
pub fn next_sibling_or_token(&self) -> Option<SyntaxElement<L>>
pub fn prev_sibling_or_token(&self) -> Option<SyntaxElement<L>>
sourcepub fn first_token(&self) -> Option<SyntaxToken<L>>
pub fn first_token(&self) -> Option<SyntaxToken<L>>
Return the leftmost token in the subtree of this node.
sourcepub fn last_token(&self) -> Option<SyntaxToken<L>>
pub fn last_token(&self) -> Option<SyntaxToken<L>>
Return the rightmost token in the subtree of this node.
pub fn siblings( &self, direction: Direction ) -> impl Iterator<Item = SyntaxNode<L>>
pub fn siblings_with_tokens( &self, direction: Direction ) -> impl Iterator<Item = SyntaxElement<L>>
pub fn descendants(&self) -> impl Iterator<Item = SyntaxNode<L>>
pub fn descendants_tokens( &self, direction: Direction ) -> impl Iterator<Item = SyntaxToken<L>>
pub fn descendants_with_tokens( &self, direction: Direction ) -> impl Iterator<Item = SyntaxElement<L>>
sourcepub fn preorder(&self) -> Preorder<L> ⓘ
pub fn preorder(&self) -> Preorder<L> ⓘ
Traverse the subtree rooted at the current node (including the current node) in preorder, excluding tokens.
sourcepub fn preorder_with_tokens(
&self,
direction: Direction
) -> PreorderWithTokens<L> ⓘ
pub fn preorder_with_tokens( &self, direction: Direction ) -> PreorderWithTokens<L> ⓘ
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<L>> ⓘ
pub fn token_at_offset(&self, offset: TextSize) -> TokenAtOffset<SyntaxToken<L>> ⓘ
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) -> SyntaxElement<L>
pub fn covering_element(&self, range: TextRange) -> SyntaxElement<L>
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
sourcepub fn child_or_token_at_range(
&self,
range: TextRange
) -> Option<SyntaxElement<L>>
pub fn child_or_token_at_range( &self, range: TextRange ) -> Option<SyntaxElement<L>>
Finds a SyntaxElement which intersects with a given range. If
there are several intersecting elements, any one can be returned.
The method uses binary search internally, so it’s complexity is
O(log(N)) where N = self.children_with_tokens().count().
sourcepub fn clone_subtree(&self) -> SyntaxNode<L>
pub fn clone_subtree(&self) -> SyntaxNode<L>
Returns an independent copy of the subtree rooted at this node.
The parent of the returned node will be None, the start offset will be
zero, but, otherwise, it’ll be equivalent to the source node.
sourcepub fn splice_slots<R, I>(self, range: R, replace_with: I) -> Self
pub fn splice_slots<R, I>(self, range: R, replace_with: I) -> Self
Return a clone of this node with the specified range of slots replaced with the elements of the provided iterator
sourcepub fn replace_child(
self,
prev_elem: SyntaxElement<L>,
next_elem: SyntaxElement<L>
) -> Option<Self>
pub fn replace_child( self, prev_elem: SyntaxElement<L>, next_elem: SyntaxElement<L> ) -> Option<Self>
Return a new version of this node with the element prev_elem replaced with next_elem
prev_elem can be a direct child of this node, or an indirect child through any descendant node
Returns None if prev_elem is not a descendant of this node
sourcepub fn with_leading_trivia_pieces<I>(self, trivia: I) -> Option<Self>
pub fn with_leading_trivia_pieces<I>(self, trivia: I) -> Option<Self>
Return a new version of this node with the leading trivia of its first token replaced with trivia.
sourcepub fn with_trailing_trivia_pieces<I>(self, trivia: I) -> Option<Self>
pub fn with_trailing_trivia_pieces<I>(self, trivia: I) -> Option<Self>
Return a new version of this node with the trailing trivia of its last token replaced with trivia.
sourcepub fn prepend_trivia_pieces<I>(self, trivia: I) -> Option<Self>
pub fn prepend_trivia_pieces<I>(self, trivia: I) -> Option<Self>
§Examples
use biome_rowan::raw_language::{RawLanguage, RawLanguageKind, RawSyntaxTreeBuilder};
use biome_rowan::*;
let mut node = RawSyntaxTreeBuilder::wrap_with_node(RawLanguageKind::ROOT, |builder| {
builder.token_with_trivia(
RawLanguageKind::LET_TOKEN,
"\t let ",
&[TriviaPiece::whitespace(2)],
&[TriviaPiece::whitespace(1)],
);
builder.token(RawLanguageKind::STRING_TOKEN, "a");
builder.token_with_trivia(
RawLanguageKind::SEMICOLON_TOKEN,
"; ",
&[],
&[TriviaPiece::whitespace(1)],
);
});
let new_node = node.clone().prepend_trivia_pieces(node.last_trailing_trivia().unwrap().pieces()).unwrap();
let leading_trivia = new_node.first_leading_trivia().unwrap();
let trailing_trivia = new_node.last_trailing_trivia().unwrap();
assert_eq!(" \t ", leading_trivia.text());
assert_eq!(" ", trailing_trivia.text());sourcepub fn append_trivia_pieces<I>(self, trivia: I) -> Option<Self>
pub fn append_trivia_pieces<I>(self, trivia: I) -> Option<Self>
§Examples
use biome_rowan::raw_language::{RawLanguage, RawLanguageKind, RawSyntaxTreeBuilder};
use biome_rowan::*;
let mut node = RawSyntaxTreeBuilder::wrap_with_node(RawLanguageKind::ROOT, |builder| {
builder.token_with_trivia(
RawLanguageKind::LET_TOKEN,
"\t let ",
&[TriviaPiece::whitespace(2)],
&[TriviaPiece::whitespace(1)],
);
builder.token(RawLanguageKind::STRING_TOKEN, "a");
builder.token_with_trivia(
RawLanguageKind::SEMICOLON_TOKEN,
"; ",
&[],
&[TriviaPiece::whitespace(1)],
);
});
let new_node = node.clone().append_trivia_pieces(node.first_leading_trivia().unwrap().pieces()).unwrap();
let leading_trivia = new_node.first_leading_trivia().unwrap();
let trailing_trivia = new_node.last_trailing_trivia().unwrap();
assert_eq!("\t ", leading_trivia.text());
assert_eq!(" \t ", trailing_trivia.text());sourcepub fn trim_trivia(self) -> Option<Self>
pub fn trim_trivia(self) -> Option<Self>
Return a new version of this node without leading and trailing newlines and whitespaces.
§Examples
use biome_rowan::raw_language::{RawLanguage, RawLanguageKind, RawSyntaxTreeBuilder};
use biome_rowan::*;
let mut node = RawSyntaxTreeBuilder::wrap_with_node(RawLanguageKind::ROOT, |builder| {
builder.token_with_trivia(
RawLanguageKind::LET_TOKEN,
"\n let ",
&[TriviaPiece::newline(1), TriviaPiece::whitespace(1)],
&[TriviaPiece::whitespace(1)],
);
builder.token(RawLanguageKind::STRING_TOKEN, "a");
builder.token_with_trivia(
RawLanguageKind::SEMICOLON_TOKEN,
"; ",
&[],
&[TriviaPiece::whitespace(1)],
);
});
let new_node = node.trim_trivia().unwrap();
let leading_trivia = new_node.first_leading_trivia().unwrap();
let trailing_trivia = new_node.last_trailing_trivia().unwrap();
assert_eq!("", leading_trivia.text());
assert_eq!("", trailing_trivia.text());sourcepub fn trim_leading_trivia(self) -> Option<Self>
pub fn trim_leading_trivia(self) -> Option<Self>
Return a new version of this node without leading newlines and whitespaces.
§Examples
use biome_rowan::raw_language::{RawLanguage, RawLanguageKind, RawSyntaxTreeBuilder};
use biome_rowan::*;
let mut node = RawSyntaxTreeBuilder::wrap_with_node(RawLanguageKind::ROOT, |builder| {
builder.token_with_trivia(
RawLanguageKind::LET_TOKEN,
"\n let ",
&[TriviaPiece::newline(1), TriviaPiece::whitespace(1)],
&[TriviaPiece::whitespace(1)],
);
builder.token(RawLanguageKind::STRING_TOKEN, "a");
builder.token_with_trivia(
RawLanguageKind::SEMICOLON_TOKEN,
"; ",
&[],
&[TriviaPiece::whitespace(1)],
);
});
let new_node = node.trim_leading_trivia().unwrap();
let leading_trivia = new_node.first_leading_trivia().unwrap();
let trailing_trivia = new_node.last_trailing_trivia().unwrap();
assert_eq!("", leading_trivia.text());
assert_eq!(" ", trailing_trivia.text());sourcepub fn trim_trailing_trivia(self) -> Option<Self>
pub fn trim_trailing_trivia(self) -> Option<Self>
Return a new version of this token without trailing whitespaces.
§Examples
use biome_rowan::raw_language::{RawLanguage, RawLanguageKind, RawSyntaxTreeBuilder};
use biome_rowan::*;
let mut node = RawSyntaxTreeBuilder::wrap_with_node(RawLanguageKind::ROOT, |builder| {
builder.token_with_trivia(
RawLanguageKind::LET_TOKEN,
"\n let ",
&[TriviaPiece::newline(1), TriviaPiece::whitespace(1)],
&[TriviaPiece::whitespace(1)],
);
builder.token(RawLanguageKind::STRING_TOKEN, "a");
builder.token_with_trivia(
RawLanguageKind::SEMICOLON_TOKEN,
"; ",
&[],
&[TriviaPiece::whitespace(1)],
);
});
let new_node = node.trim_trailing_trivia().unwrap();
let leading_trivia = new_node.first_leading_trivia().unwrap();
let trailing_trivia = new_node.last_trailing_trivia().unwrap();
assert_eq!("\n ", leading_trivia.text());
assert_eq!("", trailing_trivia.text());pub fn into_list(self) -> SyntaxList<L>
sourcepub fn has_comments_descendants(&self) -> bool
pub fn has_comments_descendants(&self) -> bool
Whether the node contains any comments. This function checks all the descendants of the current node.
sourcepub fn has_comments_direct(&self) -> bool
pub fn has_comments_direct(&self) -> bool
It checks if the current node has trailing or leading trivia
sourcepub fn first_or_last_token_have_comments(&self) -> bool
pub fn first_or_last_token_have_comments(&self) -> bool
It checks if the current node has comments at the edges: if first or last tokens contain comments (leading or trailing)
sourcepub fn has_trailing_comments(&self) -> bool
pub fn has_trailing_comments(&self) -> bool
Whether the node contains trailing comments.
sourcepub fn last_token_has_comments(&self) -> bool
pub fn last_token_has_comments(&self) -> bool
Whether the last token of a node has comments (leading or trailing)
sourcepub fn first_token_has_comments(&self) -> bool
pub fn first_token_has_comments(&self) -> bool
Whether the first token of a node has comments (leading or trailing)
sourcepub fn has_leading_comments(&self) -> bool
pub fn has_leading_comments(&self) -> bool
Whether the node contains leading comments.
sourcepub fn has_leading_newline(&self) -> bool
pub fn has_leading_newline(&self) -> bool
Whether the node contains leading newlines.
Trait Implementations§
source§impl<L: Clone + Language> Clone for SyntaxNode<L>
impl<L: Clone + Language> Clone for SyntaxNode<L>
source§fn clone(&self) -> SyntaxNode<L>
fn clone(&self) -> SyntaxNode<L>
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moresource§impl<L: Language> Debug for SyntaxNode<L>
impl<L: Language> Debug for SyntaxNode<L>
source§impl<L: Language> Display for SyntaxNode<L>
impl<L: Language> Display for SyntaxNode<L>
source§impl<L: Language> From<SyntaxNode<L>> for SyntaxElement<L>
impl<L: Language> From<SyntaxNode<L>> for SyntaxElement<L>
source§fn from(node: SyntaxNode<L>) -> SyntaxElement<L>
fn from(node: SyntaxNode<L>) -> SyntaxElement<L>
source§impl<L: PartialEq + Language> PartialEq for SyntaxNode<L>
impl<L: PartialEq + Language> PartialEq for SyntaxNode<L>
source§fn eq(&self, other: &SyntaxNode<L>) -> bool
fn eq(&self, other: &SyntaxNode<L>) -> bool
self and other values to be equal, and is used
by ==.