diff --git a/src/config.rs b/src/config.rs index 01e51d428d7..5407ad8e8b8 100644 --- a/src/config.rs +++ b/src/config.rs @@ -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"; diff --git a/src/items.rs b/src/items.rs index cbe8af541f3..bc57a3e5ed6 100644 --- a/src/items.rs +++ b/src/items.rs @@ -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); } diff --git a/tests/source/item-brace-style-always-next-line.rs b/tests/source/item-brace-style-always-next-line.rs new file mode 100644 index 00000000000..96a628349ea --- /dev/null +++ b/tests/source/item-brace-style-always-next-line.rs @@ -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 where T: Copy { + A, + } + + struct B where T: Copy { + b: i32, + } + + // For empty enums and structs, the brace remains on the same line. + enum C where T: Copy {} + + struct D where T: Copy {} +} diff --git a/tests/source/item-brace-style-prefer-same-line.rs b/tests/source/item-brace-style-prefer-same-line.rs new file mode 100644 index 00000000000..636a584ff68 --- /dev/null +++ b/tests/source/item-brace-style-prefer-same-line.rs @@ -0,0 +1,29 @@ +// rustfmt-item_brace_style: PreferSameLine + +mod M { + enum A + { + A, + } + + struct B + { + b: i32, + } + + enum C {} + + struct D {} + + enum A where T: Copy { + A, + } + + struct B where T: Copy { + b: i32, + } + + enum C where T: Copy {} + + struct D where T: Copy {} +} diff --git a/tests/source/item-brace-style-same-line-where.rs b/tests/source/item-brace-style-same-line-where.rs new file mode 100644 index 00000000000..7b2a95d3245 --- /dev/null +++ b/tests/source/item-brace-style-same-line-where.rs @@ -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 where T: Copy { + A, + } + + struct B where T: Copy { + b: i32, + } + + // For empty enums and structs, the brace remains on the same line. + enum C where T: Copy {} + + struct D where T: Copy {} +} diff --git a/tests/target/item-brace-style-always-next-line.rs b/tests/target/item-brace-style-always-next-line.rs new file mode 100644 index 00000000000..48dcebc0263 --- /dev/null +++ b/tests/target/item-brace-style-always-next-line.rs @@ -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 + where T: Copy + { + A, + } + + struct B + where T: Copy + { + b: i32, + } + + // For empty enums and structs, the brace remains on the same line. + enum C + where T: Copy {} + + struct D + where T: Copy {} +} diff --git a/tests/target/item-brace-style-prefer-same-line.rs b/tests/target/item-brace-style-prefer-same-line.rs new file mode 100644 index 00000000000..b38bd2e1a19 --- /dev/null +++ b/tests/target/item-brace-style-prefer-same-line.rs @@ -0,0 +1,31 @@ +// rustfmt-item_brace_style: PreferSameLine + +mod M { + enum A { + A, + } + + struct B { + b: i32, + } + + enum C {} + + struct D {} + + enum A + where T: Copy { + A, + } + + struct B + where T: Copy { + b: i32, + } + + enum C + where T: Copy {} + + struct D + where T: Copy {} +} diff --git a/tests/target/item-brace-style-same-line-where.rs b/tests/target/item-brace-style-same-line-where.rs new file mode 100644 index 00000000000..cdcd813fda4 --- /dev/null +++ b/tests/target/item-brace-style-same-line-where.rs @@ -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 + where T: Copy + { + A, + } + + struct B + where T: Copy + { + b: i32, + } + + // For empty enums and structs, the brace remains on the same line. + enum C + where T: Copy {} + + struct D + where T: Copy {} +}