SyntaxTriviaPiece

Struct SyntaxTriviaPiece 

Source
pub struct SyntaxTriviaPiece<L: Language> { /* private fields */ }
Expand description

SyntaxTriviaPiece gives access to the most granular information about the trivia that was specified by the lexer at the token creation time.

For example:

builder.token_with_trivia(
    RawSyntaxKind(1),
    "\n\t /**/let \t\t",
    &[TriviaPiece::whitespace(3), TriviaPiece::single_line_comment(4)],
    &[TriviaPiece::whitespace(3)],
);
});

This token has two pieces in the leading trivia, and one piece at the trailing trivia. Each piece is defined by the TriviaPiece; its content is irrelevant.

Implementations§

Source§

impl<L: Language> SyntaxTriviaPiece<L>

Source

pub fn kind(&self) -> TriviaPieceKind

Returns the internal kind of this trivia piece

Source

pub fn text(&self) -> &str

Returns the associated text just for this trivia piece. This is different from SyntaxTrivia::text(), which returns the text of the whole trivia.

use biome_rowan::raw_language::{RawLanguage, RawLanguageKind, RawSyntaxTreeBuilder};
use biome_rowan::*;
use std::iter::Iterator;
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::single_line_comment(4),
        ],
        &[TriviaPiece::whitespace(3)],
    );
});
let leading: Vec<_> = node.first_leading_trivia().unwrap().pieces().collect();
assert_eq!("\n\t ", leading[0].text());
assert_eq!("/**/", leading[1].text());

let trailing: Vec<_> = node.last_trailing_trivia().unwrap().pieces().collect();
assert_eq!(" \t\t", trailing[0].text());
Source

pub fn text_len(&self) -> TextSize

Returns the associated text length just for this trivia piece. This is different from SyntaxTrivia::len(), which returns the text length of the whole trivia.

use biome_rowan::raw_language::{RawLanguage, RawLanguageKind, RawSyntaxTreeBuilder};
use biome_rowan::*;
use std::iter::Iterator;
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::single_line_comment(4),
        ],
        &[TriviaPiece::whitespace(3)],
    );
});
let pieces: Vec<_> = node.first_leading_trivia().unwrap().pieces().collect();
assert_eq!(TextSize::from(3), pieces[0].text_len());
Source

pub fn text_range(&self) -> TextRange

Returns the associated text range just for this trivia piece. This is different from SyntaxTrivia::text_range(), which returns the text range of the whole trivia.

use biome_rowan::raw_language::{RawLanguage, RawLanguageKind, RawSyntaxTreeBuilder};
use biome_rowan::*;
use std::iter::Iterator;
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::single_line_comment(4),
        ],
        &[TriviaPiece::whitespace(3)],
    );
});
let pieces: Vec<_> = node.first_leading_trivia().unwrap().pieces().collect();
assert_eq!(TextRange::new(0.into(), 3.into()), pieces[0].text_range());
Source

pub fn is_newline(&self) -> bool

Returns true if this trivia piece is a SyntaxTriviaPieceNewline.

use biome_rowan::raw_language::{RawLanguage, RawLanguageKind, RawSyntaxTreeBuilder};
use biome_rowan::*;
use std::iter::Iterator;
let mut node = RawSyntaxTreeBuilder::wrap_with_node(RawLanguageKind::ROOT, |builder| {
    builder.token_with_trivia(
        RawLanguageKind::LET_TOKEN,
        "\n\t/**/let",
        &[
            TriviaPiece::newline(1),
            TriviaPiece::whitespace(1),
            TriviaPiece::single_line_comment(4),
        ],
        &[],
    );
});
let pieces: Vec<_> = node.first_leading_trivia().unwrap().pieces().collect();
assert!(pieces[0].is_newline())
Source

pub fn is_whitespace(&self) -> bool

Returns true if this trivia piece is a SyntaxTriviaPieceWhitespace.

use biome_rowan::raw_language::{RawLanguage, RawLanguageKind, RawSyntaxTreeBuilder};
use biome_rowan::*;
use std::iter::Iterator;
let mut node = RawSyntaxTreeBuilder::wrap_with_node(RawLanguageKind::ROOT, |builder| {
    builder.token_with_trivia(
        RawLanguageKind::LET_TOKEN,
        "\n\t/**/let",
        &[
            TriviaPiece::newline(1),
            TriviaPiece::whitespace(1),
            TriviaPiece::single_line_comment(4),
        ],
        &[],
    );
});
let pieces: Vec<_> = node.first_leading_trivia().unwrap().pieces().collect();
assert!(pieces[1].is_whitespace())
Source

