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

Implementations§

source§

impl<L: Language> SyntaxToken<L>

source

pub fn new_detached<Leading, Trailing>( kind: L::Kind, text: &str, leading: Leading, trailing: Trailing ) -> Self
where Leading: IntoIterator<Item = TriviaPiece>, Leading::IntoIter: ExactSizeIterator, Trailing: IntoIterator<Item = TriviaPiece>, Trailing::IntoIter: ExactSizeIterator,

Create a new token detached from any tree

This is mainly useful for creating a small number of individual tokens when mutating an existing tree, the bulk of the tokens in a given file should be created through the crate::TreeBuilder abstraction instead as it will efficiently cache and reuse the created tokens

source

pub fn key(&self) -> SyntaxElementKey

source

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

source

pub fn text_range(&self) -> TextRange

source

pub fn text_trimmed_range(&self) -> TextRange

source

pub fn text(&self) -> &str

Returns the text of the token, including all trivia.

use biome_rowan::raw_language::{RawLanguage, RawLanguageKind, RawSyntaxTreeBuilder};
use biome_rowan::*;
let mut token = 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)],
    );
})
.first_token()
.unwrap();
assert_eq!("\n\t let \t\t", token.text());
source

pub fn token_text(&self) -> TokenText

Returns the text of a token, including all trivia as an owned value.

use biome_rowan::raw_language::{RawLanguage, RawLanguageKind, RawSyntaxTreeBuilder};
use biome_rowan::*;
let mut token = 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)],
    );
})
.first_token()
.unwrap();
assert_eq!("\n\t let \t\t", token.token_text());
assert_eq!(
    format!("{}", "\n\t let \t\t"),
    format!("{}", token.token_text())
);
assert_eq!(
    format!("{:?}", "\n\t let \t\t"),
    format!("{:?}", token.token_text())
);
source

pub fn token_text_trimmed(&self) -> TokenText

source

pub fn text_trimmed(&self) -> &str

Returns the text of the token, excluding all trivia.

use biome_rowan::raw_language::{RawLanguage, RawLanguageKind, RawSyntaxTreeBuilder};
use biome_rowan::*;
let mut token = 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)],
    );
})
.first_token()
.unwrap();
assert_eq!("let", token.text_trimmed());
source

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

source

pub fn ancestors(&self) -> impl Iterator<Item = 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 siblings_with_tokens( &self, direction: Direction ) -> impl Iterator<Item = SyntaxElement<L>>

source

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

Next token in the tree (i.e, not necessary a sibling).

source

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

Previous token in the tree (i.e, not necessary a sibling).

source

pub fn detach(self) -> Self

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

source

pub fn with_leading_trivia<'a, I>(&self, trivia: I) -> Self

Return a new version of this token with its leading trivia replaced with trivia

source

pub fn with_leading_trivia_pieces<I>(&self, trivia: I) -> Self

Return a new version of this token with its leading trivia replaced with trivia

source

pub fn with_trailing_trivia<'a, I>(&self, trivia: I) -> Self

Return a new version of this token with its trailing trivia replaced with trivia

source

pub fn with_trailing_trivia_pieces<I>(&self, trivia: I) -> Self

Return a new version of this token with its trailing trivia replaced with trivia

source

pub fn prepend_trivia_pieces<I>(&self, trivia: I) -> Self

§Examples
use biome_rowan::raw_language::{RawLanguage, RawLanguageKind};
use biome_rowan::{RawSyntaxToken, SyntaxToken, TriviaPiece};

let token = SyntaxToken::<RawLanguage>::new_detached(
    RawLanguageKind::LET_TOKEN,
    "/*c*/ let \t",
    [TriviaPiece::multi_line_comment(5), TriviaPiece::whitespace(1)],
    [TriviaPiece::whitespace(2)]
);
let new_token = token.prepend_trivia_pieces(token.trailing_trivia().pieces());

assert_eq!(
    format!("{:?}", new_token),
    "LET_TOKEN@0..13 \"let\" [Whitespace(\" \\t\"), Comments(\"/*c*/\"), Whitespace(\" \")] [Whitespace(\" \\t\")]"
);
source

pub fn append_trivia_pieces<I>(&self, trivia: I) -> Self

§Examples
use biome_rowan::raw_language::{RawLanguage, RawLanguageKind};
use biome_rowan::{RawSyntaxToken, SyntaxToken, TriviaPiece};

let token = SyntaxToken::<RawLanguage>::new_detached(
    RawLanguageKind::LET_TOKEN,
    "\t let /*c*/",
    [TriviaPiece::whitespace(2)],
    [TriviaPiece::whitespace(1), TriviaPiece::multi_line_comment(5)],
);
let new_token = token.append_trivia_pieces(token.leading_trivia().pieces());

