Merge pull request #1744 from topecongiro/macro-arguments-trailing-comma

Do not add trailing comma inside macro invocation unless there alreay is
This commit is contained in:
Nick Cameron 2017-06-23 17:02:45 +12:00 committed by GitHub
commit 092f31b72a
3 changed files with 29 additions and 9 deletions

View file

@ -2009,6 +2009,11 @@ pub fn rewrite_call_with_binary_search<R>(
where where
R: Rewrite, R: Rewrite,
{ {
let force_trailing_comma = if context.inside_macro {
span_ends_with_comma(context, span)
} else {
false
};
let closure = |callee_max_width| { let closure = |callee_max_width| {
// FIXME using byte lens instead of char lens (and probably all over the // FIXME using byte lens instead of char lens (and probably all over the
// place too) // place too)
@ -2027,7 +2032,7 @@ where
span, span,
shape, shape,
context.config.fn_call_width(), context.config.fn_call_width(),
false, force_trailing_comma,
) )
}; };
@ -2041,6 +2046,11 @@ pub fn rewrite_call(
span: Span, span: Span,
shape: Shape, shape: Shape,
) -> Option<String> { ) -> Option<String> {
let force_trailing_comma = if context.inside_macro {
span_ends_with_comma(context, span)
} else {
false
};
rewrite_call_inner( rewrite_call_inner(
context, context,
&callee, &callee,
@ -2048,7 +2058,7 @@ pub fn rewrite_call(
span, span,
shape, shape,
context.config.fn_call_width(), context.config.fn_call_width(),
false, force_trailing_comma,
).ok() ).ok()
} }
@ -2155,15 +2165,13 @@ fn rewrite_call_args<'a, T>(
where where
T: Rewrite + Spanned + ToExpr + 'a, T: Rewrite + Spanned + ToExpr + 'a,
{ {
let mut item_context = context.clone();
item_context.inside_macro = false;
let items = itemize_list( let items = itemize_list(
context.codemap, context.codemap,
args.iter(), args.iter(),
")", ")",
|item| item.span().lo, |item| item.span().lo,
|item| item.span().hi, |item| item.span().hi,
|item| item.rewrite(&item_context, shape), |item| item.rewrite(context, shape),
span.lo, span.lo,
span.hi, span.hi,
); );
@ -2173,7 +2181,7 @@ where
// indentation. If its first line fits on one line with the other arguments, // indentation. If its first line fits on one line with the other arguments,
// we format the function arguments horizontally. // we format the function arguments horizontally.
let tactic = try_overflow_last_arg( let tactic = try_overflow_last_arg(
&item_context, context,
&mut item_vec, &mut item_vec,
&args[..], &args[..],
shape, shape,
@ -2444,6 +2452,13 @@ pub fn wrap_args_with_parens(
} }
} }
fn span_ends_with_comma(context: &RewriteContext, span: Span) -> bool {
let snippet = context.snippet(span);
snippet
.trim_right_matches(|c: char| c == ')' || c.is_whitespace())
.ends_with(',')
}
fn rewrite_paren(context: &RewriteContext, subexpr: &ast::Expr, shape: Shape) -> Option<String> { fn rewrite_paren(context: &RewriteContext, subexpr: &ast::Expr, shape: Shape) -> Option<String> {
debug!("rewrite_paren, shape: {:?}", shape); debug!("rewrite_paren, shape: {:?}", shape);
let paren_overhead = paren_overhead(context); let paren_overhead = paren_overhead(context);
@ -2733,6 +2748,11 @@ where
debug!("rewrite_tuple {:?}", shape); debug!("rewrite_tuple {:?}", shape);
if context.use_block_indent() { if context.use_block_indent() {
// We use the same rule as funcation call for rewriting tuple. // We use the same rule as funcation call for rewriting tuple.
let force_trailing_comma = if context.inside_macro {
span_ends_with_comma(context, span)
} else {
items.len() == 1
};
rewrite_call_inner( rewrite_call_inner(
context, context,
&String::new(), &String::new(),
@ -2740,7 +2760,7 @@ where
span, span,
shape, shape,
context.config.fn_call_width(), context.config.fn_call_width(),
items.len() == 1, force_trailing_comma,
).ok() ).ok()
} else { } else {
rewrite_tuple_in_visual_indent_style(context, items, span, shape) rewrite_tuple_in_visual_indent_style(context, items, span, shape)

View file

@ -33,7 +33,7 @@ fn main() {
// nesting macro and function call // nesting macro and function call
try!(foo( try!(foo(
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx, xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx,
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx, xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
)); ));
try!(foo(try!( try!(foo(try!(
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx, xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx,

View file

@ -152,7 +152,7 @@ fn issue1739() {
b: types::Timestamptz, b: types::Timestamptz,
c: types::Text, c: types::Text,
d: types::Text, d: types::Text,
e: types::Text, e: types::Text
) )
); );