Skip to content

Commit 88c7530

Browse files
committed
feat(formatter): remove FormatElement::LocatedTokenText (#15367)
The last usage has been removed in #15366, and it is no longer useful to us, so remove it. The performance gains from the `FormatElement` enum, as its size has been reduced from `40` to `24`.
1 parent f913543 commit 88c7530

File tree

8 files changed

+22
-116
lines changed

8 files changed

+22
-116
lines changed

crates/oxc_formatter/src/formatter/builders.rs

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use oxc_span::{GetSpan, Span};
1010
use oxc_syntax::identifier::{is_line_terminator, is_white_space_single_line};
1111

1212
use super::{
13-
Argument, Arguments, Buffer, Comments, GroupId, TextSize, TokenText, VecBuffer,
13+
Argument, Arguments, Buffer, Comments, GroupId, TextSize, VecBuffer,
1414
format_element::{
1515
self,
1616
tag::{Condition, Tag},
@@ -361,33 +361,6 @@ impl std::fmt::Debug for SyntaxTokenCowSlice<'_> {
361361
}
362362
}
363363

364-
/// Copies a source text 1:1 into the output text.
365-
pub fn located_token_text(span: Span, source_text: &str) -> LocatedTokenText {
366-
let slice = span.source_text(source_text);
367-
debug_assert_no_newlines(slice);
368-
LocatedTokenText { text: TokenText::new(slice.to_string(), span), source_position: span.start }
369-
}
370-
371-
pub struct LocatedTokenText {
372-
text: TokenText,
373-
source_position: TextSize,
374-
}
375-
376-
impl Format<'_> for LocatedTokenText {
377-
fn fmt(&self, f: &mut Formatter) -> FormatResult<()> {
378-
f.write_element(FormatElement::LocatedTokenText {
379-
slice: self.text.clone(),
380-
source_position: self.source_position,
381-
})
382-
}
383-
}
384-
385-
impl std::fmt::Debug for LocatedTokenText {
386-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
387-
std::write!(f, "LocatedTokenText({})", self.text)
388-
}
389-
}
390-
391364
#[track_caller]
392365
fn debug_assert_no_newlines(text: &str) {
393366
debug_assert!(

crates/oxc_formatter/src/formatter/format_element/document.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,6 @@ impl Document<'_> {
120120
}
121121
// `FormatElement::Token` cannot contain line breaks
122122
FormatElement::Text { text, .. } => text.contains('\n'),
123-
FormatElement::LocatedTokenText { slice, .. } => slice.contains('\n'),
124123
FormatElement::ExpandParent
125124
| FormatElement::Line(LineMode::Hard | LineMode::Empty) => true,
126125
_ => false,
@@ -195,8 +194,7 @@ impl<'a> Format<'a> for &[FormatElement<'a>] {
195194
element @ (FormatElement::Space
196195
| FormatElement::HardSpace
197196
| FormatElement::Token { .. }
198-
| FormatElement::Text { .. }
199-
| FormatElement::LocatedTokenText { .. }) => {
197+
| FormatElement::Text { .. }) => {
200198
if !in_text {
201199
write!(f, [token("\"")])?;
202200
}
@@ -218,12 +216,6 @@ impl<'a> Format<'a> for &[FormatElement<'a>] {
218216
text: f.context().allocator().alloc_str(&text),
219217
}
220218
}
221-
FormatElement::LocatedTokenText { slice, source_position } => {
222-
let text = slice.cow_replace('"', "\\\"");
223-
FormatElement::Text {
224-
text: f.context().allocator().alloc_str(&text),
225-
}
226-
}
227219
_ => unreachable!(),
228220
};
229221
f.write_element(new_element)?;

crates/oxc_formatter/src/formatter/format_element/mod.rs

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,32 @@
11
pub mod document;
22
pub mod tag;
33

4-
// use biome_rowan::TokenText;
54
// #[cfg(target_pointer_width = "64")]
65
// use biome_rowan::static_assert;
76
use std::hash::{Hash, Hasher};
87
use std::{borrow::Cow, ops::Deref, rc::Rc};
98

109
use super::{
11-
TagKind, TextSize, TokenText,
10+
TagKind, TextSize,
1211
format_element::tag::{LabelId, Tag},
1312
};
1413

14+
#[cfg(debug_assertions)]
15+
const _: () = {
16+
assert!(
17+
std::mem::size_of::<FormatElement>() == 40,
18+
"`FormatElement` size exceeds 40 bytes, expected 40 bytes"
19+
);
20+
};
21+
22+
#[cfg(not(debug_assertions))]
23+
const _: () = {
24+
assert!(
25+
std::mem::size_of::<FormatElement>() == 24,
26+
"`FormatElement` size exceeds 24 bytes, expected 24 bytes"
27+
);
28+
};
29+
1530
/// Language agnostic IR for formatting source code.
1631
///
1732
/// Use the helper functions like [crate::builders::space], [crate::builders::soft_line_break] etc. defined in this file to create elements.
@@ -36,15 +51,6 @@ pub enum FormatElement<'a> {
3651
text: &'a str,
3752
},
3853

