Merge pull request #3576 from rchaser53/issue-3567

add the handling for vec! with paren inside macro
This commit is contained in:
Seiichi Uchida 2019-05-22 07:00:07 +09:00 committed by GitHub
commit 86dad6e3dd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 69 additions and 41 deletions

View file

@ -361,51 +361,35 @@ fn rewrite_macro_inner(
match style {
DelimToken::Paren => {
// Format macro invocation as function call, preserve the trailing
// comma because not all macros support them.
overflow::rewrite_with_parens(
context,
&macro_name,
arg_vec.iter(),
shape,
mac.span,
context.config.width_heuristics().fn_call_width,
if trailing_comma {
Some(SeparatorTactic::Always)
} else {
Some(SeparatorTactic::Never)
},
)
.map(|rw| match position {
MacroPosition::Item => format!("{};", rw),
_ => rw,
})
// Handle special case: `vec!(expr; expr)`
if vec_with_semi {
handle_vec_semi(context, shape, arg_vec, macro_name, style)
} else {
// Format macro invocation as function call, preserve the trailing
// comma because not all macros support them.
overflow::rewrite_with_parens(
context,
&macro_name,
arg_vec.iter(),
shape,
mac.span,
context.config.width_heuristics().fn_call_width,
if trailing_comma {
Some(SeparatorTactic::Always)
} else {
Some(SeparatorTactic::Never)
},
)
.map(|rw| match position {
MacroPosition::Item => format!("{};", rw),
_ => rw,
})
}
}
DelimToken::Bracket => {
// Handle special case: `vec![expr; expr]`
if vec_with_semi {
let mac_shape = shape.offset_left(macro_name.len())?;
// 8 = `vec![]` + `; `
let total_overhead = 8;
let nested_shape = mac_shape.block_indent(context.config.tab_spaces());
let lhs = arg_vec[0].rewrite(context, nested_shape)?;
let rhs = arg_vec[1].rewrite(context, nested_shape)?;
if !lhs.contains('\n')
&& !rhs.contains('\n')
&& lhs.len() + rhs.len() + total_overhead <= shape.width
{
Some(format!("{}[{}; {}]", macro_name, lhs, rhs))
} else {
Some(format!(
"{}[{}{};{}{}{}]",
macro_name,
nested_shape.indent.to_string_with_newline(context.config),
lhs,
nested_shape.indent.to_string_with_newline(context.config),
rhs,
shape.indent.to_string_with_newline(context.config),
))
}
handle_vec_semi(context, shape, arg_vec, macro_name, style)
} else {
// If we are rewriting `vec!` macro or other special macros,
// then we can rewrite this as an usual array literal.
@ -453,6 +437,47 @@ fn rewrite_macro_inner(
}
}
fn handle_vec_semi(
context: &RewriteContext<'_>,
shape: Shape,
arg_vec: Vec<MacroArg>,
macro_name: String,
delim_token: DelimToken,
) -> Option<String> {
let (left, right) = match delim_token {
DelimToken::Paren => ("(", ")"),
DelimToken::Bracket => ("[", "]"),
_ => unreachable!(),
};
let mac_shape = shape.offset_left(macro_name.len())?;
// 8 = `vec![]` + `; ` or `vec!()` + `; `
let total_overhead = 8;
let nested_shape = mac_shape.block_indent(context.config.tab_spaces());
let lhs = arg_vec[0].rewrite(context, nested_shape)?;
let rhs = arg_vec[1].rewrite(context, nested_shape)?;
if !lhs.contains('\n')
&& !rhs.contains('\n')
&& lhs.len() + rhs.len() + total_overhead <= shape.width
{
// macro_name(lhs; rhs) or macro_name[lhs; rhs]
Some(format!("{}{}{}; {}{}", macro_name, left, lhs, rhs, right))
} else {
// macro_name(\nlhs;\nrhs\n) or macro_name[\nlhs;\nrhs\n]
Some(format!(
"{}{}{}{};{}{}{}{}",
macro_name,
left,
nested_shape.indent.to_string_with_newline(context.config),
lhs,
nested_shape.indent.to_string_with_newline(context.config),
rhs,
shape.indent.to_string_with_newline(context.config),
right
))
}
}
pub(crate) fn rewrite_macro_def(
context: &RewriteContext<'_>,
shape: Shape,

View file

@ -0,0 +1,3 @@
fn check() {
vec![vec!(0; 10); 10];
}