mk_sp(context.codemap.span_after(expr.span, "["), expr.span.hi),
context,
shape,
+ false,
)
}
ast::ExprKind::Lit(ref l) => {
span: Span,
context: &RewriteContext,
shape: Shape,
+ trailing_comma: bool,
) -> Option<String>
where
I: Iterator<Item = &'a ast::Expr>,
let fmt = ListFormatting {
tactic: tactic,
separator: ",",
- trailing_separator: SeparatorTactic::Never,
+ trailing_separator: if trailing_comma {
+ SeparatorTactic::Always
+ } else if context.inside_macro || context.config.array_layout() == IndentStyle::Visual {
+ SeparatorTactic::Never
+ } else {
+ SeparatorTactic::Vertical
+ },
shape: nested_shape,
ends_with_newline: false,
config: context.config,
}
} else {
format!(
- "[\n{}{},\n{}]",
+ "[\n{}{}\n{}]",
nested_shape.indent.to_string(context.config),
list_str,
shape.block().indent.to_string(context.config)
use Shape;
use codemap::SpanUtils;
use rewrite::{Rewrite, RewriteContext};
-use expr::{rewrite_call, rewrite_array};
+use expr::{rewrite_call_inner, rewrite_array};
use comment::{FindUncommented, contains_comment};
use utils::mk_sp;
let mut parser = new_parser_from_tts(context.parse_session, ts.trees().collect());
let mut expr_vec = Vec::new();
let mut vec_with_semi = false;
+ let mut trailing_comma = false;
if MacroStyle::Braces != style {
loop {
parser.bump();
if parser.token == Token::Eof {
- // vec! is a special case of bracket macro which should be formated as an array.
- if macro_name == "vec!" {
- break;
- } else {
- return None;
- }
+ trailing_comma = true;
+ break;
}
}
}
MacroStyle::Parens => {
// Format macro invocation as function call, forcing no trailing
// comma because not all macros support them.
- rewrite_call(context, ¯o_name, &expr_vec, mac.span, shape).map(
- |rw| match position {
- MacroPosition::Item => format!("{};", rw),
- _ => rw,
- },
- )
+ let rw = rewrite_call_inner(
+ context,
+ ¯o_name,
+ &expr_vec.iter().map(|e| &**e).collect::<Vec<_>>()[..],
+ mac.span,
+ shape,
+ context.config.fn_call_width(),
+ trailing_comma,
+ );
+ rw.ok().map(|rw| match position {
+ MacroPosition::Item => format!("{};", rw),
+ _ => rw,
+ })
}
MacroStyle::Brackets => {
let mac_shape = try_opt!(shape.offset_left(macro_name.len()));
))
}
} else {
- // Format macro invocation as array literal.
+ // If we are rewriting `vec!` macro or other special macros,
+ // then we can rewrite this as an usual array literal.
+ // Otherwise, we must preserve the original existence of trailing comma.
+ if FORCED_BRACKET_MACROS.contains(&¯o_name.as_str()) {
+ context.inside_macro = false;
+ trailing_comma = false;
+ }
let rewrite = try_opt!(rewrite_array(
expr_vec.iter().map(|x| &**x),
mk_sp(
),
context,
mac_shape,
+ trailing_comma,
));
Some(format!("{}{}", macro_name, rewrite))