From 8658774ad2252f00559d88f1fd8884197041f307 Mon Sep 17 00:00:00 2001 From: Pavel Sountsov Date: Sun, 15 Nov 2015 11:55:18 -0800 Subject: [PATCH 1/3] Implement initial option for brace style for non-fn items. --- src/config.rs | 1 + src/items.rs | 20 ++++++++++++++++++-- tests/source/item-brace-style.rs | 16 ++++++++++++++++ tests/target/item-brace-style.rs | 18 ++++++++++++++++++ 4 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 tests/source/item-brace-style.rs create mode 100644 tests/target/item-brace-style.rs diff --git a/src/config.rs b/src/config.rs index dcf0816335b..7a64ab15916 100644 --- a/src/config.rs +++ b/src/config.rs @@ -302,4 +302,5 @@ create_config! { 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"; wrap_comments: bool, false, "Break comments to fit on the line"; + item_brace_style: BraceStyle, BraceStyle::SameLineWhere, "Brace style for structs and enums"; } diff --git a/src/items.rs b/src/items.rs index 19a2a118e8b..c4c55c841a6 100644 --- a/src/items.rs +++ b/src/items.rs @@ -634,10 +634,17 @@ impl<'a> FmtVisitor<'a> { let header_str = self.format_header("enum ", ident, vis); 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 body_start = span.lo + BytePos(enum_snippet.find_uncommented("{").unwrap() as u32 + 1); let generics_str = self.format_generics(generics, "{", + &separator, "{", self.block_indent, self.block_indent.block_indent(self.config), @@ -813,16 +820,24 @@ impl<'a> FmtVisitor<'a> { 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 { Some(g) => { try_opt!(self.format_generics(g, "{", + &separator, "{", offset, offset + header_str.len(), mk_sp(span.lo, body_lo))) } - None => " {".to_owned(), + None => format!("{}{{", separator), }; result.push_str(&generics_str); @@ -954,6 +969,7 @@ impl<'a> FmtVisitor<'a> { fn format_generics(&self, generics: &ast::Generics, opener: &str, + separator: &str, terminator: &str, 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(opener); } else { - result.push(' '); + result.push_str(separator); result.push_str(opener); } diff --git a/tests/source/item-brace-style.rs b/tests/source/item-brace-style.rs new file mode 100644 index 00000000000..37b02f0940a --- /dev/null +++ b/tests/source/item-brace-style.rs @@ -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 {} +} diff --git a/tests/target/item-brace-style.rs b/tests/target/item-brace-style.rs new file mode 100644 index 00000000000..006d6b4d3bf --- /dev/null +++ b/tests/target/item-brace-style.rs @@ -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 {} +} From a96a69b708dd2dae12d87ea36c545003009c9416 Mon Sep 17 00:00:00 2001 From: Pavel Sountsov Date: Mon, 16 Nov 2015 23:18:07 -0800 Subject: [PATCH 2/3] Comments. - Move the option - Add some more tests (don't pass yet) --- src/config.rs | 2 +- tests/source/item-brace-style-next-line.rs | 29 ++++++++++++++++ tests/source/item-brace-style-same-line.rs | 29 ++++++++++++++++ tests/source/item-brace-style.rs | 16 --------- tests/target/item-brace-style-next-line.rs | 39 ++++++++++++++++++++++ tests/target/item-brace-style-same-line.rs | 31 +++++++++++++++++ tests/target/item-brace-style.rs | 18 ---------- 7 files changed, 129 insertions(+), 35 deletions(-) create mode 100644 tests/source/item-brace-style-next-line.rs create mode 100644 tests/source/item-brace-style-same-line.rs delete mode 100644 tests/source/item-brace-style.rs create mode 100644 tests/target/item-brace-style-next-line.rs create mode 100644 tests/target/item-brace-style-same-line.rs delete mode 100644 tests/target/item-brace-style.rs diff --git a/src/config.rs b/src/config.rs index 7a64ab15916..3990e1af6ad 100644 --- a/src/config.rs +++ b/src/config.rs @@ -266,6 +266,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"; @@ -302,5 +303,4 @@ create_config! { 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"; wrap_comments: bool, false, "Break comments to fit on the line"; - item_brace_style: BraceStyle, BraceStyle::SameLineWhere, "Brace style for structs and enums"; } diff --git a/tests/source/item-brace-style-next-line.rs b/tests/source/item-brace-style-next-line.rs new file mode 100644 index 00000000000..96a628349ea --- /dev/null +++ b/tests/source/item-brace-style-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-same-line.rs b/tests/source/item-brace-style-same-line.rs new file mode 100644 index 00000000000..636a584ff68 --- /dev/null +++ b/tests/source/item-brace-style-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.rs b/tests/source/item-brace-style.rs deleted file mode 100644 index 37b02f0940a..00000000000 --- a/tests/source/item-brace-style.rs +++ /dev/null @@ -1,16 +0,0 @@ -// 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 {} -} diff --git a/tests/target/item-brace-style-next-line.rs b/tests/target/item-brace-style-next-line.rs new file mode 100644 index 00000000000..a5c24102d92 --- /dev/null +++ b/tests/target/item-brace-style-next-line.rs @@ -0,0 +1,39 @@ +// 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-same-line.rs b/tests/target/item-brace-style-same-line.rs new file mode 100644 index 00000000000..b38bd2e1a19 --- /dev/null +++ b/tests/target/item-brace-style-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.rs b/tests/target/item-brace-style.rs deleted file mode 100644 index 006d6b4d3bf..00000000000 --- a/tests/target/item-brace-style.rs +++ /dev/null @@ -1,18 +0,0 @@ -// 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 {} -} From 10560067905331adc158b5613fa8460a3122be1f Mon Sep 17 00:00:00 2001 From: Pavel Sountsov Date: Wed, 18 Nov 2015 22:34:14 -0800 Subject: [PATCH 3/3] Properly follow the brace styles. --- src/items.rs | 46 +++++++++++-------- ...s => item-brace-style-always-next-line.rs} | 0 ...s => item-brace-style-prefer-same-line.rs} | 0 .../item-brace-style-same-line-where.rs | 31 +++++++++++++ ...s => item-brace-style-always-next-line.rs} | 6 +-- ...s => item-brace-style-prefer-same-line.rs} | 0 .../item-brace-style-same-line-where.rs | 35 ++++++++++++++ 7 files changed, 94 insertions(+), 24 deletions(-) rename tests/source/{item-brace-style-next-line.rs => item-brace-style-always-next-line.rs} (100%) rename tests/source/{item-brace-style-same-line.rs => item-brace-style-prefer-same-line.rs} (100%) create mode 100644 tests/source/item-brace-style-same-line-where.rs rename tests/target/{item-brace-style-next-line.rs => item-brace-style-always-next-line.rs} (88%) rename tests/target/{item-brace-style-same-line.rs => item-brace-style-prefer-same-line.rs} (100%) create mode 100644 tests/target/item-brace-style-same-line-where.rs diff --git a/src/items.rs b/src/items.rs index c4c55c841a6..5595b41607b 100644 --- a/src/items.rs +++ b/src/items.rs @@ -634,18 +634,13 @@ impl<'a> FmtVisitor<'a> { let header_str = self.format_header("enum ", ident, vis); 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 body_start = span.lo + BytePos(enum_snippet.find_uncommented("{").unwrap() as u32 + 1); let generics_str = self.format_generics(generics, "{", - &separator, "{", + 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)) @@ -820,24 +815,23 @@ impl<'a> FmtVisitor<'a> { 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 { Some(g) => { try_opt!(self.format_generics(g, "{", - &separator, "{", + self.config.item_brace_style, + fields.is_empty(), offset, offset + header_str.len(), mk_sp(span.lo, body_lo))) } - None => format!("{}{{", separator), + 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); @@ -969,8 +963,9 @@ impl<'a> FmtVisitor<'a> { fn format_generics(&self, generics: &ast::Generics, opener: &str, - separator: &str, terminator: &str, + brace_style: BraceStyle, + force_same_line_brace: bool, offset: Indent, generics_offset: Indent, span: Span) @@ -985,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_str(separator); + 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-next-line.rs b/tests/source/item-brace-style-always-next-line.rs similarity index 100% rename from tests/source/item-brace-style-next-line.rs rename to tests/source/item-brace-style-always-next-line.rs diff --git a/tests/source/item-brace-style-same-line.rs b/tests/source/item-brace-style-prefer-same-line.rs similarity index 100% rename from tests/source/item-brace-style-same-line.rs rename to tests/source/item-brace-style-prefer-same-line.rs 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-next-line.rs b/tests/target/item-brace-style-always-next-line.rs similarity index 88% rename from tests/target/item-brace-style-next-line.rs rename to tests/target/item-brace-style-always-next-line.rs index a5c24102d92..48dcebc0263 100644 --- a/tests/target/item-brace-style-next-line.rs +++ b/tests/target/item-brace-style-always-next-line.rs @@ -30,10 +30,8 @@ mod M { // For empty enums and structs, the brace remains on the same line. enum C - where T: Copy - {} + where T: Copy {} struct D - where T: Copy - {} + where T: Copy {} } diff --git a/tests/target/item-brace-style-same-line.rs b/tests/target/item-brace-style-prefer-same-line.rs similarity index 100% rename from tests/target/item-brace-style-same-line.rs rename to tests/target/item-brace-style-prefer-same-line.rs 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 {} +}