diff --git a/src/config.rs b/src/config.rs index 46c28322c92..84a23b6425d 100644 --- a/src/config.rs +++ b/src/config.rs @@ -56,6 +56,17 @@ configuration_option_enum! { StructLitStyle: // FIXME Maybe we should also have an option to align types. } +// How to style fn args. +configuration_option_enum! { FnArgLayoutStyle: + // First line on the same line as the opening brace, all lines aligned with + // the first line. + Visual, + // Put args on one line if they fit, or start a new line with block indent. + Block, + // First line is on a new line and all lines align with block indent. + BlockAlways, +} + configuration_option_enum! { BlockIndentStyle: // Same level as parent. Inherit, @@ -309,7 +320,7 @@ create_config! { "Location of return type in function declaration"; fn_args_paren_newline: bool, true, "If function argument parenthesis goes on a newline"; fn_args_density: Density, Density::Tall, "Argument density in functions"; - fn_args_layout: StructLitStyle, StructLitStyle::Visual, "Layout of function arguments"; + fn_args_layout: FnArgLayoutStyle, FnArgLayoutStyle::Visual, "Layout of function arguments"; fn_arg_indent: BlockIndentStyle, BlockIndentStyle::Visual, "Indent on function arguments"; type_punctuation_density: TypeDensity, TypeDensity::Wide, "Determines if '+' or '=' are wrapped in spaces in the punctuation of types"; diff --git a/src/items.rs b/src/items.rs index 82937e5a2ad..e2f41259cef 100644 --- a/src/items.rs +++ b/src/items.rs @@ -19,7 +19,7 @@ use expr::{is_empty_block, is_simple_block_stmt, rewrite_assign_rhs}; use comment::{FindUncommented, contains_comment}; use visitor::FmtVisitor; use rewrite::{Rewrite, RewriteContext}; -use config::{Config, BlockIndentStyle, Density, ReturnIndent, BraceStyle, StructLitStyle}; +use config::{Config, BlockIndentStyle, Density, ReturnIndent, BraceStyle, FnArgLayoutStyle}; use syntax::{ast, abi, ptr, codemap}; use syntax::codemap::{Span, BytePos, mk_sp}; @@ -651,8 +651,8 @@ pub fn format_trait(context: &RewriteContext, item: &ast::Item, offset: Indent) let where_density = if (context.config.where_density == Density::Compressed && (!result.contains('\n') || - context.config.fn_args_layout == StructLitStyle::Block)) || - (context.config.fn_args_layout == StructLitStyle::Block && + context.config.fn_args_layout == FnArgLayoutStyle::Block)) || + (context.config.fn_args_layout == FnArgLayoutStyle::Block && result.is_empty()) || (context.config.where_density == Density::CompressedIfEmpty && !has_body && @@ -1294,9 +1294,15 @@ fn rewrite_fn_base(context: &RewriteContext, }; // Args. - let (mut one_line_budget, multi_line_budget, mut arg_indent) = + let (mut one_line_budget, mut multi_line_budget, mut arg_indent) = compute_budgets_for_args(context, &result, indent, ret_str_len, newline_brace); + if context.config.fn_args_layout == FnArgLayoutStyle::Block || + context.config.fn_args_layout == FnArgLayoutStyle::BlockAlways { + arg_indent = indent.block_indent(context.config); + multi_line_budget = context.config.max_width - arg_indent.width(); + } + debug!("rewrite_fn: one_line_budget: {}, multi_line_budget: {}, arg_indent: {:?}", one_line_budget, multi_line_budget, @@ -1313,10 +1319,6 @@ fn rewrite_fn_base(context: &RewriteContext, result.push_str("(\n"); result.push_str(&arg_indent.to_string(context.config)); } - } else if context.config.fn_args_layout == StructLitStyle::Block { - arg_indent = indent.block_indent(context.config); - result.push_str("(\n"); - result.push_str(&arg_indent.to_string(context.config)); } else { result.push('('); } @@ -1340,23 +1342,42 @@ fn rewrite_fn_base(context: &RewriteContext, arg_indent, args_span, fd.variadic)); - result.push_str(&arg_str); - if context.config.fn_args_layout == StructLitStyle::Block { + + let multi_line_arg_str = arg_str.contains('\n'); + + let put_args_in_block = match context.config.fn_args_layout { + FnArgLayoutStyle::Block => multi_line_arg_str, + FnArgLayoutStyle::BlockAlways => true, + _ => false, + } && fd.inputs.len() > 0; + + if put_args_in_block { + arg_indent = indent.block_indent(context.config); + result.push('\n'); + result.push_str(&arg_indent.to_string(context.config)); + result.push_str(&arg_str); result.push('\n'); result.push_str(&indent.to_string(context.config)); + result.push(')'); + } else { + result.push_str(&arg_str); + result.push(')'); } - result.push(')'); // Return type. if !ret_str.is_empty() { - // If we've already gone multi-line, or the return type would push - // over the max width, then put the return type on a new line. - // Unless we are formatting args like a block, in which case there - // should always be room for the return type. - let ret_indent = if (result.contains("\n") || multi_line_ret_str || - result.len() + indent.width() + ret_str_len > - context.config.max_width) && - context.config.fn_args_layout != StructLitStyle::Block { + let ret_should_indent = match context.config.fn_args_layout { + // If our args are block layout then we surely must have space. + FnArgLayoutStyle::Block if put_args_in_block => false, + FnArgLayoutStyle::BlockAlways => false, + _ => { + // If we've already gone multi-line, or the return type would push + // over the max width, then put the return type on a new line. + result.contains("\n") || multi_line_ret_str || + result.len() + indent.width() + ret_str_len > context.config.max_width + } + }; + let ret_indent = if ret_should_indent { let indent = match context.config.fn_return_indent { ReturnIndent::WithWhereClause => indent + 4, // Aligning with non-existent args looks silly. @@ -1407,13 +1428,13 @@ fn rewrite_fn_base(context: &RewriteContext, } } - let where_density = if (context.config.where_density == Density::Compressed && - (!result.contains('\n') || - context.config.fn_args_layout == StructLitStyle::Block)) || - (context.config.fn_args_layout == StructLitStyle::Block && - ret_str.is_empty()) || - (context.config.where_density == Density::CompressedIfEmpty && - !has_body && !result.contains('\n')) { + let should_compress_where = match context.config.where_density { + Density::Compressed => !result.contains('\n') || put_args_in_block, + Density::CompressedIfEmpty => !has_body && !result.contains('\n'), + _ => false, + } || (put_args_in_block && ret_str.is_empty()); + + let where_density = if should_compress_where { Density::Compressed } else { Density::Tall @@ -1554,8 +1575,10 @@ fn rewrite_args(context: &RewriteContext, _ => multi_line_budget, }; + debug!("rewrite_args: budget: {}, tactic: {:?}", budget, tactic); + let end_with_newline = match context.config.fn_args_layout { - StructLitStyle::Block => true, + FnArgLayoutStyle::Block | FnArgLayoutStyle::BlockAlways => true, _ => false, }; diff --git a/tests/source/fn-custom-6.rs b/tests/source/fn-custom-6.rs index 7941d3a215e..1f2d740a075 100644 --- a/tests/source/fn-custom-6.rs +++ b/tests/source/fn-custom-6.rs @@ -1,4 +1,4 @@ -// rustfmt-fn_args_layout: Block +// rustfmt-fn_args_layout: BlockAlways // rustfmt-where_indent: Inherit // rustfmt-fn_brace_style: PreferSameLine // Test different indents. diff --git a/tests/source/fn-custom-7.rs b/tests/source/fn-custom-7.rs index 98f457207e3..27c97867d26 100644 --- a/tests/source/fn-custom-7.rs +++ b/tests/source/fn-custom-7.rs @@ -1,4 +1,4 @@ -// rustfmt-fn_args_layout: Block +// rustfmt-fn_args_layout: BlockAlways // rustfmt-fn_args_density: Vertical // rustfmt-fn_arg_indent: Tabbed // rustfmt-fn_brace_style: AlwaysNextLine diff --git a/tests/source/fn-custom-8.rs b/tests/source/fn-custom-8.rs new file mode 100644 index 00000000000..a1aebf1f38c --- /dev/null +++ b/tests/source/fn-custom-8.rs @@ -0,0 +1,50 @@ +// rustfmt-fn_args_layout: Block +// rustfmt-where_indent: Inherit +// rustfmt-fn_brace_style: PreferSameLine +// Test different indents. + +fn foo(a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb) { + foo(); +} + +fn bar(a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb, c: Cccccccccccccccccc, d: Dddddddddddddddd, e: Eeeeeeeeeeeeeee) { + bar(); +} + +fn foo(a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb) -> String { + foo(); +} + +fn bar(a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb, c: Cccccccccccccccccc, d: Dddddddddddddddd, e: Eeeeeeeeeeeeeee) -> String { + bar(); +} + +fn foo(a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb) where T: UUUUUUUUUUU { + foo(); +} + +fn bar(a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb, c: Cccccccccccccccccc, d: Dddddddddddddddd, e: Eeeeeeeeeeeeeee) where T: UUUUUUUUUUU { + bar(); +} + +fn foo(a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb) -> String where T: UUUUUUUUUUU { + foo(); +} + +fn bar(a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb, c: Cccccccccccccccccc, d: Dddddddddddddddd, e: Eeeeeeeeeeeeeee) -> String where T: UUUUUUUUUUU { + bar(); +} + +trait Test { + fn foo( + a: u8) { + + } + + fn bar(a: u8) + -> String { + + } + + fn bar(a: u8) -> String where Foo: foooo, Bar: barrr {} +} diff --git a/tests/source/fn_args_layout-block.rs b/tests/source/fn_args_layout-block.rs index 07cc983d5a8..b87158a4d7f 100644 --- a/tests/source/fn_args_layout-block.rs +++ b/tests/source/fn_args_layout-block.rs @@ -1,5 +1,8 @@ // rustfmt-fn_args_layout: Block +fn foo() { + foo(); +} fn foo(a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb) { foo(); @@ -17,8 +20,58 @@ fn bar(a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb, c: Cccccccccccccccccc, d: Ddddddddd bar(); } +fn foo(a: u8 /* Comment 1 */, b: u8 /* Comment 2 */) -> u8 { + bar() +} + +fn foo(a: u8 /* Comment 1 */, b: Bbbbbbbbbbbbbb, c: Cccccccccccccccccc, d: Dddddddddddddddd, e: Eeeeeeeeeeeeeee /* Comment 2 */) -> u8 { + bar() +} + +fn bar(a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb, c: Cccccccccccccccccc, d: Dddddddddddddddd, e: Eeeeeeeeeeeeeee) -> String where X: Fooooo, Y: Baaar { + bar(); +} + +fn foo() -> T { + foo(); +} + +fn foo() -> T where X: Foooo, Y: Baaar { + foo(); +} + +fn foo() where X: Foooo { +} + +fn foo() where X: Foooo, Y: Baaar { +} + +fn foo() -> (Loooooooooooooooooooooong, Reeeeeeeeeeeeeeeeeeeeeeeeturn, iiiiiiiiis, Looooooooooooooooong) { + foo(); +} + +fn foo() { + foo(); +} + +fn foo() { + foo(); +} + +fn foo() { + foo(); +} + trait Test { fn foo(a: u8) {} - fn bar(a: u8) -> String {} + fn bar(a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb, c: Cccccccccccccccccc, d: Dddddddddddddddd, e: Eeeeeeeeeeeeeee) -> String {} +} + +fn foo(a: Aaaaaaaaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbbbbb, c: Cccccccccccccccccc, d: Dddddddddddddddd) { + foo(); +} + +fn foo() -> (Looooooooooooooooooooooooooong, Reeeeeeeeeeeeeeeeeeeeeeeeeeeeeturn, iiiiiiiiiiiiiis, Loooooooooooooooooooooong) { + foo(); } diff --git a/tests/source/fn_args_layout-blockalways.rs b/tests/source/fn_args_layout-blockalways.rs new file mode 100644 index 00000000000..d4bb00b9142 --- /dev/null +++ b/tests/source/fn_args_layout-blockalways.rs @@ -0,0 +1,27 @@ +// rustfmt-fn_args_layout: BlockAlways + +fn foo() { + foo(); +} + +fn foo(a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb) { + foo(); +} + +fn bar(a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb, c: Cccccccccccccccccc, d: Dddddddddddddddd, e: Eeeeeeeeeeeeeee) { + bar(); +} + +fn foo(a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb) -> String { + foo(); +} + +fn bar(a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb, c: Cccccccccccccccccc, d: Dddddddddddddddd, e: Eeeeeeeeeeeeeee) -> String { + bar(); +} + +trait Test { + fn foo(a: u8) {} + + fn bar(a: u8) -> String {} +} diff --git a/tests/target/fn-custom-6.rs b/tests/target/fn-custom-6.rs index 8c1f89173d7..74e6765847e 100644 --- a/tests/target/fn-custom-6.rs +++ b/tests/target/fn-custom-6.rs @@ -1,4 +1,4 @@ -// rustfmt-fn_args_layout: Block +// rustfmt-fn_args_layout: BlockAlways // rustfmt-where_indent: Inherit // rustfmt-fn_brace_style: PreferSameLine // Test different indents. diff --git a/tests/target/fn-custom-7.rs b/tests/target/fn-custom-7.rs index c58678d45d9..82260d8c863 100644 --- a/tests/target/fn-custom-7.rs +++ b/tests/target/fn-custom-7.rs @@ -1,4 +1,4 @@ -// rustfmt-fn_args_layout: Block +// rustfmt-fn_args_layout: BlockAlways // rustfmt-fn_args_density: Vertical // rustfmt-fn_arg_indent: Tabbed // rustfmt-fn_brace_style: AlwaysNextLine diff --git a/tests/target/fn-custom-8.rs b/tests/target/fn-custom-8.rs new file mode 100644 index 00000000000..bd4a379969b --- /dev/null +++ b/tests/target/fn-custom-8.rs @@ -0,0 +1,74 @@ +// rustfmt-fn_args_layout: Block +// rustfmt-where_indent: Inherit +// rustfmt-fn_brace_style: PreferSameLine +// Test different indents. + +fn foo(a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb) { + foo(); +} + +fn bar( + a: Aaaaaaaaaaaaaa, + b: Bbbbbbbbbbbbbb, + c: Cccccccccccccccccc, + d: Dddddddddddddddd, + e: Eeeeeeeeeeeeeee +) { + bar(); +} + +fn foo(a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb) -> String { + foo(); +} + +fn bar( + a: Aaaaaaaaaaaaaa, + b: Bbbbbbbbbbbbbb, + c: Cccccccccccccccccc, + d: Dddddddddddddddd, + e: Eeeeeeeeeeeeeee +) -> String { + bar(); +} + +fn foo(a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb) +where T: UUUUUUUUUUU { + foo(); +} + +fn bar( + a: Aaaaaaaaaaaaaa, + b: Bbbbbbbbbbbbbb, + c: Cccccccccccccccccc, + d: Dddddddddddddddd, + e: Eeeeeeeeeeeeeee +) where T: UUUUUUUUUUU { + bar(); +} + +fn foo(a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb) -> String +where T: UUUUUUUUUUU { + foo(); +} + +fn bar( + a: Aaaaaaaaaaaaaa, + b: Bbbbbbbbbbbbbb, + c: Cccccccccccccccccc, + d: Dddddddddddddddd, + e: Eeeeeeeeeeeeeee +) -> String +where T: UUUUUUUUUUU { + bar(); +} + +trait Test { + fn foo(a: u8) {} + + fn bar(a: u8) -> String {} + + fn bar(a: u8) -> String + where Foo: foooo, + Bar: barrr { + } +} diff --git a/tests/target/fn_args_layout-block.rs b/tests/target/fn_args_layout-block.rs index 7e1318a7a3c..cf380c649c8 100644 --- a/tests/target/fn_args_layout-block.rs +++ b/tests/target/fn_args_layout-block.rs @@ -1,9 +1,10 @@ // rustfmt-fn_args_layout: Block +fn foo() { + foo(); +} -fn foo( - a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb -) { +fn foo(a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb) { foo(); } @@ -17,9 +18,7 @@ fn bar( bar(); } -fn foo( - a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb -) -> String { +fn foo(a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb) -> String { foo(); } @@ -33,14 +32,99 @@ fn bar( bar(); } +fn foo(a: u8 /* Comment 1 */, b: u8 /* Comment 2 */) -> u8 { + bar() +} + +fn foo( + a: u8, // Comment 1 + b: Bbbbbbbbbbbbbb, + c: Cccccccccccccccccc, + d: Dddddddddddddddd, + e: Eeeeeeeeeeeeeee // Comment 2 +) -> u8 { + bar() +} + +fn bar( + a: Aaaaaaaaaaaaaa, + b: Bbbbbbbbbbbbbb, + c: Cccccccccccccccccc, + d: Dddddddddddddddd, + e: Eeeeeeeeeeeeeee +) -> String + where X: Fooooo, + Y: Baaar +{ + bar(); +} + +fn foo() -> T { + foo(); +} + +fn foo() -> T + where X: Foooo, + Y: Baaar +{ + foo(); +} + +fn foo() where X: Foooo {} + +fn foo() + where X: Foooo, + Y: Baaar +{ +} + +fn foo + () + -> (Loooooooooooooooooooooong, Reeeeeeeeeeeeeeeeeeeeeeeeturn, iiiiiiiiis, Looooooooooooooooong) +{ + foo(); +} + +fn foo() { + foo(); +} + +fn foo + () { + foo(); +} + +fn foo() { + foo(); +} + trait Test { - fn foo( - a: u8 - ) { - } + fn foo(a: u8) {} fn bar( - a: u8 + a: Aaaaaaaaaaaaaa, + b: Bbbbbbbbbbbbbb, + c: Cccccccccccccccccc, + d: Dddddddddddddddd, + e: Eeeeeeeeeeeeeee ) -> String { } } + +fn foo( + a: Aaaaaaaaaaaaaaaaaaaa, + b: Bbbbbbbbbbbbbbbbb, + c: Cccccccccccccccccc, + d: Dddddddddddddddd +) { + foo(); +} + +fn foo() + -> (Looooooooooooooooooooooooooong, + Reeeeeeeeeeeeeeeeeeeeeeeeeeeeeturn, + iiiiiiiiiiiiiis, + Loooooooooooooooooooooong) +{ + foo(); +} diff --git a/tests/target/fn_args_layout-blockalways.rs b/tests/target/fn_args_layout-blockalways.rs new file mode 100644 index 00000000000..11f04d13ce6 --- /dev/null +++ b/tests/target/fn_args_layout-blockalways.rs @@ -0,0 +1,49 @@ +// rustfmt-fn_args_layout: BlockAlways + +fn foo() { + foo(); +} + +fn foo( + a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb +) { + foo(); +} + +fn bar( + a: Aaaaaaaaaaaaaa, + b: Bbbbbbbbbbbbbb, + c: Cccccccccccccccccc, + d: Dddddddddddddddd, + e: Eeeeeeeeeeeeeee +) { + bar(); +} + +fn foo( + a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb +) -> String { + foo(); +} + +fn bar( + a: Aaaaaaaaaaaaaa, + b: Bbbbbbbbbbbbbb, + c: Cccccccccccccccccc, + d: Dddddddddddddddd, + e: Eeeeeeeeeeeeeee +) -> String { + bar(); +} + +trait Test { + fn foo( + a: u8 + ) { + } + + fn bar( + a: u8 + ) -> String { + } +}