Skip to content

Commit e2a0297

Browse files
committed
Add trailing comma support
1 parent b9fde2d commit e2a0297

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

src/algorithm.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ pub enum Breaks {
1616
pub struct BreakToken {
1717
pub offset: isize,
1818
pub blank_space: usize,
19+
pub trailing_comma: bool,
1920
}
2021

2122
#[derive(Clone, Copy)]
@@ -255,6 +256,9 @@ impl Printer {
255256
self.pending_indentation += token.blank_space;
256257
self.space -= token.blank_space as isize;
257258
} else {
259+
if token.trailing_comma {
260+
self.out.push(',');
261+
}
258262
self.out.push('\n');
259263
let indent = self.indent as isize + token.offset;
260264
self.pending_indentation = indent as usize;

src/convenience.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ impl Printer {
2626
self.scan_break(BreakToken {
2727
offset: off,
2828
blank_space: n,
29+
trailing_comma: false,
2930
});
3031
}
3132

@@ -62,6 +63,15 @@ impl Printer {
6263
Token::Break(BreakToken {
6364
offset: off,
6465
blank_space: algorithm::SIZE_INFINITY as usize,
66+
trailing_comma: false,
6567
})
6668
}
69+
70+
pub fn trailing_comma(&mut self) {
71+
self.scan_break(BreakToken {
72+
offset: 0,
73+
blank_space: 0,
74+
trailing_comma: true,
75+
});
76+
}
6777
}

src/data.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,14 @@ impl Printer {
4747
self.word("(");
4848
self.cbox(INDENT);
4949
self.zerobreak();
50-
for field in &fields.unnamed {
50+
for (i, field) in fields.unnamed.iter().enumerate() {
5151
self.field(field);
52-
self.word(",");
53-
self.space();
52+
if i < fields.unnamed.len() - 1 {
53+
self.word(",");
54+
self.space();
55+
} else {
56+
self.trailing_comma();
57+
}
5458
}
5559
self.offset(-INDENT);
5660
self.end();

0 commit comments

Comments
 (0)