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

View file

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

View file

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