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>
impl<L: Language> SyntaxTriviaPiece<L>
Sourcepub fn kind(&self) -> TriviaPieceKind
pub fn kind(&self) -> TriviaPieceKind
Returns the internal kind of this trivia piece
Sourcepub fn text(&self) -> &str
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());Sourcepub fn text_len(&self) -> TextSize
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());Sourcepub fn text_range(&self) -> TextRange
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());Sourcepub fn is_newline(&self) -> bool
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())Sourcepub fn is_whitespace(&self) -> bool
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())Sourcepub const fn is_comments(&self) -> bool
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())Sourcepub fn is_skipped(&self) -> bool
pub fn is_skipped(&self) -> bool
Returns true if this trivia piece is a SyntaxTriviaPieceSkipped.
Sourcepub fn as_newline(&self) -> Option<SyntaxTriviaPieceNewline<L>>
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());Sourcepub fn as_whitespace(&self) -> Option<SyntaxTriviaPieceWhitespace<L>>
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());Sourcepub fn as_comments(&self) -> Option<SyntaxTriviaPieceComments<L>>
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());Sourcepub fn as_skipped(&self) -> Option<SyntaxTriviaPieceSkipped<L>>
pub fn as_skipped(&self) -> Option<SyntaxTriviaPieceSkipped<L>>
Casts this piece to a skipped trivia piece.
pub fn token(&self) -> SyntaxToken<L>
Trait Implementations§
Source§impl<L: Clone + Language> Clone for SyntaxTriviaPiece<L>
impl<L: Clone + Language> Clone for SyntaxTriviaPiece<L>
Source§fn clone(&self) -> SyntaxTriviaPiece<L>
fn clone(&self) -> SyntaxTriviaPiece<L>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more