Skip to content

Commit 311a1f9

Browse files
authored
Remove len from JoinCommaSeparatedBuilder (#6185)
1 parent b95fc6d commit 311a1f9

File tree

1 file changed

+40
-11
lines changed

1 file changed

+40
-11
lines changed

crates/ruff_python_formatter/src/builders.rs

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -202,23 +202,53 @@ impl<'fmt, 'ast, 'buf> JoinNodesBuilder<'fmt, 'ast, 'buf> {
202202
}
203203
}
204204

205+
#[derive(Copy, Clone, Debug)]
206+
enum Entries {
207+
/// No previous entry
208+
None,
209+
/// One previous ending at the given position.
210+
One(TextSize),
211+
/// More than one entry, the last one ending at the specific position.
212+
MoreThanOne(TextSize),
213+
}
214+
215+
impl Entries {
216+
fn position(self) -> Option<TextSize> {
217+
match self {
218+
Entries::None => None,
219+
Entries::One(position) | Entries::MoreThanOne(position) => Some(position),
220+
}
221+
}
222+
223+
const fn is_one_or_more(self) -> bool {
224+
!matches!(self, Entries::None)
225+
}
226+
227+
const fn is_more_than_one(self) -> bool {
228+
matches!(self, Entries::MoreThanOne(_))
229+
}
230+
231+
const fn next(self, end_position: TextSize) -> Self {
232+
match self {
233+
Entries::None => Entries::One(end_position),
234+
Entries::One(_) | Entries::MoreThanOne(_) => Entries::MoreThanOne(end_position),
235+
}
236+
}
237+
}
238+
205239
pub(crate) struct JoinCommaSeparatedBuilder<'fmt, 'ast, 'buf> {
206240
result: FormatResult<()>,
207241
fmt: &'fmt mut PyFormatter<'ast, 'buf>,
208-
end_of_last_entry: Option<TextSize>,
242+
entries: Entries,
209243
sequence_end: TextSize,
210-
/// We need to track whether we have more than one entry since a sole entry doesn't get a
211-
/// magic trailing comma even when expanded
212-
len: usize,
213244
}
214245

215246
impl<'fmt, 'ast, 'buf> JoinCommaSeparatedBuilder<'fmt, 'ast, 'buf> {
216247
fn new(f: &'fmt mut PyFormatter<'ast, 'buf>, sequence_end: TextSize) -> Self {
217248
Self {
218249
fmt: f,
219250
result: Ok(()),
220-
end_of_last_entry: None,
221-
len: 0,
251+
entries: Entries::None,
222252
sequence_end,
223253
}
224254
}
@@ -245,12 +275,11 @@ impl<'fmt, 'ast, 'buf> JoinCommaSeparatedBuilder<'fmt, 'ast, 'buf> {
245275
Separator: Format<PyFormatContext<'ast>>,
246276
{
247277
self.result = self.result.and_then(|_| {
248-
if self.end_of_last_entry.is_some() {
278+
if self.entries.is_one_or_more() {
249279
write!(self.fmt, [text(","), separator])?;
250280
}
251281

252-
self.end_of_last_entry = Some(node.end());
253-
self.len += 1;
282+
self.entries = self.entries.next(node.end());
254283

255284
content.fmt(self.fmt)
256285
});
@@ -286,7 +315,7 @@ impl<'fmt, 'ast, 'buf> JoinCommaSeparatedBuilder<'fmt, 'ast, 'buf> {
286315

287316
pub(crate) fn finish(&mut self) -> FormatResult<()> {
288317
self.result.and_then(|_| {
289-
if let Some(last_end) = self.end_of_last_entry.take() {
318+
if let Some(last_end) = self.entries.position() {
290319
let magic_trailing_comma = match self.fmt.options().magic_trailing_comma() {
291320
MagicTrailingComma::Respect => {
292321
let first_token = SimpleTokenizer::new(
@@ -310,7 +339,7 @@ impl<'fmt, 'ast, 'buf> JoinCommaSeparatedBuilder<'fmt, 'ast, 'buf> {
310339

311340
// If there is a single entry, only keep the magic trailing comma, don't add it if
312341
// it wasn't there. If there is more than one entry, always add it.
313-
if magic_trailing_comma || self.len > 1 {
342+
if magic_trailing_comma || self.entries.is_more_than_one() {
314343
if_group_breaks(&text(",")).fmt(self.fmt)?;
315344
}
316345

0 commit comments

Comments
 (0)