Add option to disable (un)wrapping of match arms

This commit is contained in:
Marcus Klaas 2015-11-20 21:50:25 +01:00
parent 7b0cceafee
commit 9fe3c08abc
4 changed files with 43 additions and 8 deletions

View File

@ -307,4 +307,5 @@ create_config! {
take_source_hints: bool, true, "Retain some formatting characteristics from the source code";
hard_tabs: bool, false, "Use tab characters for indentation, spaces for alignment";
wrap_comments: bool, false, "Break comments to fit on the line";
wrap_match_arms: bool, true, "Wrap multiline match arms in blocks";
}

View File

@ -941,10 +941,11 @@ impl Rewrite for ast::Arm {
}
let body = match **body {
ast::Expr { node: ast::ExprBlock(ref b), .. } if !is_unsafe_block(b) &&
is_simple_block(b,
context.codemap) => {
b.expr.as_ref().map(|e| &**e).unwrap()
ast::Expr { node: ast::ExprBlock(ref block), .. } if !is_unsafe_block(block) &&
is_simple_block(block,
context.codemap) &&
context.config.wrap_match_arms => {
block.expr.as_ref().map(|e| &**e).unwrap()
}
ref x => x,
};
@ -959,7 +960,8 @@ impl Rewrite for ast::Arm {
let rewrite = nop_block_collapse(body.rewrite(context, budget, offset), budget);
match rewrite {
Some(ref body_str) if !body_str.contains('\n') || comma.is_empty() => {
Some(ref body_str) if !body_str.contains('\n') || !context.config.wrap_match_arms ||
comma.is_empty() => {
return Some(format!("{}{} => {}{}",
attr_str.trim_left(),
pats_str,
@ -970,7 +972,7 @@ impl Rewrite for ast::Arm {
}
}
// FIXME: we're doing a second rewrite of the expr -- this may not be
// FIXME: we're doing a second rewrite of the expr; This may not be
// necessary.
let body_budget = try_opt!(width.checked_sub(context.config.tab_spaces));
let indent = context.block_indent.block_indent(context.config);
@ -980,13 +982,20 @@ impl Rewrite for ast::Arm {
indent),
body_budget));
let indent_str = offset.block_indent(context.config).to_string(context.config);
let (body_prefix, body_suffix) = if context.config.wrap_match_arms {
(" {", "}")
} else {
("", "")
};
Some(format!("{}{} => {{\n{}{}\n{}}}",
Some(format!("{}{} =>{}\n{}{}\n{}{}",
attr_str.trim_left(),
pats_str,
body_prefix,
indent_str,
next_line_body,
offset.to_string(context.config)))
offset.to_string(context.config),
body_suffix))
}
}

View File

@ -0,0 +1,12 @@
// rustfmt-wrap_match_arms: false
// Match expressions, no unwrapping of block arms or wrapping of multiline
// expressions.
fn foo() {
match x {
a => { foo() }
b =>
(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb),
}
}

View File

@ -0,0 +1,13 @@
// rustfmt-wrap_match_arms: false
// Match expressions, no unwrapping of block arms or wrapping of multiline
// expressions.
fn foo() {
match x {
a => {
foo()
}
b => (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb),
}
}