From abf8f43233cd281ef005cda050e887406fa9e1e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96mer=20Sinan=20A=C4=9Facan?= Date: Sat, 7 Oct 2017 15:44:28 +0300 Subject: [PATCH 1/3] Implement match_arm_forces_newline option (#2039) --- src/config.rs | 2 ++ src/expr.rs | 21 +++++++++++++++------ 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/config.rs b/src/config.rs index 8aff7747dd27..8c200302c5b0 100644 --- a/src/config.rs +++ b/src/config.rs @@ -597,6 +597,8 @@ create_config! { the same line with the pattern of arms"; match_block_trailing_comma: bool, false, "Put a trailing comma after a block based match arm (non-block arms are not affected)"; + match_arm_forces_newline: bool, false, + "Force match arm bodies to be in a new lines"; indent_match_arms: bool, true, "Indent match arms instead of keeping them at the same \ indentation level as the match keyword"; match_pattern_separator_break_point: SeparatorPlace, SeparatorPlace::Back, diff --git a/src/expr.rs b/src/expr.rs index 8bef270cf763..51dc7a9bb7a6 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -1695,15 +1695,20 @@ fn rewrite_match_body( is_last: bool, ) -> Option { let (extend, body) = flatten_arm_body(context, body); - - let comma = arm_comma(context.config, body, is_last); - let alt_block_sep = String::from("\n") + &shape.indent.block_only().to_string(context.config); - let alt_block_sep = alt_block_sep.as_str(); let (is_block, is_empty_block) = if let ast::ExprKind::Block(ref block) = body.node { (true, is_empty_block(block, context.codemap)) } else { (false, false) }; + let extend = if context.config.match_arm_forces_newline() { + is_block + } else { + extend + }; + + let comma = arm_comma(context.config, body, is_last); + let alt_block_sep = String::from("\n") + &shape.indent.block_only().to_string(context.config); + let alt_block_sep = alt_block_sep.as_str(); let combine_orig_body = |body_str: &str| { let block_sep = match context.config.control_brace_style() { @@ -1716,7 +1721,11 @@ fn rewrite_match_body( let forbid_same_line = has_guard && pats_str.contains('\n') && !is_empty_block; let next_line_indent = if is_block { - shape.indent + if is_empty_block { + shape.indent.block_indent(context.config) + } else { + shape.indent + } } else { shape.indent.block_indent(context.config) }; @@ -1772,7 +1781,7 @@ fn rewrite_match_body( match rewrite { Some(ref body_str) - if !forbid_same_line + if !forbid_same_line && !context.config.match_arm_forces_newline() && (is_block || (!body_str.contains('\n') && body_str.len() <= body_shape.width)) => { From 84bea05719fef273a04d8fb662ff115ab3abedbb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96mer=20Sinan=20A=C4=9Facan?= Date: Sun, 15 Oct 2017 14:35:20 +0300 Subject: [PATCH 2/3] Add tests --- .../configs-match_arm_forces_newline-true.rs | 20 +++++++++ .../configs-match_arm_forces_newline-true.rs | 44 +++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 tests/source/configs-match_arm_forces_newline-true.rs create mode 100644 tests/target/configs-match_arm_forces_newline-true.rs diff --git a/tests/source/configs-match_arm_forces_newline-true.rs b/tests/source/configs-match_arm_forces_newline-true.rs new file mode 100644 index 000000000000..e9c8d575f1df --- /dev/null +++ b/tests/source/configs-match_arm_forces_newline-true.rs @@ -0,0 +1,20 @@ +// rustfmt-match_arm_forces_newline: true +// rustfmt-wrap_match_arms: false + +// match_arm_forces_newline puts all match arms bodies in a newline and indents +// them. + +fn main() { + match x() { + // a short non-empty block + X0 => { f(); } + // a long non-empty block + X1 => { some.really.long.expression.fooooooooooooooooooooooooooooooooooooooooo().baaaaarrrrrrrrrrrrrrrrrrrrrrrrrr(); } + // an empty block + X2 => {} + // a short non-block + X3 => println!("ok"), + // a long non-block + X4 => foo.bar.baz.test.x.y.z.a.s.d.fasdfasdf.asfads.fasd.fasdfasdf.dfasfdsaf(), + } +} diff --git a/tests/target/configs-match_arm_forces_newline-true.rs b/tests/target/configs-match_arm_forces_newline-true.rs new file mode 100644 index 000000000000..5629a56d6646 --- /dev/null +++ b/tests/target/configs-match_arm_forces_newline-true.rs @@ -0,0 +1,44 @@ +// rustfmt-match_arm_forces_newline: true +// rustfmt-wrap_match_arms: false + +// match_arm_forces_newline puts all match arms bodies in a newline and indents +// them. + +fn main() { + match x() { + // a short non-empty block + X0 => { + f(); + } + // a long non-empty block + X1 => { + some.really + .long + .expression + .fooooooooooooooooooooooooooooooooooooooooo() + .baaaaarrrrrrrrrrrrrrrrrrrrrrrrrr(); + } + // an empty block + X2 => + {} + // a short non-block + X3 => + println!("ok"), + // a long non-block + X4 => + foo.bar + .baz + .test + .x + .y + .z + .a + .s + .d + .fasdfasdf + .asfads + .fasd + .fasdfasdf + .dfasfdsaf(), + } +} From 48bdecf99d33772488fa216ddd3dfe379d9ce4c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96mer=20Sinan=20A=C4=9Facan?= Date: Fri, 27 Oct 2017 08:25:14 +0300 Subject: [PATCH 3/3] Add Configurations.md section --- Configurations.md | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/Configurations.md b/Configurations.md index 16849d11880a..bd67caef09fc 100644 --- a/Configurations.md +++ b/Configurations.md @@ -1229,6 +1229,48 @@ struct Dolor } ``` +## `match_arm_forces_newline` + +Consistently put match arms (block based or not) in a newline. + +- **Default value**: `false` +- **Possible values**: `true`, `false` + +#### `false` (default): + +```rust +match x { + // a non-empty block + X0 => { + f(); + } + // an empty block + X1 => {} + // a non-block + X2 => println!("ok"), +} +``` + +#### `true`: + +```rust +match x { + // a non-empty block + X0 => { + f(); + } + // an empty block + X1 => + {} + // a non-block + X2 => { + println!("ok") + } +} +``` + +See also: [`wrap_match_arms`](#wrap_match_arms). + ## `match_block_trailing_comma` Put a trailing comma after a block based match arm (non-block arms are not affected)