+
Skip to content
This repository was archived by the owner on Aug 31, 2023. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 26 additions & 27 deletions crates/rome_rowan/src/syntax/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
use std::fmt;
use std::marker::PhantomData;
use std::ptr::NonNull;
use text_size::{TextRange, TextSize};
use text_size::{TextLen, TextRange, TextSize};

#[derive(Clone, PartialEq, Eq, Hash)]
pub struct SyntaxToken<L: Language> {
Expand Down Expand Up @@ -191,26 +191,25 @@ impl<L: Language> SyntaxToken<L> {
#[must_use = "syntax elements are immutable, the result of update methods must be propagated to have any effect"]
pub fn with_leading_trivia<'a, I>(self, trivia: I) -> Self
where
I: Iterator<Item = (TriviaPieceKind, &'a str)> + Clone + ExactSizeIterator,
I: Iterator<Item = (TriviaPieceKind, &'a str)> + ExactSizeIterator,
{
let mut text = String::new();
for (_, piece) in trivia.clone() {
text.push_str(piece);
}
let mut token_text = String::new();
let trivia = trivia.map(|(kind, text)| {
token_text.push_str(text);
TriviaPiece::new(kind, TextSize::of(text))
});

text.push_str(self.text_trimmed());
let leading = GreenTrivia::new(trivia);

for piece in self.trailing_trivia().pieces() {
text.push_str(piece.text());
}
// Copy over token text and trailing trivia
let leading_len = self.raw.green().leading_trivia().text_len();
token_text.push_str(&self.text()[usize::from(leading_len)..]);

Self {
raw: cursor::SyntaxToken::new_detached(GreenToken::with_trivia(
self.kind().to_raw(),
&text,
GreenTrivia::new(
trivia.map(|(kind, text)| TriviaPiece::new(kind, TextSize::of(text))),
),
&token_text,
leading,
self.green_token().trailing_trivia().clone(),
)),
_p: PhantomData,
Expand All @@ -221,27 +220,27 @@ impl<L: Language> SyntaxToken<L> {
#[must_use = "syntax elements are immutable, the result of update methods must be propagated to have any effect"]
pub fn with_trailing_trivia<'a, I>(self, trivia: I) -> Self
where
I: Iterator<Item = (TriviaPieceKind, &'a str)> + Clone + ExactSizeIterator,
I: Iterator<Item = (TriviaPieceKind, &'a str)> + ExactSizeIterator,
{
let mut text = String::new();
for piece in self.leading_trivia().pieces() {
text.push_str(piece.text());
}
let mut token_text = String::new();

text.push_str(self.text_trimmed());
// copy over leading trivia and token text
let trailing_len = self.green_token().trailing_trivia().text_len();
token_text.push_str(&self.text()[..usize::from(self.text().text_len() - trailing_len)]);

for (_, piece) in trivia.clone() {
text.push_str(piece);
}
let trivia = trivia.map(|(kind, text)| {
token_text.push_str(text);
TriviaPiece::new(kind, TextSize::of(text))
});

let trailing = GreenTrivia::new(trivia);

Self {
raw: cursor::SyntaxToken::new_detached(GreenToken::with_trivia(
self.kind().to_raw(),
&text,
&token_text,
self.green_token().leading_trivia().clone(),
GreenTrivia::new(
trivia.map(|(kind, text)| TriviaPiece::new(kind, TextSize::of(text))),
),
trailing,
)),
_p: PhantomData,
}
Expand Down
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载