39-
/// A token for a text that is taken as is from the source code (input text and formatted representation are identical).
40-
/// Implementing by taking a slice from a `SyntaxToken` to avoid allocating a new string.
41-
LocatedTokenText {
42-
/// The start position of the token in the unformatted source code
43-
source_position: TextSize,
44-
/// The token text
45-
slice: TokenText,
46-
},
47-
4854
/// Prevents that line suffixes move past this boundary. Forces the printer to print any pending
4955
/// line suffixes, potentially by inserting a hard line break.
5056
LineSuffixBoundary,
@@ -69,9 +75,6 @@ impl std::fmt::Debug for FormatElement<'_> {
6975
FormatElement::ExpandParent => fmt.write_str("ExpandParent"),
7076
FormatElement::Token { text } => fmt.debug_tuple("Token").field(text).finish(),
7177
FormatElement::Text { text, .. } => fmt.debug_tuple("Text").field(text).finish(),
72-
FormatElement::LocatedTokenText { slice, .. } => {
73-
fmt.debug_tuple("LocatedTokenText").field(slice).finish()
74-
}
7578
FormatElement::LineSuffixBoundary => fmt.write_str("LineSuffixBoundary"),
7679
FormatElement::BestFitting(best_fitting) => {
7780
fmt.debug_tuple("BestFitting").field(&best_fitting).finish()
@@ -217,12 +220,7 @@ impl FormatElement<'_> {
217220
}
218221

219222
pub const fn is_text(&self) -> bool {
220-
matches!(
221-
self,
222-
FormatElement::LocatedTokenText { .. }
223-
| FormatElement::Text { .. }
224-
| FormatElement::Token { .. }
225-
)
223+
matches!(self, FormatElement::Text { .. } | FormatElement::Token { .. })
226224
}
227225

228226
pub const fn is_space(&self) -> bool {
@@ -241,7 +239,6 @@ impl FormatElements for FormatElement<'_> {
241239
FormatElement::Tag(Tag::StartGroup(group)) => !group.mode().is_flat(),
242240
FormatElement::Line(line_mode) => line_mode.will_break(),
243241
FormatElement::Text { text } => text.contains('\n'),
244-
FormatElement::LocatedTokenText { slice, .. } => slice.contains('\n'),
245242
FormatElement::Interned(interned) => interned.will_break(),
246243
// Traverse into the most flat version because the content is guaranteed to expand when even
247244
// the most flat version contains some content that forces a break.

crates/oxc_formatter/src/formatter/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ mod text_len;
4343
mod text_range;
4444
mod text_size;
4545
pub mod token;
46-
mod token_text;
4746
pub mod trivia;
4847
mod verbatim;
4948

@@ -74,7 +73,6 @@ pub use self::{
7473
text_len::TextLen,
7574
text_range::TextRange,
7675
text_size::TextSize,
77-
token_text::TokenText,
7876
};
7977
use self::{format_element::document::Document, group_id::UniqueGroupIdBuilder, prelude::TagKind};
8078

crates/oxc_formatter/src/formatter/printer/mod.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,6 @@ impl<'a> Printer<'a> {
9797
FormatElement::Text { text } => {
9898
self.print_text(Text::Text(text));
9999
}
100-
FormatElement::LocatedTokenText { slice, source_position } => {
101-
self.print_text(Text::Text(slice));
102-
}
103100
FormatElement::Line(line_mode) => {
104101
if args.mode().is_flat() {
105102
match line_mode {
@@ -1027,9 +1024,6 @@ impl<'a, 'print> FitsMeasurer<'a, 'print> {
10271024
FormatElement::Text { text, .. } => {
10281025
return Ok(self.fits_text(Text::Text(text)));
10291026
}
1030-
FormatElement::LocatedTokenText { slice, .. } => {
1031-
return Ok(self.fits_text(Text::Text(slice)));
1032-
}
10331027

10341028
FormatElement::LineSuffixBoundary => {
10351029
if self.state.has_line_suffix {

crates/oxc_formatter/src/formatter/token_text.rs

Lines changed: 0 additions & 46 deletions
This file was deleted.

crates/oxc_formatter/src/ir_transform/sort_imports/import_unit.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,6 @@ impl SortableImport {
132132

133133
// Strip quotes and params
134134
let source = match &elements[*source_idx] {
135-
FormatElement::LocatedTokenText { slice, .. } => slice,
136135
FormatElement::Text { text } => *text,
137136
_ => unreachable!(
138137
"`source_idx` must point to either `LocatedTokenText` or `Text` in the `elements`."

crates/oxc_formatter/src/ir_transform/sort_imports/source_line.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,7 @@ impl SourceLine {
123123
"{" => has_named_specifier = true,
124124
_ => {}
125125
},
126-
FormatElement::LocatedTokenText { .. }
127-
| FormatElement::Text { .. } => {
126+
FormatElement::Text { .. } => {
128127
has_default_specifier = true;
129128
}
130129
_ => {}
@@ -138,7 +137,7 @@ impl SourceLine {
138137
}
139138
_ => {}
140139
},
141-
FormatElement::LocatedTokenText { .. } | FormatElement::Text { .. } => {
140+
FormatElement::Text { .. } => {
142141
if source_idx.is_none() {
143142
source_idx = Some(idx);
144143
}

0 commit comments

Comments
 (0)