From eeb5599b4b767762a3bdd9f6b9493b05faa34c84 Mon Sep 17 00:00:00 2001 From: topecongiro Date: Fri, 23 Jun 2017 13:40:30 +0900 Subject: [PATCH] Do not add trailing comma inside macro invocation unless there already is --- src/expr.rs | 34 ++++++++++++++++----- tests/target/configs-fn_call_style-block.rs | 2 +- tests/target/macros.rs | 2 +- 3 files changed, 29 insertions(+), 9 deletions(-) diff --git a/src/expr.rs b/src/expr.rs index 4eafd726807..35c028d24db 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -2009,6 +2009,11 @@ pub fn rewrite_call_with_binary_search( 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 { + 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 { 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) diff --git a/tests/target/configs-fn_call_style-block.rs b/tests/target/configs-fn_call_style-block.rs index 55c5ecdf593..75d6b6765fb 100644 --- a/tests/target/configs-fn_call_style-block.rs +++ b/tests/target/configs-fn_call_style-block.rs @@ -33,7 +33,7 @@ fn main() { // nesting macro and function call try!(foo( xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx, - xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx, + xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx )); try!(foo(try!( xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx, diff --git a/tests/target/macros.rs b/tests/target/macros.rs index bd1042638a1..eb95fe4758c 100644 --- a/tests/target/macros.rs +++ b/tests/target/macros.rs @@ -152,7 +152,7 @@ fn issue1739() { b: types::Timestamptz, c: types::Text, d: types::Text, - e: types::Text, + e: types::Text ) );