Generalize rewrite_array() to types other than ast::Expr

This commit is contained in:
topecongiro 2017-11-30 22:13:28 +09:00
parent 5aaa00a929
commit 65cb9b4649
2 changed files with 9 additions and 22 deletions

View File

@ -63,7 +63,7 @@ pub fn format_expr(
let expr_rw = match expr.node {
ast::ExprKind::Array(ref expr_vec) => rewrite_array(
expr_vec.iter().map(|e| &**e),
&ptr_vec_to_ref_vec(expr_vec),
mk_sp(context.codemap.span_after(expr.span, "["), expr.span.hi()),
context,
shape,
@ -397,16 +397,13 @@ where
))
}
pub fn rewrite_array<'a, I>(
expr_iter: I,
pub fn rewrite_array<T: Rewrite + Spanned + ToExpr>(
exprs: &[&T],
span: Span,
context: &RewriteContext,
shape: Shape,
trailing_comma: bool,
) -> Option<String>
where
I: Iterator<Item = &'a ast::Expr>,
{
) -> Option<String> {
let bracket_size = if context.config.spaces_within_parens_and_brackets() {
2 // "[ "
} else {
@ -426,11 +423,11 @@ where
let items = itemize_list(
context.codemap,
expr_iter,
exprs.iter(),
"]",
",",
|item| item.span.lo(),
|item| item.span.hi(),
|item| item.span().lo(),
|item| item.span().hi(),
|item| item.rewrite(context, nested_shape),
span.lo(),
span.hi(),

View File

@ -255,24 +255,14 @@ pub fn rewrite_macro(
trailing_comma = false;
}
// Convert `MacroArg` into `ast::Expr`, as `rewrite_array` only accepts the latter.
let expr_vec: Vec<_> = arg_vec
.iter()
.filter_map(|e| match *e {
MacroArg::Expr(ref e) => Some(e.clone()),
_ => None,
})
.collect();
if expr_vec.len() != arg_vec.len() {
return Some(context.snippet(mac.span));
}
let sp = mk_sp(
context
.codemap
.span_after(mac.span, original_style.opener()),
mac.span.hi() - BytePos(1),
);
let rewrite =
rewrite_array(expr_vec.iter(), sp, context, mac_shape, trailing_comma)?;
let arg_vec = &arg_vec.iter().map(|e| &*e).collect::<Vec<_>>()[..];
let rewrite = rewrite_array(arg_vec, sp, context, mac_shape, trailing_comma)?;
Some(format!("{}{}", macro_name, rewrite))
}