Struct biome_rowan::syntax::SyntaxToken
source · pub struct SyntaxToken<L: Language> { /* private fields */ }Implementations§
source§impl<L: Language> SyntaxToken<L>
impl<L: Language> SyntaxToken<L>
sourcepub fn new_detached<Leading, Trailing>(
kind: L::Kind,
text: &str,
leading: Leading,
trailing: Trailing
) -> Selfwhere
Leading: IntoIterator<Item = TriviaPiece>,
Leading::IntoIter: ExactSizeIterator,
Trailing: IntoIterator<Item = TriviaPiece>,
Trailing::IntoIter: ExactSizeIterator,
pub fn new_detached<Leading, Trailing>(
kind: L::Kind,
text: &str,
leading: Leading,
trailing: Trailing
) -> Selfwhere
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
pub fn key(&self) -> SyntaxElementKey
pub fn kind(&self) -> L::Kind
pub fn text_range(&self) -> TextRange
pub fn text_trimmed_range(&self) -> TextRange
sourcepub fn text(&self) -> &str
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());sourcepub fn token_text(&self) -> TokenText
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())
);pub fn token_text_trimmed(&self) -> TokenText
sourcepub fn text_trimmed(&self) -> &str
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());pub fn parent(&self) -> Option<SyntaxNode<L>>
pub fn ancestors(&self) -> impl Iterator<Item = SyntaxNode<L>>
pub fn next_sibling_or_token(&self) -> Option<SyntaxElement<L>>
pub fn prev_sibling_or_token(&self) -> Option<SyntaxElement<L>>
pub fn siblings_with_tokens( &self, direction: Direction ) -> impl Iterator<Item = SyntaxElement<L>>
sourcepub fn next_token(&self) -> Option<SyntaxToken<L>>
pub fn next_token(&self) -> Option<SyntaxToken<L>>
Next token in the tree (i.e, not necessary a sibling).
sourcepub fn prev_token(&self) -> Option<SyntaxToken<L>>
pub fn prev_token(&self) -> Option<SyntaxToken<L>>
Previous token in the tree (i.e, not necessary a sibling).
sourcepub fn with_leading_trivia<'a, I>(&self, trivia: I) -> Self
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
sourcepub fn with_leading_trivia_pieces<I>(&self, trivia: I) -> Self
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
sourcepub fn with_trailing_trivia<'a, I>(&self, trivia: I) -> Self
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
sourcepub fn with_trailing_trivia_pieces<I>(&self, trivia: I) -> Self
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
sourcepub fn prepend_trivia_pieces<I>(&self, trivia: I) -> Self
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\")]"
);sourcepub fn append_trivia_pieces<I>(&self, trivia: I) -> Self
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 \")]"
);sourcepub fn trim_leading_trivia(&self) -> Self
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\")]"
);sourcepub fn trim_trailing_trivia(&self) -> Self
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*/\")]"
);sourcepub fn indentation_trivia_pieces(
&self
) -> impl ExactSizeIterator<Item = SyntaxTriviaPiece<L>>
pub fn indentation_trivia_pieces( &self ) -> impl ExactSizeIterator<Item = SyntaxTriviaPiece<L>>
Return whitespace that juxtapose the token until the first non-whitespace item.
sourcepub fn leading_trivia(&self) -> SyntaxTrivia<L>
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());sourcepub fn trailing_trivia(&self) -> SyntaxTrivia<L>
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());sourcepub fn has_trailing_comments(&self) -> bool
pub fn has_trailing_comments(&self) -> bool
Checks if the current token has trailing comments
sourcepub fn has_leading_comments(&self) -> bool
pub fn has_leading_comments(&self) -> bool
Checks if the current token has leading comments
sourcepub fn has_leading_non_whitespace_trivia(&self) -> bool
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
sourcepub fn has_leading_newline(&self) -> bool
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>
impl<L: Clone + Language> Clone for SyntaxToken<L>
source§fn clone(&self) -> SyntaxToken<L>
fn clone(&self) -> SyntaxToken<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 SyntaxToken<L>
impl<L: Language> Debug for SyntaxToken<L>
source§impl<L: Language> Display for SyntaxToken<L>
impl<L: Language> Display for SyntaxToken<L>
source§impl<L: Language> From<SyntaxToken<L>> for SyntaxElement<L>
impl<L: Language> From<SyntaxToken<L>> for SyntaxElement<L>
source§fn from(token: SyntaxToken<L>) -> SyntaxElement<L>
fn from(token: SyntaxToken<L>) -> SyntaxElement<L>
source§impl<L: PartialEq + Language> PartialEq for SyntaxToken<L>
impl<L: PartialEq + Language> PartialEq for SyntaxToken<L>
source§fn eq(&self, other: &SyntaxToken<L>) -> bool
fn eq(&self, other: &SyntaxToken<L>) -> bool
self and other values to be equal, and is used
by ==.