Implement initial option for brace style for non-fn items.

This commit is contained in:
Pavel Sountsov 2015-11-15 11:55:18 -08:00 committed by SiegeLord
parent cdf56f75a1
commit 8658774ad2
4 changed files with 53 additions and 2 deletions

View file

@ -302,4 +302,5 @@ create_config! {
take_source_hints: bool, true, "Retain some formatting characteristics from the source code"; take_source_hints: bool, true, "Retain some formatting characteristics from the source code";
hard_tabs: bool, false, "Use tab characters for indentation, spaces for alignment"; hard_tabs: bool, false, "Use tab characters for indentation, spaces for alignment";
wrap_comments: bool, false, "Break comments to fit on the line"; wrap_comments: bool, false, "Break comments to fit on the line";
item_brace_style: BraceStyle, BraceStyle::SameLineWhere, "Brace style for structs and enums";
} }

View file

@ -634,10 +634,17 @@ impl<'a> FmtVisitor<'a> {
let header_str = self.format_header("enum ", ident, vis); let header_str = self.format_header("enum ", ident, vis);
self.buffer.push_str(&header_str); self.buffer.push_str(&header_str);
let separator = if self.config.item_brace_style == BraceStyle::AlwaysNextLine &&
!enum_def.variants.is_empty() {
format!("\n{}", self.block_indent.to_string(self.config))
} else {
" ".to_owned()
};
let enum_snippet = self.snippet(span); let enum_snippet = self.snippet(span);
let body_start = span.lo + BytePos(enum_snippet.find_uncommented("{").unwrap() as u32 + 1); let body_start = span.lo + BytePos(enum_snippet.find_uncommented("{").unwrap() as u32 + 1);
let generics_str = self.format_generics(generics, let generics_str = self.format_generics(generics,
"{", "{",
&separator,
"{", "{",
self.block_indent, self.block_indent,
self.block_indent.block_indent(self.config), self.block_indent.block_indent(self.config),
@ -813,16 +820,24 @@ impl<'a> FmtVisitor<'a> {
let body_lo = span_after(span, "{", self.codemap); let body_lo = span_after(span, "{", self.codemap);
let separator = if self.config.item_brace_style == BraceStyle::AlwaysNextLine &&
!fields.is_empty() {
format!("\n{}", self.block_indent.to_string(self.config))
} else {
" ".to_owned()
};
let generics_str = match generics { let generics_str = match generics {
Some(g) => { Some(g) => {
try_opt!(self.format_generics(g, try_opt!(self.format_generics(g,
"{", "{",
&separator,
"{", "{",
offset, offset,
offset + header_str.len(), offset + header_str.len(),
mk_sp(span.lo, body_lo))) mk_sp(span.lo, body_lo)))
} }
None => " {".to_owned(), None => format!("{}{{", separator),
}; };
result.push_str(&generics_str); result.push_str(&generics_str);
@ -954,6 +969,7 @@ impl<'a> FmtVisitor<'a> {
fn format_generics(&self, fn format_generics(&self,
generics: &ast::Generics, generics: &ast::Generics,
opener: &str, opener: &str,
separator: &str,
terminator: &str, terminator: &str,
offset: Indent, offset: Indent,
generics_offset: Indent, generics_offset: Indent,
@ -973,7 +989,7 @@ impl<'a> FmtVisitor<'a> {
result.push_str(&self.block_indent.to_string(self.config)); result.push_str(&self.block_indent.to_string(self.config));
result.push_str(opener); result.push_str(opener);
} else { } else {
result.push(' '); result.push_str(separator);
result.push_str(opener); result.push_str(opener);
} }

View file

@ -0,0 +1,16 @@
// rustfmt-item_brace_style: AlwaysNextLine
mod M {
enum A {
A,
}
struct B {
b: i32,
}
// For empty enums and structs, the brace remains on the same line.
enum C {}
struct D {}
}

View file

@ -0,0 +1,18 @@
// rustfmt-item_brace_style: AlwaysNextLine
mod M {
enum A
{
A,
}
struct B
{
b: i32,
}
// For empty enums and structs, the brace remains on the same line.
enum C {}
struct D {}
}