mirror of
https://github.com/rust-lang/rust.git
synced 2025-04-12 20:16:49 +00:00
Add option to disable (un)wrapping of match arms
This commit is contained in:
parent
7b0cceafee
commit
9fe3c08abc
@ -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";
|
||||
}
|
||||
|
25
src/expr.rs
25
src/expr.rs
@ -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))
|
||||
}
|
||||
}
|
||||
|
||||
|
12
tests/source/match-nowrap.rs
Normal file
12
tests/source/match-nowrap.rs
Normal 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),
|
||||
}
|
||||
}
|
13
tests/target/match-nowrap.rs
Normal file
13
tests/target/match-nowrap.rs
Normal 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),
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user