mirror of
https://github.com/rust-lang/rust.git
synced 2025-04-14 04:56:49 +00:00
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:
commit
f09aa85798
@ -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";
|
||||
|
30
src/items.rs
30
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);
|
||||
}
|
||||
|
||||
|
29
tests/source/item-brace-style-always-next-line.rs
Normal file
29
tests/source/item-brace-style-always-next-line.rs
Normal 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 {}
|
||||
}
|
29
tests/source/item-brace-style-prefer-same-line.rs
Normal file
29
tests/source/item-brace-style-prefer-same-line.rs
Normal 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 {}
|
||||
}
|
31
tests/source/item-brace-style-same-line-where.rs
Normal file
31
tests/source/item-brace-style-same-line-where.rs
Normal 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 {}
|
||||
}
|
37
tests/target/item-brace-style-always-next-line.rs
Normal file
37
tests/target/item-brace-style-always-next-line.rs
Normal 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 {}
|
||||
}
|
31
tests/target/item-brace-style-prefer-same-line.rs
Normal file
31
tests/target/item-brace-style-prefer-same-line.rs
Normal 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 {}
|
||||
}
|
35
tests/target/item-brace-style-same-line-where.rs
Normal file
35
tests/target/item-brace-style-same-line-where.rs
Normal 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 {}
|
||||
}
|
Loading…
Reference in New Issue
Block a user