assert_eq!(
    format!("{:?}", new_token),
    "LET_TOKEN@0..13 \"let\" [Whitespace(\"\\t \")] [Whitespace(\" \"), Comments(\"/*c*/\"), Whitespace(\"\\t \")]"
);
source

pub fn trim_leading_trivia(&self) -> Self

Return a new version of this token without leading newlines and whitespaces.

§Examples
use biome_rowan::raw_language::{RawLanguage, RawLanguageKind};
use biome_rowan::{RawSyntaxToken, SyntaxToken, TriviaPiece};

let token = SyntaxToken::<RawLanguage>::new_detached(
    RawLanguageKind::LET_TOKEN,
    "\n\t /*c*/ let \t",
    [TriviaPiece::newline(1), TriviaPiece::whitespace(2), TriviaPiece::multi_line_comment(5), TriviaPiece::whitespace(1)],
    [TriviaPiece::whitespace(2)]
);
let new_token = token.trim_leading_trivia();

assert_eq!(
    format!("{:?}", new_token),
    "LET_TOKEN@0..11 \"let\" [Comments(\"/*c*/\"), Whitespace(\" \")] [Whitespace(\" \\t\")]"
);
source

pub fn trim_trailing_trivia(&self) -> Self

Return a new version of this token without trailing whitespaces.

§Examples
use biome_rowan::raw_language::{RawLanguage, RawLanguageKind};
use biome_rowan::{RawSyntaxToken, SyntaxToken, TriviaPiece};

let token = SyntaxToken::<RawLanguage>::new_detached(
    RawLanguageKind::LET_TOKEN,
    "\n let /*c*/\t ",
    [TriviaPiece::newline(1), TriviaPiece::whitespace(1)],
    [TriviaPiece::whitespace(1), TriviaPiece::multi_line_comment(5), TriviaPiece::whitespace(2)]
);
let new_token = token.trim_trailing_trivia();

assert_eq!(
    format!("{:?}", new_token),
    "LET_TOKEN@0..11 \"let\" [Newline(\"\\n\"), Whitespace(\" \")] [Whitespace(\" \"), Comments(\"/*c*/\")]"
);
source

pub fn indentation_trivia_pieces( &self ) -> impl ExactSizeIterator<Item = SyntaxTriviaPiece<L>>

Return whitespace that juxtapose the token until the first non-whitespace item.

source

pub fn leading_trivia(&self) -> SyntaxTrivia<L>

Returns the token’s leading trivia.

Looking backward in the text, a token owns all of its preceding trivia up to and including the first newline character.

use biome_rowan::raw_language::{RawLanguage, RawLanguageKind, RawSyntaxTreeBuilder};
use biome_rowan::*;
let mut token = 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)],
    );
})
.first_token()
.unwrap();
assert_eq!("\n\t ", token.leading_trivia().text());
source

pub fn trailing_trivia(&self) -> SyntaxTrivia<L>

Returns the token’s trailing trivia.

A token owns all of its following trivia up to, but not including, the next newline character.

use biome_rowan::raw_language::{RawLanguage, RawLanguageKind, RawSyntaxTreeBuilder};
use biome_rowan::*;
let mut token = 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)],
    );
})
.first_token()
.unwrap();
assert_eq!(" \t\t", token.trailing_trivia().text());
source

pub fn has_trailing_comments(&self) -> bool

Checks if the current token has trailing comments

source

pub fn has_leading_comments(&self) -> bool

Checks if the current token has leading comments

source

pub fn has_leading_non_whitespace_trivia(&self) -> bool

Checks if the token has any leading trivia that isn’t a whitespace nor a line break

source

pub fn has_leading_newline(&self) -> bool

Checks if the current token has leading newline

Trait Implementations§

source§

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

source§

fn clone(&self) -> SyntaxToken<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 SyntaxToken<L>

source§

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

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

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

source§

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

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

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

source§

fn from(token: SyntaxToken<L>) -> SyntaxElement<L>

Converts to this type from the input type.
source§

impl<L: Hash + Language> Hash for SyntaxToken<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 SyntaxToken<L>

source§

fn eq(&self, other: &SyntaxToken<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: Eq + Language> Eq for SyntaxToken<L>

source§

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

Auto Trait Implementations§

§

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

§

impl<L> !Send for SyntaxToken<L>

§

impl<L> !Sync for SyntaxToken<L>

§

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

§

impl<L> UnwindSafe for SyntaxToken<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