pub const fn is_comments(&self) -> bool

Returns true if this trivia piece is a SyntaxTriviaPieceComments.

use biome_rowan::raw_language::{RawLanguage, RawLanguageKind, RawSyntaxTreeBuilder};
use biome_rowan::*;
use std::iter::Iterator;
let mut node = RawSyntaxTreeBuilder::wrap_with_node(RawLanguageKind::ROOT, |builder| {
    builder.token_with_trivia(
        RawLanguageKind::LET_TOKEN,
        "\n\t/**/let",
        &[
            TriviaPiece::newline(1),
            TriviaPiece::whitespace(1),
            TriviaPiece::single_line_comment(4),
        ],
        &[],
    );
});
let pieces: Vec<_> = node.first_leading_trivia().unwrap().pieces().collect();
assert!(pieces[2].is_comments())
Source

pub fn is_skipped(&self) -> bool

Returns true if this trivia piece is a SyntaxTriviaPieceSkipped.

Source

pub fn as_newline(&self) -> Option<SyntaxTriviaPieceNewline<L>>

Cast this trivia piece to SyntaxTriviaPieceNewline.

use biome_rowan::raw_language::{RawLanguage, RawLanguageKind, RawSyntaxTreeBuilder};
use biome_rowan::*;
use std::iter::Iterator;
let mut node = RawSyntaxTreeBuilder::wrap_with_node(RawLanguageKind::ROOT, |builder| {
    builder.token_with_trivia(
        RawLanguageKind::LET_TOKEN,
        "\n/**/let \t\t",
        &[TriviaPiece::newline(1), TriviaPiece::single_line_comment(4)],
        &[TriviaPiece::newline(3)],
    );
});
let pieces: Vec<_> = node.first_leading_trivia().unwrap().pieces().collect();
let w = pieces[0].as_newline();
assert!(w.is_some());
let w = pieces[1].as_newline();
assert!(w.is_none());
Source

pub fn as_whitespace(&self) -> Option<SyntaxTriviaPieceWhitespace<L>>

Cast this trivia piece to SyntaxTriviaPieceWhitespace.

use biome_rowan::raw_language::{RawLanguage, RawLanguageKind, RawSyntaxTreeBuilder};
use biome_rowan::*;
use std::iter::Iterator;
let mut node = RawSyntaxTreeBuilder::wrap_with_node(RawLanguageKind::ROOT, |builder| {
    builder.token_with_trivia(
        RawLanguageKind::LET_TOKEN,
        "\t /**/let \t\t",
        &[
            TriviaPiece::whitespace(2),
            TriviaPiece::single_line_comment(4),
        ],
        &[TriviaPiece::whitespace(3)],
    );
});
let pieces: Vec<_> = node.first_leading_trivia().unwrap().pieces().collect();
let w = pieces[0].as_whitespace();
assert!(w.is_some());
let w = pieces[1].as_whitespace();
assert!(w.is_none());
Source

pub fn as_comments(&self) -> Option<SyntaxTriviaPieceComments<L>>

Cast this trivia piece to SyntaxTriviaPieceComments.

use biome_rowan::raw_language::{RawLanguage, RawLanguageKind, RawSyntaxTreeBuilder};
use biome_rowan::*;
use std::iter::Iterator;
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::single_line_comment(4),
        ],
        &[TriviaPiece::whitespace(3)],
    );
});
let pieces: Vec<_> = node.first_leading_trivia().unwrap().pieces().collect();
let w = pieces[0].as_comments();
assert!(w.is_none());
let w = pieces[1].as_comments();
assert!(w.is_some());
Source

pub fn as_skipped(&self) -> Option<SyntaxTriviaPieceSkipped<L>>

Casts this piece to a skipped trivia piece.

Source

pub fn token(&self) -> SyntaxToken<L>

Trait Implementations§

Source§

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

Source§

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

Returns a duplicate 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 SyntaxTriviaPiece<L>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<L> Freeze for SyntaxTriviaPiece<L>

§

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

§

impl<L> !Send for SyntaxTriviaPiece<L>

§

impl<L> !Sync for SyntaxTriviaPiece<L>

§

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

§

impl<L> UnwindSafe for SyntaxTriviaPiece<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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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,

Source§

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, U> TryFrom<U> for T
where U: Into<T>,

Source§

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>,

Source§

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