Merge pull request #619 from SiegeLord/item_brace_style_1

Implement initial option for brace style for non-fn items.
This commit is contained in:
Nick Cameron 2015-11-20 07:41:12 +13:00
commit f09aa85798
8 changed files with 219 additions and 4 deletions

View file

@ -268,6 +268,7 @@ create_config! {
"Maximum width in the body of a struct lit before falling back to vertical formatting";
newline_style: NewlineStyle, NewlineStyle::Unix, "Unix or Windows line endings";
fn_brace_style: BraceStyle, BraceStyle::SameLineWhere, "Brace style for functions";
item_brace_style: BraceStyle, BraceStyle::SameLineWhere, "Brace style for structs and enums";
fn_return_indent: ReturnIndent, ReturnIndent::WithArgs,
"Location of return type in function declaration";
fn_args_paren_newline: bool, true, "If function argument parenthesis goes on a newline";

View file

@ -639,6 +639,8 @@ impl<'a> FmtVisitor<'a> {
let generics_str = self.format_generics(generics,
"{",
"{",
self.config.item_brace_style,
enum_def.variants.is_empty(),
self.block_indent,
self.block_indent.block_indent(self.config),
mk_sp(span.lo, body_start))
@ -818,11 +820,18 @@ impl<'a> FmtVisitor<'a> {
try_opt!(self.format_generics(g,
"{",
"{",
self.config.item_brace_style,
fields.is_empty(),
offset,
offset + header_str.len(),
mk_sp(span.lo, body_lo)))
}
None => " {".to_owned(),
None => if self.config.item_brace_style == BraceStyle::AlwaysNextLine &&
!fields.is_empty() {
format!("\n{}{{", self.block_indent.to_string(self.config))
} else {
" {".to_owned()
},
};
result.push_str(&generics_str);
@ -955,6 +964,8 @@ impl<'a> FmtVisitor<'a> {
generics: &ast::Generics,
opener: &str,
terminator: &str,
brace_style: BraceStyle,
force_same_line_brace: bool,
offset: Indent,
generics_offset: Indent,
span: Span)
@ -969,11 +980,22 @@ impl<'a> FmtVisitor<'a> {
terminator,
Some(span.hi)));
result.push_str(&where_clause_str);
result.push('\n');
result.push_str(&self.block_indent.to_string(self.config));
if !force_same_line_brace &&
(brace_style == BraceStyle::SameLineWhere ||
brace_style == BraceStyle::AlwaysNextLine) {
result.push('\n');
result.push_str(&self.block_indent.to_string(self.config));
} else {
result.push(' ');
}
result.push_str(opener);
} else {
result.push(' ');
if !force_same_line_brace && brace_style == BraceStyle::AlwaysNextLine {
result.push('\n');
result.push_str(&self.block_indent.to_string(self.config));
} else {
result.push(' ');
}
result.push_str(opener);
}

View file

@ -0,0 +1,29 @@
// 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 {}
enum A<T> where T: Copy {
A,
}
struct B<T> where T: Copy {
b: i32,
}
// For empty enums and structs, the brace remains on the same line.
enum C<T> where T: Copy {}
struct D<T> where T: Copy {}
}

View file

@ -0,0 +1,29 @@
// rustfmt-item_brace_style: PreferSameLine
mod M {
enum A
{
A,
}
struct B
{
b: i32,
}
enum C {}
struct D {}
enum A<T> where T: Copy {
A,
}
struct B<T> where T: Copy {
b: i32,
}
enum C<T> where T: Copy {}
struct D<T> where T: Copy {}
}

View file

@ -0,0 +1,31 @@
// rustfmt-item_brace_style: SameLineWhere
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 {}
enum A<T> where T: Copy {
A,
}
struct B<T> where T: Copy {
b: i32,
}
// For empty enums and structs, the brace remains on the same line.
enum C<T> where T: Copy {}
struct D<T> where T: Copy {}
}

View file

@ -0,0 +1,37 @@
// 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 {}
enum A<T>
where T: Copy
{
A,
}
struct B<T>
where T: Copy
{
b: i32,
}
// For empty enums and structs, the brace remains on the same line.
enum C<T>
where T: Copy {}
struct D<T>
where T: Copy {}
}

View file

@ -0,0 +1,31 @@
// rustfmt-item_brace_style: PreferSameLine
mod M {
enum A {
A,
}
struct B {
b: i32,
}
enum C {}
struct D {}
enum A<T>
where T: Copy {
A,
}
struct B<T>
where T: Copy {
b: i32,
}
enum C<T>
where T: Copy {}
struct D<T>
where T: Copy {}
}

View file

@ -0,0 +1,35 @@
// rustfmt-item_brace_style: SameLineWhere
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 {}
enum A<T>
where T: Copy
{
A,
}
struct B<T>
where T: Copy
{
b: i32,
}
// For empty enums and structs, the brace remains on the same line.
enum C<T>
where T: Copy {}
struct D<T>
where T: Copy {}
}