mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-22 06:44:35 +00:00
Add trailing comma support
This commit is contained in:
parent
4e8fb743cc
commit
0b7e1baa58
@ -148,7 +148,7 @@ pub enum Breaks {
|
||||
Inconsistent,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
#[derive(Clone, Copy, PartialEq)]
|
||||
enum IndentStyle {
|
||||
/// Vertically aligned under whatever column this block begins at.
|
||||
///
|
||||
@ -164,19 +164,20 @@ enum IndentStyle {
|
||||
Block { offset: isize },
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
#[derive(Clone, Copy, Default, PartialEq)]
|
||||
pub struct BreakToken {
|
||||
offset: isize,
|
||||
blank_space: isize,
|
||||
pre_break: Option<char>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
#[derive(Clone, Copy, PartialEq)]
|
||||
pub struct BeginToken {
|
||||
indent: IndentStyle,
|
||||
breaks: Breaks,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
#[derive(Clone, PartialEq)]
|
||||
pub enum Token {
|
||||
// In practice a string token contains either a `&'static str` or a
|
||||
// `String`. `Cow` is overkill for this because we never modify the data,
|
||||
@ -415,6 +416,9 @@ impl Printer {
|
||||
self.pending_indentation += token.blank_space;
|
||||
self.space -= token.blank_space;
|
||||
} else {
|
||||
if let Some(pre_break) = token.pre_break {
|
||||
self.out.push(pre_break);
|
||||
}
|
||||
self.out.push('\n');
|
||||
let indent = self.indent as isize + token.offset;
|
||||
self.pending_indentation = indent;
|
||||
|
@ -25,7 +25,11 @@ impl Printer {
|
||||
}
|
||||
|
||||
pub fn break_offset(&mut self, n: usize, off: isize) {
|
||||
self.scan_break(BreakToken { offset: off, blank_space: n as isize })
|
||||
self.scan_break(BreakToken {
|
||||
offset: off,
|
||||
blank_space: n as isize,
|
||||
..BreakToken::default()
|
||||
});
|
||||
}
|
||||
|
||||
pub fn end(&mut self) {
|
||||
@ -66,12 +70,24 @@ impl Printer {
|
||||
}
|
||||
|
||||
pub fn hardbreak_tok_offset(off: isize) -> Token {
|
||||
Token::Break(BreakToken { offset: off, blank_space: SIZE_INFINITY })
|
||||
Token::Break(BreakToken {
|
||||
offset: off,
|
||||
blank_space: SIZE_INFINITY,
|
||||
..BreakToken::default()
|
||||
})
|
||||
}
|
||||
|
||||
pub fn trailing_comma(&mut self) {
|
||||
self.scan_break(BreakToken {
|
||||
blank_space: 1,
|
||||
pre_break: Some(','),
|
||||
..BreakToken::default()
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
impl Token {
|
||||
pub fn is_hardbreak_tok(&self) -> bool {
|
||||
matches!(self, Token::Break(BreakToken { offset: 0, blank_space: SIZE_INFINITY }))
|
||||
*self == Printer::hardbreak_tok_offset(0)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user