From 4edc6f1a4e069dcb46d1fc583dccefba7e4aae44 Mon Sep 17 00:00:00 2001 From: Erik Johnston Date: Thu, 7 Apr 2016 20:01:16 +0100 Subject: [PATCH] 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 @@ pub enum $e { // 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 @@ fn default() -> 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 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 @@ enum ArgumentKind<'a> { }; 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 { + } +}