Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

Commit

Permalink
Code review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaReiser committed Apr 19, 2022
1 parent e10d648 commit 245c0ea
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
20 changes: 10 additions & 10 deletions crates/rome_formatter/src/format_element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1127,7 +1127,8 @@ pub enum Token {
// The position of the dynamic token in the unformatted source code
source_position: TextSize,
},
// A token that is taken 1:1 from the source code
/// A token for a text that is taken as is from the source code (input text and formatted representation are identical).
/// Implementing by taking a slice from a `SyntaxToken` to avoid allocating a new string.
SyntaxTokenSlice {
/// The start position of the token in the unformatted source code
source_position: TextSize,
Expand Down Expand Up @@ -1160,7 +1161,8 @@ impl Token {

/// Create a token from a dynamic string and a range of the input source
pub fn new_dynamic(text: String, position: TextSize) -> Self {
Self::assert_no_newlines(&text);
debug_assert_no_newlines(&text);

Self::Dynamic {
text: text.into_boxed_str(),
source_position: position,
Expand All @@ -1179,16 +1181,14 @@ impl Token {
token: &SyntaxToken<L>,
start: TextSize,
) -> Self {
Self::assert_no_newlines(&text);

match text {
Cow::Owned(text) => Self::new_dynamic(text, start),
Cow::Borrowed(text) => {
let range = TextRange::at(start, text.text_len());
debug_assert_eq!(
text,
&token.text()[range - token.text_range().start()],
"The borrowed string doesn't match the specified token substring"
"The borrowed string doesn't match the specified token substring. Does the borrowed string belong to this token and range?"
);
Token::new_syntax_token_slice(token, range)
}
Expand All @@ -1200,18 +1200,14 @@ impl Token {
let relative_range = range - token.text_range().start();
let slice = token.token_text().slice(relative_range);

Self::assert_no_newlines(&slice);
debug_assert_no_newlines(&slice);

Self::SyntaxTokenSlice {
slice,
source_position: range.start(),
}
}

fn assert_no_newlines(text: &str) {
debug_assert!(!text.contains('\r'), "The content '{}' contains an unsupported '\\r' line terminator character but string tokens must only use line feeds '\\n' as line separator. Use '\\n' instead of '\\r' and '\\r\\n' to insert a line break in strings.", text);
}

/// Get the range of the input source covered by this token,
/// or None if the token was synthesized by the formatter
pub fn source_position(&self) -> Option<&TextSize> {
Expand All @@ -1227,6 +1223,10 @@ impl Token {
}
}

fn debug_assert_no_newlines(text: &str) {
debug_assert!(!text.contains('\r'), "The content '{}' contains an unsupported '\\r' line terminator character but string tokens must only use line feeds '\\n' as line separator. Use '\\n' instead of '\\r' and '\\r\\n' to insert a line break in strings.", text);
}

// Token equality only compares the text content
impl PartialEq for Token {
fn eq(&self, other: &Self) -> bool {
Expand Down
1 change: 1 addition & 0 deletions crates/rome_rowan/src/syntax/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ impl<L: Language> SyntaxToken<L> {
self.raw.text()
}

/// Returns the text of a token, including all trivia as an owned value.
pub fn token_text(&self) -> SyntaxTokenText {
self.raw.token_text()
}
Expand Down

0 comments on commit 245c0ea

Please sign in to comment.