pub struct SyntaxNode<L: Language> { /* private fields */ }

Implementations§

source§

impl<L: Language> SyntaxNode<L>

source

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)

source

pub fn key(&self) -> SyntaxElementKey

source

pub fn element_in_slot(&self, slot: u32) -> Option<SyntaxElement<L>>

Returns the element stored in the slot with the given index. Returns None if the slot is empty.

§Panics

If the slot index is out of bounds

source

pub fn kind(&self) -> L::Kind

source

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());
source

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());
source

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()));
source

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()));
source

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());
source

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());
source

pub fn parent(&self) -> Option<SyntaxNode<L>>

source

pub fn grand_parent(&self) -> Option<SyntaxNode<L>>

Returns the grand parent.

source

pub fn index(&self) -> usize

Returns the index of this node inside of its parent

source

pub fn ancestors(&self) -> impl Iterator<Item = SyntaxNode<L>>

source

pub fn children(&self) -> SyntaxNodeChildren<L>

source

pub fn slots(&self) -> SyntaxSlots<L>

Returns an iterator over all the slots of this syntax node.

source

pub fn children_with_tokens(&self) -> SyntaxElementChildren<L>

source

pub fn tokens(&self) -> impl DoubleEndedIterator<Item = SyntaxToken<L>> + '_

source

pub fn first_child(&self) -> Option<SyntaxNode<L>>

source

pub fn last_child(&self) -> Option<SyntaxNode<L>>

source

pub fn first_child_or_token(&self) -> Option<SyntaxElement<L>>

source

pub fn last_child_or_token(&self) -> Option<SyntaxElement<L>>

source

pub fn next_sibling(&self) -> Option<SyntaxNode<L>>

source

pub fn prev_sibling(&self) -> Option<SyntaxNode<L>>

source

pub fn next_sibling_or_token(&self) -> Option<SyntaxElement<L>>

source

pub fn prev_sibling_or_token(&self) -> Option<SyntaxElement<L>>

source

pub fn first_token(&self) -> Option<SyntaxToken<L>>

Return the leftmost token in the subtree of this node.

source

pub fn last_token(&self) -> Option<SyntaxToken<L>>

Return the rightmost token in the subtree of this node.

source

pub fn siblings( &self, direction: Direction ) -> impl Iterator<Item = SyntaxNode<L>>

source

pub fn siblings_with_tokens( &self, direction: Direction ) -> impl Iterator<Item = SyntaxElement<L>>

source

pub fn descendants(&self) -> impl Iterator<Item = SyntaxNode<L>>

source

pub fn descendants_tokens( &self, direction: Direction ) -> impl Iterator<Item = SyntaxToken<L>>

source

pub fn descendants_with_tokens( &self, direction: Direction ) -> impl Iterator<Item = SyntaxElement<L>>

source

pub fn preorder(&self) -> Preorder<L>

Traverse the subtree rooted at the current node (including the current node) in preorder, excluding tokens.

source

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.

source

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.

source

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

source

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().

source

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.

source

pub fn detach(self) -> Self

Return a new version of this node detached from its parent node

source

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

source

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

source

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.

source

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.

source

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());
source

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());
source

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());
source

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());
source

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());
source

pub fn into_list(self) -> SyntaxList<L>

source

pub fn has_comments_descendants(&self) -> bool

Whether the node contains any comments. This function checks all the descendants of the current node.

source

pub fn has_comments_direct(&self) -> bool

It checks if the current node has trailing or leading trivia

source

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)

source

pub fn has_trailing_comments(&self) -> bool

Whether the node contains trailing comments.

source

pub fn last_token_has_comments(&self) -> bool

Whether the last token of a node has comments (leading or trailing)

source

pub fn first_token_has_comments(&self) -> bool

Whether the first token of a node has comments (leading or trailing)

source

pub fn has_leading_comments(&self) -> bool

Whether the node contains leading comments.

source

pub fn has_leading_newline(&self) -> bool

Whether the node contains leading newlines.

source§

impl<L> SyntaxNode<L>
where L: Language + 'static,

source

pub fn as_send(&self) -> Option<SendNode>

Create a Send + Sync handle to this node

Returns None if self is not a root node

Trait Implementations§

source§

impl<L: Clone + Language> Clone for SyntaxNode<L>

source§

fn clone(&self) -> SyntaxNode<L>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<L: Language> Debug for SyntaxNode<L>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<L: Language> Display for SyntaxNode<L>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<L: Language> From<SyntaxNode<L>> for SyntaxElement<L>

source§

fn from(node: SyntaxNode<L>) -> SyntaxElement<L>

Converts to this type from the input type.
source§

impl<L: Hash + Language> Hash for SyntaxNode<L>

source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl<L: PartialEq + Language> PartialEq for SyntaxNode<L>

source§

fn eq(&self, other: &SyntaxNode<L>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<L: Language> SyntaxNodeCast<L> for SyntaxNode<L>

source§

fn cast<T: AstNode<Language = L>>(self) -> Option<T>

Tries to cast the current syntax node to specified AST node. Read more
source§

impl<L: Eq + Language> Eq for SyntaxNode<L>

source§

impl<L: Language> StructuralPartialEq for SyntaxNode<L>

Auto Trait Implementations§

§

impl<L> Freeze for SyntaxNode<L>

§

impl<L> RefUnwindSafe for SyntaxNode<L>
where L: RefUnwindSafe,

§

impl<L> !Send for SyntaxNode<L>

§

impl<L> !Sync for SyntaxNode<L>

§

impl<L> Unpin for SyntaxNode<L>
where L: Unpin,

§

impl<L> UnwindSafe for SyntaxNode<L>
where L: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more