]> git.lizzy.rs Git - rust.git/commitdiff
Add option to disable (un)wrapping of match arms
authorMarcus Klaas <mail@marcusklaas.nl>
Fri, 20 Nov 2015 20:50:25 +0000 (21:50 +0100)
committerMarcus Klaas <mail@marcusklaas.nl>
Fri, 20 Nov 2015 20:50:25 +0000 (21:50 +0100)
src/config.rs
src/expr.rs
tests/source/match-nowrap.rs [new file with mode: 0644]
tests/target/match-nowrap.rs [new file with mode: 0644]

index 295c1076b5fcb7c87391b242dfd9d155f26bf84f..c02d618437fa3620648a893630191b0babf2ccfe 100644 (file)
@@ -307,4 +307,5 @@ fn default() -> 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";
 }
index a0d7e08878ff2323b6c85091b09708adbed6947a..29d6d32c0dd47af356b37729bda312504daea07d 100644 (file)
@@ -941,10 +941,11 @@ fn rewrite(&self, context: &RewriteContext, width: usize, offset: Indent) -> Opt
         }
 
         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 @@ fn rewrite(&self, context: &RewriteContext, width: usize, offset: Indent) -> Opt
             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 @@ fn rewrite(&self, context: &RewriteContext, width: usize, offset: Indent) -> Opt
             }
         }
 
-        // 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 @@ fn rewrite(&self, context: &RewriteContext, width: usize, offset: Indent) -> Opt
                                                                       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))
     }
 }
 
diff --git a/tests/source/match-nowrap.rs b/tests/source/match-nowrap.rs
new file mode 100644 (file)
index 0000000..83e0fbb
--- /dev/null
@@ -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),
+    }
+}
diff --git a/tests/target/match-nowrap.rs b/tests/target/match-nowrap.rs
new file mode 100644 (file)
index 0000000..db2a874
--- /dev/null
@@ -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),
+    }
+}