From 57847e451af3d0057f36f61c357f5c451b4ac7ed Mon Sep 17 00:00:00 2001 From: Erik Johnston Date: Sat, 2 Apr 2016 00:25:35 +0100 Subject: [PATCH 1/4] Add fn_arg_one_line option If set, arguments will be kept on one line if they fit. Currently only applies when `fn_args_layout` is set to `Block`. This commit also fixes a bug where newlines were inserted inbetween argument brackets when there were no arguments and `fn_args_layout` was set to `Block`. --- src/config.rs | 1 + src/items.rs | 27 ++++++--- tests/source/fn_arg_one_line.rs | 48 ++++++++++++++++ tests/source/fn_args_layout-block.rs | 3 + tests/target/fn_arg_one_line.rs | 85 ++++++++++++++++++++++++++++ tests/target/fn_args_layout-block.rs | 3 + 6 files changed, 160 insertions(+), 7 deletions(-) create mode 100644 tests/source/fn_arg_one_line.rs create mode 100644 tests/target/fn_arg_one_line.rs diff --git a/src/config.rs b/src/config.rs index 32eb9af4b11..02ee5741c45 100644 --- a/src/config.rs +++ b/src/config.rs @@ -310,6 +310,7 @@ create_config! { fn_args_density: Density, Density::Tall, "Argument density in functions"; fn_args_layout: StructLitStyle, StructLitStyle::Visual, "Layout of function arguments"; fn_arg_indent: BlockIndentStyle, BlockIndentStyle::Visual, "Indent on function arguments"; + fn_arg_one_line: bool, false, "Keep arguments on one line if they fit"; type_punctuation_density: TypeDensity, TypeDensity::Wide, "Determines if '+' or '=' are wrapped in spaces in the punctuation of types"; // Should we at least try to put the where clause on the same line as the rest of the diff --git a/src/items.rs b/src/items.rs index 2e0cac0df07..da6d77fca45 100644 --- a/src/items.rs +++ b/src/items.rs @@ -1293,6 +1293,10 @@ fn rewrite_fn_base(context: &RewriteContext, let (mut one_line_budget, multi_line_budget, mut arg_indent) = compute_budgets_for_args(context, &result, indent, ret_str_len, newline_brace); + if context.config.fn_args_layout == StructLitStyle::Block { + arg_indent = indent.block_indent(context.config); + } + debug!("rewrite_fn: one_line_budget: {}, multi_line_budget: {}, arg_indent: {:?}", one_line_budget, multi_line_budget, @@ -1309,10 +1313,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('('); } @@ -1336,12 +1336,25 @@ 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 should_put_args_in_block = context.config.fn_args_layout == StructLitStyle::Block && + (multi_line_arg_str || !context.config.fn_arg_one_line) && + fd.inputs.len() > 0; + + if should_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() { diff --git a/tests/source/fn_arg_one_line.rs b/tests/source/fn_arg_one_line.rs new file mode 100644 index 00000000000..23aea2b58f7 --- /dev/null +++ b/tests/source/fn_arg_one_line.rs @@ -0,0 +1,48 @@ +// rustfmt-fn_args_layout: Block +// rustfmt-fn_arg_one_line: true + +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(); +} + +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(); +} + +trait Test { + fn foo(a: u8) {} + + fn bar(a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb, c: Cccccccccccccccccc, d: Dddddddddddddddd, e: Eeeeeeeeeeeeeee) -> String {} +} diff --git a/tests/source/fn_args_layout-block.rs b/tests/source/fn_args_layout-block.rs index 07cc983d5a8..a5b8e66931f 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(); diff --git a/tests/target/fn_arg_one_line.rs b/tests/target/fn_arg_one_line.rs new file mode 100644 index 00000000000..ef5eeea90bd --- /dev/null +++ b/tests/target/fn_arg_one_line.rs @@ -0,0 +1,85 @@ +// rustfmt-fn_args_layout: Block +// rustfmt-fn_arg_one_line: true + +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(); +} + +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(); +} + +trait Test { + fn foo(a: u8) {} + + fn bar( + a: Aaaaaaaaaaaaaa, + b: Bbbbbbbbbbbbbb, + c: Cccccccccccccccccc, + d: Dddddddddddddddd, + e: Eeeeeeeeeeeeeee + ) -> String { + } +} diff --git a/tests/target/fn_args_layout-block.rs b/tests/target/fn_args_layout-block.rs index 7e1318a7a3c..f18cf2197d8 100644 --- a/tests/target/fn_args_layout-block.rs +++ b/tests/target/fn_args_layout-block.rs @@ -1,5 +1,8 @@ // rustfmt-fn_args_layout: Block +fn foo() { + foo(); +} fn foo( a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb From 4edc6f1a4e069dcb46d1fc583dccefba7e4aae44 Mon Sep 17 00:00:00 2001 From: Erik Johnston Date: Thu, 7 Apr 2016 20:01:16 +0100 Subject: [PATCH 2/4] Add variant to fn_args_layout --- src/config.rs | 14 +++- src/items.rs | 50 ++++++------ tests/source/fn_arg_one_line.rs | 48 ------------ tests/source/fn_args_layout-block.rs | 48 +++++++++++- tests/source/fn_args_layout-blockalways.rs | 27 +++++++ tests/target/fn_arg_one_line.rs | 85 --------------------- tests/target/fn_args_layout-block.rs | 89 +++++++++++++++++++--- tests/target/fn_args_layout-blockalways.rs | 49 ++++++++++++ 8 files changed, 241 insertions(+), 169 deletions(-) delete mode 100644 tests/source/fn_arg_one_line.rs create mode 100644 tests/source/fn_args_layout-blockalways.rs delete mode 100644 tests/target/fn_arg_one_line.rs create mode 100644 tests/target/fn_args_layout-blockalways.rs diff --git a/src/config.rs b/src/config.rs index 3c426b88033..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,9 +320,8 @@ 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"; - fn_arg_one_line: bool, false, "Keep arguments on one line if they fit"; type_punctuation_density: TypeDensity, TypeDensity::Wide, "Determines if '+' or '=' are wrapped in spaces in the punctuation of types"; // Should we at least try to put the where clause on the same line as the rest of the diff --git a/src/items.rs b/src/items.rs index da6d77fca45..f3ba3872a91 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 && @@ -1293,7 +1293,8 @@ fn rewrite_fn_base(context: &RewriteContext, let (mut one_line_budget, multi_line_budget, mut arg_indent) = compute_budgets_for_args(context, &result, indent, ret_str_len, newline_brace); - if context.config.fn_args_layout == StructLitStyle::Block { + if context.config.fn_args_layout == FnArgLayoutStyle::Block || + context.config.fn_args_layout == FnArgLayoutStyle::BlockAlways { arg_indent = indent.block_indent(context.config); } @@ -1339,11 +1340,13 @@ fn rewrite_fn_base(context: &RewriteContext, let multi_line_arg_str = arg_str.contains('\n'); - let should_put_args_in_block = context.config.fn_args_layout == StructLitStyle::Block && - (multi_line_arg_str || !context.config.fn_arg_one_line) && - fd.inputs.len() > 0; + 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 should_put_args_in_block { + if put_args_in_block { arg_indent = indent.block_indent(context.config); result.push('\n'); result.push_str(&arg_indent.to_string(context.config)); @@ -1358,14 +1361,17 @@ fn rewrite_fn_base(context: &RewriteContext, // Return type. if !ret_str.is_empty() { + let ret_should_indent = match context.config.fn_args_layout { + FnArgLayoutStyle::Block if put_args_in_block => false, + FnArgLayoutStyle::BlockAlways => false, + _ => { + result.contains("\n") || multi_line_ret_str || + result.len() + indent.width() + ret_str_len > context.config.max_width + } + }; // 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_indent = if ret_should_indent { let indent = match context.config.fn_return_indent { ReturnIndent::WithWhereClause => indent + 4, // Aligning with non-existent args looks silly. @@ -1416,13 +1422,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 where_compressed = 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 where_compressed { Density::Compressed } else { Density::Tall @@ -1564,7 +1570,7 @@ fn rewrite_args(context: &RewriteContext, }; 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_arg_one_line.rs b/tests/source/fn_arg_one_line.rs deleted file mode 100644 index 23aea2b58f7..00000000000 --- a/tests/source/fn_arg_one_line.rs +++ /dev/null @@ -1,48 +0,0 @@ -// rustfmt-fn_args_layout: Block -// rustfmt-fn_arg_one_line: true - -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(); -} - -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(); -} - -trait Test { - fn foo(a: u8) {} - - fn bar(a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb, c: Cccccccccccccccccc, d: Dddddddddddddddd, e: Eeeeeeeeeeeeeee) -> String {} -} diff --git a/tests/source/fn_args_layout-block.rs b/tests/source/fn_args_layout-block.rs index a5b8e66931f..983ccc60d2b 100644 --- a/tests/source/fn_args_layout-block.rs +++ b/tests/source/fn_args_layout-block.rs @@ -20,8 +20,54 @@ 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(); +} + +fn foo(a: Aaaaaaaaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbbbbb, c: Cccccccccccccccccc, d: Dddddddddddddddd) { + 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 {} } 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_arg_one_line.rs b/tests/target/fn_arg_one_line.rs deleted file mode 100644 index ef5eeea90bd..00000000000 --- a/tests/target/fn_arg_one_line.rs +++ /dev/null @@ -1,85 +0,0 @@ -// rustfmt-fn_args_layout: Block -// rustfmt-fn_arg_one_line: true - -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(); -} - -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(); -} - -trait Test { - fn foo(a: u8) {} - - fn bar( - a: Aaaaaaaaaaaaaa, - b: Bbbbbbbbbbbbbb, - c: Cccccccccccccccccc, - d: Dddddddddddddddd, - e: Eeeeeeeeeeeeeee - ) -> String { - } -} diff --git a/tests/target/fn_args_layout-block.rs b/tests/target/fn_args_layout-block.rs index f18cf2197d8..b2d30018abd 100644 --- a/tests/target/fn_args_layout-block.rs +++ b/tests/target/fn_args_layout-block.rs @@ -4,9 +4,7 @@ fn foo() { foo(); } -fn foo( - a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb -) { +fn foo(a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb) { foo(); } @@ -20,9 +18,7 @@ fn bar( bar(); } -fn foo( - a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb -) -> String { +fn foo(a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb) -> String { foo(); } @@ -36,14 +32,85 @@ 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(); +} + +fn foo(a: Aaaaaaaaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbbbbb, c: Cccccccccccccccccc, d: Dddddddddddddddd) { + 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 { } } 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 { + } +} From 4c9dd747bf3e6f3b3502d75755fc083f466ed3d8 Mon Sep 17 00:00:00 2001 From: Erik Johnston Date: Thu, 7 Apr 2016 20:24:30 +0100 Subject: [PATCH 3/4] Fix long generic indent --- src/items.rs | 5 +- tests/source/fn-custom-6.rs | 2 +- tests/source/fn-custom-7.rs | 2 +- tests/source/fn-custom-8.rs | 50 +++++++++++++++++++ tests/source/fn_args_layout-block.rs | 12 +++-- tests/target/fn-custom-6.rs | 2 +- tests/target/fn-custom-7.rs | 2 +- tests/target/fn-custom-8.rs | 74 ++++++++++++++++++++++++++++ tests/target/fn_args_layout-block.rs | 22 +++++++-- 9 files changed, 158 insertions(+), 13 deletions(-) create mode 100644 tests/source/fn-custom-8.rs create mode 100644 tests/target/fn-custom-8.rs diff --git a/src/items.rs b/src/items.rs index f3ba3872a91..04bb2ec4a4c 100644 --- a/src/items.rs +++ b/src/items.rs @@ -1290,12 +1290,13 @@ 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: {:?}", @@ -1569,6 +1570,8 @@ 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 { 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 983ccc60d2b..b87158a4d7f 100644 --- a/tests/source/fn_args_layout-block.rs +++ b/tests/source/fn_args_layout-block.rs @@ -62,12 +62,16 @@ fn foo(a: Aaaaaaaaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbbbbb, c: Cccccccccccccccccc, d: Dddddddddddddddd) { - foo(); -} - trait Test { fn foo(a: u8) {} 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/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 b2d30018abd..cf380c649c8 100644 --- a/tests/target/fn_args_layout-block.rs +++ b/tests/target/fn_args_layout-block.rs @@ -98,10 +98,6 @@ fn foo(a: Aaaaaaaaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbbbbb, c: Cccccccccccccccccc, d: Dddddddddddddddd) { - foo(); -} - trait Test { fn foo(a: u8) {} @@ -114,3 +110,21 @@ trait Test { ) -> String { } } + +fn foo( + a: Aaaaaaaaaaaaaaaaaaaa, + b: Bbbbbbbbbbbbbbbbb, + c: Cccccccccccccccccc, + d: Dddddddddddddddd +) { + foo(); +} + +fn foo() + -> (Looooooooooooooooooooooooooong, + Reeeeeeeeeeeeeeeeeeeeeeeeeeeeeturn, + iiiiiiiiiiiiiis, + Loooooooooooooooooooooong) +{ + foo(); +} From 6a82a5fb0eb523b8fda8058ddffa31fa6c25ca87 Mon Sep 17 00:00:00 2001 From: Erik Johnston Date: Thu, 7 Apr 2016 20:47:43 +0100 Subject: [PATCH 4/4] Tidy up --- src/items.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/items.rs b/src/items.rs index 04bb2ec4a4c..5b901f2eafc 100644 --- a/src/items.rs +++ b/src/items.rs @@ -1363,15 +1363,16 @@ fn rewrite_fn_base(context: &RewriteContext, // Return type. if !ret_str.is_empty() { 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 } }; - // 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. let ret_indent = if ret_should_indent { let indent = match context.config.fn_return_indent { ReturnIndent::WithWhereClause => indent + 4, @@ -1423,13 +1424,13 @@ fn rewrite_fn_base(context: &RewriteContext, } } - let where_compressed = match context.config.where_density { + 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 where_compressed { + let where_density = if should_compress_where { Density::Compressed } else { Density::Tall