Add trailing comma to a single arg in multiline

This commit is contained in:
topecongiro 2017-05-23 12:03:04 +09:00
parent b4cd9584b3
commit f83c22f24f
6 changed files with 79 additions and 18 deletions

View File

@ -1655,24 +1655,24 @@ fn rewrite_call_inner<R>(context: &RewriteContext,
shape, shape,
used_width + 2 * paren_overhead, used_width + 2 * paren_overhead,
used_width + paren_overhead) used_width + paren_overhead)
.ok_or(Ordering::Greater)?; .ok_or(Ordering::Greater)?;
let span_lo = context.codemap.span_after(span, "("); let span_lo = context.codemap.span_after(span, "(");
let span = mk_sp(span_lo, span.hi); let span = mk_sp(span_lo, span.hi);
let list_str = rewrite_call_args(context, let (extendable, list_str) = rewrite_call_args(context,
args, args,
span, span,
nested_shape, nested_shape,
one_line_width, one_line_width,
force_trailing_comma) force_trailing_comma)
.ok_or(Ordering::Less)?; .ok_or(Ordering::Less)?;
let arg_one_line_budget = min(one_line_width, context.config.fn_call_width()); let arg_one_line_budget = min(one_line_width, context.config.fn_call_width());
Ok(format!("{}{}", Ok(format!("{}{}",
callee_str, callee_str,
wrap_args_with_parens(context, wrap_args_with_parens(context,
&list_str, &list_str,
is_extendable(args), extendable,
arg_one_line_budget, arg_one_line_budget,
shape, shape,
nested_shape))) nested_shape)))
@ -1684,7 +1684,7 @@ fn rewrite_call_args(context: &RewriteContext,
shape: Shape, shape: Shape,
one_line_width: usize, one_line_width: usize,
force_trailing_comma: bool) force_trailing_comma: bool)
-> Option<String> { -> Option<(bool, String)> {
let items = itemize_list(context.codemap, let items = itemize_list(context.codemap,
args.iter(), args.iter(),
")", ")",
@ -1717,12 +1717,12 @@ fn rewrite_call_args(context: &RewriteContext,
shape.block() shape.block()
}; };
let rewrite = args.last().unwrap().rewrite(context, arg_shape); let rewrite = args.last().unwrap().rewrite(context, arg_shape);
swap(&mut item_vec[args.len() - 1].item, &mut orig_last);
if let Some(rewrite) = rewrite { if let Some(rewrite) = rewrite {
let rewrite_first_line = Some(rewrite[..first_line_width(&rewrite)].to_owned()); let rewrite_first_line = Some(rewrite[..first_line_width(&rewrite)].to_owned());
placeholder = Some(rewrite); placeholder = Some(rewrite);
swap(&mut item_vec[args.len() - 1].item, &mut orig_last);
item_vec[args.len() - 1].item = rewrite_first_line; item_vec[args.len() - 1].item = rewrite_first_line;
} }
} }
@ -1765,18 +1765,27 @@ fn rewrite_call_args(context: &RewriteContext,
config: context.config, config: context.config,
}; };
let args_in_single_line =
item_vec
.iter()
.rev()
.skip(1)
.all(|item| item.item.as_ref().map_or(false, |s| !s.contains('\n')));
match write_list(&item_vec, &fmt) { match write_list(&item_vec, &fmt) {
// If arguments do not fit in a single line and do not contain newline, // If arguments do not fit in a single line and do not contain newline,
// try to put it on the next line. Try this only when we are in block mode // try to put it on the next line. Try this only when we are in block mode
// and not rewriting macro. // and not rewriting macro.
Some(ref s) if context.config.fn_call_style() == IndentStyle::Block && Some(ref s) if context.config.fn_call_style() == IndentStyle::Block &&
!context.inside_macro && !context.inside_macro &&
(first_line_width(s) > one_line_width || (!can_be_overflowed(context, args) && args.len() == 1 && s.contains('\n') ||
first_line_width(s) > one_line_width ||
first_line_width(s) > context.config.fn_call_width()) => { first_line_width(s) > context.config.fn_call_width()) => {
fmt.trailing_separator = SeparatorTactic::Vertical; fmt.trailing_separator = SeparatorTactic::Vertical;
write_list(&item_vec, &fmt) fmt.tactic = DefinitiveListTactic::Vertical;
write_list(&item_vec, &fmt).map(|rw| (false, rw))
} }
rewrite @ _ => rewrite, rewrite @ _ => rewrite.map(|rw| (args_in_single_line && is_extendable(args), rw)),
} }
} }
@ -2091,7 +2100,7 @@ pub fn rewrite_tuple(context: &RewriteContext,
return rewrite_tuple_type(context, items.iter().map(|x| &**x), span, shape); return rewrite_tuple_type(context, items.iter().map(|x| &**x), span, shape);
} }
// We use the same rule as funcation call for rewriting tuple with multiple expressions. // We use the same rule as funcation call for rewriting tuple.
// 1 = "," // 1 = ","
rewrite_call_inner(context, rewrite_call_inner(context,
&String::new(), &String::new(),
@ -2100,7 +2109,7 @@ pub fn rewrite_tuple(context: &RewriteContext,
span, span,
shape, shape,
items.len() == 1) items.len() == 1)
.ok() .ok()
} }
pub fn rewrite_unary_prefix<R: Rewrite>(context: &RewriteContext, pub fn rewrite_unary_prefix<R: Rewrite>(context: &RewriteContext,

View File

@ -4,4 +4,5 @@
// rustfmt should not add trailing comma when rewriting macro. See #1528. // rustfmt should not add trailing comma when rewriting macro. See #1528.
fn a() { fn a() {
panic!("this is a long string that goes past the maximum line length causing rustfmt to insert a comma here:"); panic!("this is a long string that goes past the maximum line length causing rustfmt to insert a comma here:");
foo(oooptoptoptoptptooptoptoptoptptooptoptoptoptptoptoptoptoptpt());
} }

View File

@ -191,6 +191,19 @@ fn combine_block() {
}, },
); );
do_thing(
x,
(
1,
2,
3,
|param| {
action();
foo(param)
},
),
);
Ok( Ok(
some_function( some_function(
lllllllllong_argument_one, lllllllllong_argument_one,
@ -226,6 +239,21 @@ fn combine_block() {
long_argument_two, long_argument_two,
long_argument_three, long_argument_three,
)), )),
Some(x) => |x| {
let y = x + 1;
let z = y + 1;
z
},
Some(x) => (1, 2, |x| {
let y = x + 1;
let z = y + 1;
z
}),
Some(x) => SomeStruct {
f1: long_argument_one,
f2: long_argument_two,
f3: long_argument_three,
},
None => Ok(SomeStruct { None => Ok(SomeStruct {
f1: long_argument_one, f1: long_argument_one,
f2: long_argument_two, f2: long_argument_two,

View File

@ -4,4 +4,7 @@
// rustfmt should not add trailing comma when rewriting macro. See #1528. // rustfmt should not add trailing comma when rewriting macro. See #1528.
fn a() { fn a() {
panic!("this is a long string that goes past the maximum line length causing rustfmt to insert a comma here:"); panic!("this is a long string that goes past the maximum line length causing rustfmt to insert a comma here:");
foo(
oooptoptoptoptptooptoptoptoptptooptoptoptoptptoptoptoptoptpt(),
);
} }

View File

@ -36,7 +36,7 @@ fn issue1420() {
r#" r#"
# Getting started # Getting started
... ...
"# "#,
) )
.running(waltz) .running(waltz)
} }

View File

@ -102,7 +102,7 @@ fn arrays() {
Weighted { weight: 1, item: 1 }, Weighted { weight: 1, item: 1 },
Weighted { weight: x, item: 2 }, Weighted { weight: x, item: 2 },
Weighted { weight: 1, item: 3 }, Weighted { weight: 1, item: 3 },
] ],
); );
let z = [ let z = [
@ -257,6 +257,11 @@ fn combine_block() {
foo(param) foo(param)
}); });
do_thing(x, (1, 2, 3, |param| {
action();
foo(param)
}));
Ok(some_function( Ok(some_function(
lllllllllong_argument_one, lllllllllong_argument_one,
lllllllllong_argument_two, lllllllllong_argument_two,
@ -286,6 +291,21 @@ fn combine_block() {
long_argument_two, long_argument_two,
long_argument_three, long_argument_three,
)), )),
Some(x) => |x| {
let y = x + 1;
let z = y + 1;
z
},
Some(x) => (1, 2, |x| {
let y = x + 1;
let z = y + 1;
z
}),
Some(x) => SomeStruct {
f1: long_argument_one,
f2: long_argument_two,
f3: long_argument_three,
},
None => Ok(SomeStruct { None => Ok(SomeStruct {
f1: long_argument_one, f1: long_argument_one,
f2: long_argument_two, f2: long_argument_two,