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`.
This commit is contained in:
Erik Johnston 2016-04-02 00:25:35 +01:00
parent c3cef52cde
commit 57847e451a
6 changed files with 160 additions and 7 deletions

View File

@ -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

View File

@ -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() {

View File

@ -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 {}
}

View File

@ -1,5 +1,8 @@
// rustfmt-fn_args_layout: Block
fn foo() {
foo();
}
fn foo(a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb) {
foo();

View File

@ -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 {
}
}

View File

@ -1,5 +1,8 @@
// rustfmt-fn_args_layout: Block
fn foo() {
foo();
}
fn foo(
a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb