Skip to content

Commit 0b7e1ba

Browse files
committed
Add trailing comma support
1 parent 4e8fb74 commit 0b7e1ba

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

compiler/rustc_ast_pretty/src/pp.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ pub enum Breaks {
148148
Inconsistent,
149149
}
150150

151-
#[derive(Clone, Copy)]
151+
#[derive(Clone, Copy, PartialEq)]
152152
enum IndentStyle {
153153
/// Vertically aligned under whatever column this block begins at.
154154
///
@@ -164,19 +164,20 @@ enum IndentStyle {
164164
Block { offset: isize },
165165
}
166166

167-
#[derive(Clone, Copy)]
167+
#[derive(Clone, Copy, Default, PartialEq)]
168168
pub struct BreakToken {
169169
offset: isize,
170170
blank_space: isize,
171+
pre_break: Option<char>,
171172
}
172173

173-
#[derive(Clone, Copy)]
174+
#[derive(Clone, Copy, PartialEq)]
174175
pub struct BeginToken {
175176
indent: IndentStyle,
176177
breaks: Breaks,
177178
}
178179

179-
#[derive(Clone)]
180+
#[derive(Clone, PartialEq)]
180181
pub enum Token {
181182
// In practice a string token contains either a `&'static str` or a
182183
// `String`. `Cow` is overkill for this because we never modify the data,
@@ -415,6 +416,9 @@ impl Printer {
415416
self.pending_indentation += token.blank_space;
416417
self.space -= token.blank_space;
417418
} else {
419+
if let Some(pre_break) = token.pre_break {
420+
self.out.push(pre_break);
421+
}
418422
self.out.push('\n');
419423
let indent = self.indent as isize + token.offset;
420424
self.pending_indentation = indent;

compiler/rustc_ast_pretty/src/pp/convenience.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ impl Printer {
2525
}
2626

2727
pub fn break_offset(&mut self, n: usize, off: isize) {
28-
self.scan_break(BreakToken { offset: off, blank_space: n as isize })
28+
self.scan_break(BreakToken {
29+
offset: off,
30+
blank_space: n as isize,
31+
..BreakToken::default()
32+
});
2933
}
3034

3135
pub fn end(&mut self) {
@@ -66,12 +70,24 @@ impl Printer {
6670
}
6771

6872
pub fn hardbreak_tok_offset(off: isize) -> Token {
69-
Token::Break(BreakToken { offset: off, blank_space: SIZE_INFINITY })
73+
Token::Break(BreakToken {
74+
offset: off,
75+
blank_space: SIZE_INFINITY,
76+
..BreakToken::default()
77+
})
78+
}
79+
80+
pub fn trailing_comma(&mut self) {
81+
self.scan_break(BreakToken {
82+
blank_space: 1,
83+
pre_break: Some(','),
84+
..BreakToken::default()
85+
});
7086
}
7187
}
7288

7389
impl Token {
7490
pub fn is_hardbreak_tok(&self) -> bool {
75-
matches!(self, Token::Break(BreakToken { offset: 0, blank_space: SIZE_INFINITY }))
91+
*self == Printer::hardbreak_tok_offset(0)
7692
}
7793
}

0 commit comments

Comments
 (0)