Skip to content

Commit

Permalink
Combine fn_brace_style and item_brace_style
Browse files Browse the repository at this point in the history
  • Loading branch information
topecongiro committed Nov 14, 2017
1 parent bf15438 commit f8074b3
Show file tree
Hide file tree
Showing 29 changed files with 50 additions and 49 deletions.
4 changes: 1 addition & 3 deletions src/config.rs
Expand Up @@ -538,9 +538,7 @@ create_config! {
"Maximum width in the body of a struct variant before falling back to vertical formatting";
force_explicit_abi: bool, true, false, "Always print the abi for extern items";
newline_style: NewlineStyle, NewlineStyle::Unix, false, "Unix or Windows line endings";
fn_brace_style: BraceStyle, BraceStyle::SameLineWhere, false, "Brace style for functions";
item_brace_style: BraceStyle, BraceStyle::SameLineWhere, false,
"Brace style for structs and enums";
brace_style: BraceStyle, BraceStyle::SameLineWhere, false, "Brace style for items";
control_brace_style: ControlBraceStyle, ControlBraceStyle::AlwaysSameLine, false,
"Brace style for control flow constructs";
impl_empty_single_line: bool, true, false, "Put empty-body implementations on a single line";
Expand Down
31 changes: 15 additions & 16 deletions src/items.rs
Expand Up @@ -314,7 +314,7 @@ impl<'a> FmtVisitor<'a> {
rewrite_fn_base(&context, indent, ident, fn_sig, span, newline_brace, true)?;

// 2 = ` {`
if self.config.fn_brace_style() == BraceStyle::AlwaysNextLine || force_newline_brace
if self.config.brace_style() == BraceStyle::AlwaysNextLine || force_newline_brace
|| last_line_width(&result) + 2 > self.shape().width
{
newline_brace = true;
Expand Down Expand Up @@ -440,7 +440,7 @@ impl<'a> FmtVisitor<'a> {
let generics_str = format_generics(
&self.get_context(),
generics,
self.config.item_brace_style(),
self.config.brace_style(),
if enum_def.variants.is_empty() {
BracePos::ForceSameLine
} else {
Expand Down Expand Up @@ -595,7 +595,7 @@ pub fn format_impl(
let where_clause_str = rewrite_where_clause(
context,
&generics.where_clause,
context.config.item_brace_style(),
context.config.brace_style(),
Shape::legacy(where_budget, offset.block_only()),
context.config.where_density(),
"{",
Expand Down Expand Up @@ -641,7 +641,7 @@ pub fn format_impl(
}
result.push_str(&where_clause_str);

match context.config.item_brace_style() {
match context.config.brace_style() {
_ if last_line_contains_single_line_comment(&result) => result.push_str(&sep),
BraceStyle::AlwaysNextLine => result.push_str(&sep),
BraceStyle::PreferSameLine => result.push(' '),
Expand Down Expand Up @@ -784,7 +784,7 @@ fn format_impl_ref_and_type(
let curly_brace_overhead = if generics.where_clause.predicates.is_empty() {
// If there is no where clause adapt budget for type formatting to take space and curly
// brace into account.
match context.config.item_brace_style() {
match context.config.brace_style() {
BraceStyle::AlwaysNextLine => 0,
_ => 2,
}
Expand Down Expand Up @@ -994,7 +994,7 @@ pub fn format_trait(context: &RewriteContext, item: &ast::Item, offset: Indent)
let where_clause_str = rewrite_where_clause(
context,
&generics.where_clause,
context.config.item_brace_style(),
context.config.brace_style(),
Shape::legacy(where_budget, offset.block_only()),
where_density,
"{",
Expand Down Expand Up @@ -1038,7 +1038,7 @@ pub fn format_trait(context: &RewriteContext, item: &ast::Item, offset: Indent)
}
}

match context.config.item_brace_style() {
match context.config.brace_style() {
_ if last_line_contains_single_line_comment(&result) => {
result.push('\n');
result.push_str(&offset.to_string(context.config));
Expand Down Expand Up @@ -1103,7 +1103,7 @@ fn format_unit_struct(context: &RewriteContext, p: &StructParts, offset: Indent)
format_generics(
context,
generics,
context.config.item_brace_style(),
context.config.brace_style(),
BracePos::None,
offset,
mk_sp(generics.span.lo(), hi),
Expand Down Expand Up @@ -1135,7 +1135,7 @@ pub fn format_struct_struct(
Some(g) => format_generics(
context,
g,
context.config.item_brace_style(),
context.config.brace_style(),
if fields.is_empty() {
BracePos::ForceSameLine
} else {
Expand All @@ -1148,8 +1148,7 @@ pub fn format_struct_struct(
None => {
// 3 = ` {}`, 2 = ` {`.
let overhead = if fields.is_empty() { 3 } else { 2 };
if (context.config.item_brace_style() == BraceStyle::AlwaysNextLine
&& !fields.is_empty())
if (context.config.brace_style() == BraceStyle::AlwaysNextLine && !fields.is_empty())
|| context.config.max_width() < overhead + result.len()
{
format!("\n{}{{", offset.block_only().to_string(context.config))
Expand Down Expand Up @@ -1279,7 +1278,7 @@ fn format_tuple_struct(
rewrite_where_clause(
context,
&generics.where_clause,
context.config.item_brace_style(),
context.config.brace_style(),
Shape::legacy(where_budget, offset.block_only()),
Density::Compressed,
";",
Expand Down Expand Up @@ -1365,7 +1364,7 @@ pub fn rewrite_type_alias(
let where_clause_str = rewrite_where_clause(
context,
&generics.where_clause,
context.config.item_brace_style(),
context.config.brace_style(),
Shape::legacy(where_budget, indent),
context.config.where_density(),
"=",
Expand Down Expand Up @@ -2071,7 +2070,7 @@ fn rewrite_fn_base(
if let Some(where_clause_str) = rewrite_where_clause(
context,
where_clause,
context.config.fn_brace_style(),
context.config.brace_style(),
Shape::legacy(budget, indent),
Density::Compressed,
"{",
Expand All @@ -2090,7 +2089,7 @@ fn rewrite_fn_base(
let where_clause_str = rewrite_where_clause(
context,
where_clause,
context.config.fn_brace_style(),
context.config.brace_style(),
Shape::indented(indent, context.config),
Density::Tall,
"{",
Expand Down Expand Up @@ -2386,7 +2385,7 @@ fn newline_for_brace(config: &Config, where_clause: &ast::WhereClause, has_body:
if config.where_single_line() && predicate_count == 1 {
return false;
}
match (config.fn_brace_style(), config.where_density()) {
match (config.brace_style(), config.where_density()) {
(BraceStyle::AlwaysNextLine, _) => true,
(_, Density::Compressed) if predicate_count == 1 => false,
(_, Density::CompressedIfEmpty) if predicate_count == 1 && !has_body => false,
Expand Down
2 changes: 1 addition & 1 deletion src/visitor.rs
Expand Up @@ -666,7 +666,7 @@ impl<'a> FmtVisitor<'a> {
self.buffer.push_str(&ident.to_string());

if is_internal {
match self.config.item_brace_style() {
match self.config.brace_style() {
BraceStyle::AlwaysNextLine => self.buffer
.push_str(&format!("\n{}{{", self.block_indent.to_string(self.config))),
_ => self.buffer.push_str(" {"),
Expand Down
2 changes: 1 addition & 1 deletion tests/config/small_tabs.toml
Expand Up @@ -2,7 +2,7 @@ max_width = 100
comment_width = 80
tab_spaces = 2
newline_style = "Unix"
fn_brace_style = "SameLineWhere"
brace_style = "SameLineWhere"
fn_return_indent = "WithArgs"
fn_args_paren_newline = true
fn_args_density = "Tall"
Expand Down
2 changes: 1 addition & 1 deletion tests/source/configs-fn_brace_style-always_next_line.rs
@@ -1,4 +1,4 @@
// rustfmt-fn_brace_style: AlwaysNextLine
// rustfmt-brace_style: AlwaysNextLine
// Function brace style

fn lorem() {
Expand Down
2 changes: 1 addition & 1 deletion tests/source/configs-fn_brace_style-prefer_same_line.rs
@@ -1,4 +1,4 @@
// rustfmt-fn_brace_style: PreferSameLine
// rustfmt-brace_style: PreferSameLine
// Function brace style

fn lorem() {
Expand Down
2 changes: 1 addition & 1 deletion tests/source/configs-fn_brace_style-same_line_where.rs
@@ -1,4 +1,4 @@
// rustfmt-fn_brace_style: SameLineWhere
// rustfmt-brace_style: SameLineWhere
// Function brace style

fn lorem() {
Expand Down
2 changes: 1 addition & 1 deletion tests/source/configs-item_brace_style-always_next_line.rs
@@ -1,4 +1,4 @@
// rustfmt-item_brace_style: AlwaysNextLine
// rustfmt-brace_style: AlwaysNextLine
// Item brace style

enum Foo {}
Expand Down
2 changes: 1 addition & 1 deletion tests/source/configs-item_brace_style-prefer_same_line.rs
@@ -1,4 +1,4 @@
// rustfmt-item_brace_style: PreferSameLine
// rustfmt-brace_style: PreferSameLine
// Item brace style

struct Lorem {
Expand Down
2 changes: 1 addition & 1 deletion tests/source/configs-item_brace_style-same_line_where.rs
@@ -1,4 +1,4 @@
// rustfmt-item_brace_style: SameLineWhere
// rustfmt-brace_style: SameLineWhere
// Item brace style

struct Lorem {
Expand Down
2 changes: 1 addition & 1 deletion tests/source/fn-custom-6.rs
@@ -1,5 +1,5 @@
// rustfmt-indent_style: Block
// rustfmt-fn_brace_style: PreferSameLine
// rustfmt-brace_style: PreferSameLine
// Test different indents.

fn foo(a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb) {
Expand Down
2 changes: 1 addition & 1 deletion tests/source/fn-custom-7.rs
@@ -1,7 +1,7 @@
// rustfmt-normalize_comments: true
// rustfmt-indent_style: Block
// rustfmt-fn_args_density: Vertical
// rustfmt-fn_brace_style: AlwaysNextLine
// rustfmt-brace_style: AlwaysNextLine

// Case with only one variable.
fn foo(a: u8) -> u8 {
Expand Down
2 changes: 1 addition & 1 deletion tests/source/fn-custom-8.rs
@@ -1,5 +1,5 @@
// rustfmt-indent_style: Block
// rustfmt-fn_brace_style: PreferSameLine
// rustfmt-brace_style: PreferSameLine
// Test different indents.

fn foo(a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb) {
Expand Down
2 changes: 1 addition & 1 deletion tests/source/item-brace-style-always-next-line.rs
@@ -1,4 +1,4 @@
// rustfmt-item_brace_style: AlwaysNextLine
// rustfmt-brace_style: AlwaysNextLine

mod M {
enum A {
Expand Down
2 changes: 1 addition & 1 deletion tests/source/item-brace-style-prefer-same-line.rs
@@ -1,4 +1,4 @@
// rustfmt-item_brace_style: PreferSameLine
// rustfmt-brace_style: PreferSameLine

mod M {
enum A
Expand Down
2 changes: 1 addition & 1 deletion tests/source/item-brace-style-same-line-where.rs
@@ -1,4 +1,4 @@
// rustfmt-item_brace_style: SameLineWhere
// rustfmt-brace_style: SameLineWhere

mod M {
enum A
Expand Down
2 changes: 1 addition & 1 deletion tests/target/configs-fn_brace_style-always_next_line.rs
@@ -1,4 +1,4 @@
// rustfmt-fn_brace_style: AlwaysNextLine
// rustfmt-brace_style: AlwaysNextLine
// Function brace style

fn lorem()
Expand Down
2 changes: 1 addition & 1 deletion tests/target/configs-fn_brace_style-prefer_same_line.rs
@@ -1,4 +1,4 @@
// rustfmt-fn_brace_style: PreferSameLine
// rustfmt-brace_style: PreferSameLine
// Function brace style

fn lorem() {
Expand Down
2 changes: 1 addition & 1 deletion tests/target/configs-fn_brace_style-same_line_where.rs
@@ -1,4 +1,4 @@
// rustfmt-fn_brace_style: SameLineWhere
// rustfmt-brace_style: SameLineWhere
// Function brace style

fn lorem() {
Expand Down
6 changes: 4 additions & 2 deletions tests/target/configs-item_brace_style-always_next_line.rs
@@ -1,4 +1,4 @@
// rustfmt-item_brace_style: AlwaysNextLine
// rustfmt-brace_style: AlwaysNextLine
// Item brace style

enum Foo {}
Expand All @@ -21,5 +21,7 @@ where
mod tests
{
#[test]
fn it_works() {}
fn it_works()
{
}
}
2 changes: 1 addition & 1 deletion tests/target/configs-item_brace_style-prefer_same_line.rs
@@ -1,4 +1,4 @@
// rustfmt-item_brace_style: PreferSameLine
// rustfmt-brace_style: PreferSameLine
// Item brace style

struct Lorem {
Expand Down
2 changes: 1 addition & 1 deletion tests/target/configs-item_brace_style-same_line_where.rs
@@ -1,4 +1,4 @@
// rustfmt-item_brace_style: SameLineWhere
// rustfmt-brace_style: SameLineWhere
// Item brace style

struct Lorem {
Expand Down
2 changes: 1 addition & 1 deletion tests/target/fn-custom-6.rs
@@ -1,5 +1,5 @@
// rustfmt-indent_style: Block
// rustfmt-fn_brace_style: PreferSameLine
// rustfmt-brace_style: PreferSameLine
// Test different indents.

fn foo(a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb) {
Expand Down
5 changes: 3 additions & 2 deletions tests/target/fn-custom-7.rs
@@ -1,7 +1,7 @@
// rustfmt-normalize_comments: true
// rustfmt-indent_style: Block
// rustfmt-fn_args_density: Vertical
// rustfmt-fn_brace_style: AlwaysNextLine
// rustfmt-brace_style: AlwaysNextLine

// Case with only one variable.
fn foo(a: u8) -> u8
Expand Down Expand Up @@ -29,7 +29,8 @@ fn foo(
bar()
}

trait Test {
trait Test
{
fn foo(a: u8)
{
}
Expand Down
2 changes: 1 addition & 1 deletion tests/target/fn-custom-8.rs
@@ -1,5 +1,5 @@
// rustfmt-indent_style: Block
// rustfmt-fn_brace_style: PreferSameLine
// rustfmt-brace_style: PreferSameLine
// Test different indents.

fn foo(a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb) {
Expand Down
5 changes: 3 additions & 2 deletions tests/target/indented-impl.rs
@@ -1,11 +1,12 @@
// rustfmt-item_brace_style: AlwaysNextLine
// rustfmt-brace_style: AlwaysNextLine
mod x
{
struct X(i8);

impl Y for X
{
fn y(self) -> () {
fn y(self) -> ()
{
println!("ok");
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/target/item-brace-style-always-next-line.rs
@@ -1,4 +1,4 @@
// rustfmt-item_brace_style: AlwaysNextLine
// rustfmt-brace_style: AlwaysNextLine

mod M
{
Expand Down
2 changes: 1 addition & 1 deletion tests/target/item-brace-style-prefer-same-line.rs
@@ -1,4 +1,4 @@
// rustfmt-item_brace_style: PreferSameLine
// rustfmt-brace_style: PreferSameLine

mod M {
enum A {
Expand Down
2 changes: 1 addition & 1 deletion tests/target/item-brace-style-same-line-where.rs
@@ -1,4 +1,4 @@
// rustfmt-item_brace_style: SameLineWhere
// rustfmt-brace_style: SameLineWhere

mod M {
enum A {
Expand Down

0 comments on commit f8074b3

Please sign in to comment.