From db9d1290254c5703d67fcf4f277b23641fb92d60 Mon Sep 17 00:00:00 2001 From: Ari Koivula Date: Sun, 13 Mar 2016 22:57:03 +0200 Subject: [PATCH 1/4] Add Vertical fn_args_density This adds new option Vertical to fn_args_density, which formats the list vertically no matter what. --- src/config.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/config.rs b/src/config.rs index 448ab3b8d514..32eb9af4b118 100644 --- a/src/config.rs +++ b/src/config.rs @@ -71,6 +71,8 @@ configuration_option_enum! { Density: Tall, // Try to compress if the body is empty. CompressedIfEmpty, + // Place every item on a separate line. + Vertical, } configuration_option_enum! { TypeDensity: @@ -85,6 +87,7 @@ impl Density { match self { Density::Compressed => ListTactic::Mixed, Density::Tall | Density::CompressedIfEmpty => ListTactic::HorizontalVertical, + Density::Vertical => ListTactic::Vertical, } } } From f4171e97ebed0f2e574be0ab5e552fc4b6bc9e96 Mon Sep 17 00:00:00 2001 From: Ari Koivula Date: Sun, 13 Mar 2016 23:02:03 +0200 Subject: [PATCH 2/4] Add test for vertical fn_args_density --- tests/source/fn-custom-7.rs | 19 +++++++++++++++++++ tests/target/fn-custom-7.rs | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 tests/source/fn-custom-7.rs create mode 100644 tests/target/fn-custom-7.rs diff --git a/tests/source/fn-custom-7.rs b/tests/source/fn-custom-7.rs new file mode 100644 index 000000000000..6a56c440d331 --- /dev/null +++ b/tests/source/fn-custom-7.rs @@ -0,0 +1,19 @@ +// rustfmt-fn_args_layout: Block +// rustfmt-fn_args_density: Vertical +// rustfmt-fn_arg_indent: Tabbed +// rustfmt-fn_brace_style: AlwaysNextLine + +// Case with only one variable. +fn foo(a: u8) -> u8 { + bar() +} + +// Case with 2 variables and some pre-comments. +fn foo(a: u8 /* Comment 1 */, b: u8 /* Comment 2 */) -> u8 { + bar() +} + +// Case with 2 variables and some post-comments. +fn foo(/* Comment 1 */ a: u8, /* Comment 2 */ b: u8) -> u8 { + bar() +} diff --git a/tests/target/fn-custom-7.rs b/tests/target/fn-custom-7.rs new file mode 100644 index 000000000000..1c4cbde24b85 --- /dev/null +++ b/tests/target/fn-custom-7.rs @@ -0,0 +1,32 @@ +// rustfmt-fn_args_layout: Block +// rustfmt-fn_args_density: Vertical +// rustfmt-fn_arg_indent: Tabbed +// rustfmt-fn_brace_style: AlwaysNextLine + +// Case with only one variable. +fn foo( + a: u8 +) -> u8 +{ + bar() +} + +// Case with 2 variables and some pre-comments. +fn foo( + a: u8, // Comment 1 + b: u8 // Comment 2 +) -> u8 +{ + bar() +} + +// Case with 2 variables and some post-comments. +fn foo( + // Comment 1 + a: u8, + // Comment 2 + b: u8 +) -> u8 +{ + bar() +} From 0c8640a84f0cf4ce01665aa482f4470edcf12410 Mon Sep 17 00:00:00 2001 From: Ari Koivula Date: Sun, 13 Mar 2016 23:27:13 +0200 Subject: [PATCH 3/4] Fix last comment turning into block comment This fixes the comment after the last argument in an argument list being turned into a block comment when it's not necessary. --- src/items.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/items.rs b/src/items.rs index 9e5b964429e7..f0a7d94ebe78 100644 --- a/src/items.rs +++ b/src/items.rs @@ -1370,13 +1370,18 @@ fn rewrite_args(context: &RewriteContext, _ => multi_line_budget, }; + let end_with_newline = match context.config.fn_args_layout { + StructLitStyle::Block => true, + _ => false, + }; + let fmt = ListFormatting { tactic: tactic, separator: ",", trailing_separator: SeparatorTactic::Never, indent: indent, width: budget, - ends_with_newline: false, + ends_with_newline: end_with_newline, config: context.config, }; From 8d299210a6bee1ead0301073c72b5671ce4d8c84 Mon Sep 17 00:00:00 2001 From: Ari Koivula Date: Mon, 14 Mar 2016 03:53:26 +0200 Subject: [PATCH 4/4] Add independent test for Vertical rustfmt-fn_args_density --- tests/source/fn_args_density-vertical.rs | 33 ++++++++++++++++++++++ tests/target/fn_args_density-vertical.rs | 36 ++++++++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 tests/source/fn_args_density-vertical.rs create mode 100644 tests/target/fn_args_density-vertical.rs diff --git a/tests/source/fn_args_density-vertical.rs b/tests/source/fn_args_density-vertical.rs new file mode 100644 index 000000000000..7dd4dcbfc4dd --- /dev/null +++ b/tests/source/fn_args_density-vertical.rs @@ -0,0 +1,33 @@ +// rustfmt-fn_args_density: Vertical + +// Empty list shoul stay on one line. +fn do_bar( + +) -> u8 { + bar() +} + +// A single argument should stay on the same line. +fn do_bar( + a: u8) -> u8 { + bar() +} + +// Multiple arguments should each get their own line. +fn do_bar(a: u8, mut b: u8, c: &u8, d: &mut u8, closure: &Fn(i32) -> i32) -> i32 { + // This feature should not affect closures. + let bar = |x: i32, y: i32| -> i32 { x + y }; + bar(a, b) +} + +// If the first argument doesn't fit on the same line with the function name, +// the whole list should probably be pushed to the next line with hanging +// indent. That's not what happens though, so check current behaviour instead. +// In any case, it should maintain single argument per line. +fn do_this_that_and_the_other_thing( + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa: u8, + b: u8, c: u8, d: u8) { + this(); + that(); + the_other_thing(); +} diff --git a/tests/target/fn_args_density-vertical.rs b/tests/target/fn_args_density-vertical.rs new file mode 100644 index 000000000000..250d32654e56 --- /dev/null +++ b/tests/target/fn_args_density-vertical.rs @@ -0,0 +1,36 @@ +// rustfmt-fn_args_density: Vertical + +// Empty list shoul stay on one line. +fn do_bar() -> u8 { + bar() +} + +// A single argument should stay on the same line. +fn do_bar(a: u8) -> u8 { + bar() +} + +// Multiple arguments should each get their own line. +fn do_bar(a: u8, + mut b: u8, + c: &u8, + d: &mut u8, + closure: &Fn(i32) -> i32) + -> i32 { + // This feature should not affect closures. + let bar = |x: i32, y: i32| -> i32 { x + y }; + bar(a, b) +} + +// If the first argument doesn't fit on the same line with the function name, +// the whole list should probably be pushed to the next line with hanging +// indent. That's not what happens though, so check current behaviour instead. +// In any case, it should maintain single argument per line. +fn do_this_that_and_the_other_thing(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa: u8, + b: u8, + c: u8, + d: u8) { + this(); + that(); + the_other_thing(); +}