about summary refs log tree commit diff
path: root/src/expr.rs
diff options
context:
space:
mode:
authorNick Cameron <nrc@ncameron.org>2017-06-13 11:58:16 +1200
committerGitHub <noreply@github.com>2017-06-13 11:58:16 +1200
commit5531a93b573c14ca4e6d8a8ad665cb0823abea81 (patch)
treebead914b52b7b8fbb1dae7317d595e3c73ba3364 /src/expr.rs
parent5d78d1fe00addaf49a9ba470405aa031b12d6c51 (diff)
parent57fc39305dc765d4454869ab6a6b9667c17b6e57 (diff)
Merge pull request #1657 from topecongiro/issue-1656
Put match arm's guard on new line if it exceeds max width
Diffstat (limited to 'src/expr.rs')
-rw-r--r--src/expr.rs43
1 files changed, 15 insertions, 28 deletions
diff --git a/src/expr.rs b/src/expr.rs
index 40c678174a8..ef47d0fd6c6 100644
--- a/src/expr.rs
+++ b/src/expr.rs
@@ -1321,20 +1321,13 @@ impl Rewrite for ast::Arm {
             ref body,
         } = self;
 
-        // FIXME this is all a bit grotty, would be nice to abstract out the
-        // treatment of attributes.
         let attr_str = if !attrs.is_empty() {
-            // We only use this visitor for the attributes, should we use it for
-            // more?
-            let mut attr_visitor = FmtVisitor::from_codemap(context.parse_session, context.config);
-            attr_visitor.block_indent = shape.indent.block_only();
-            attr_visitor.last_pos = attrs[0].span.lo;
-            if attr_visitor.visit_attrs(attrs) {
-                // Attributes included a skip instruction.
+            if contains_skip(attrs) {
                 return None;
             }
-            attr_visitor.format_missing(pats[0].span.lo);
-            attr_visitor.buffer.to_string()
+            format!("{}\n{}",
+                    try_opt!(attrs.rewrite(context, shape)),
+                    shape.indent.to_string(context.config))
         } else {
             String::new()
         };
@@ -1502,28 +1495,22 @@ fn rewrite_guard(context: &RewriteContext,
     if let Some(ref guard) = *guard {
         // First try to fit the guard string on the same line as the pattern.
         // 4 = ` if `, 5 = ` => {`
-        let overhead = pattern_width + 4 + 5;
-        if overhead < shape.width {
-            let cond_shape = shape
-                .shrink_left(pattern_width + 4)
-                .unwrap()
-                .sub_width(5)
-                .unwrap();
-            let cond_str = guard.rewrite(context, cond_shape);
-            if let Some(cond_str) = cond_str {
+        if let Some(cond_shape) = shape
+               .shrink_left(pattern_width + 4)
+               .and_then(|s| s.sub_width(5)) {
+            if let Some(cond_str) = guard
+                   .rewrite(context, cond_shape)
+                   .and_then(|s| s.rewrite(context, cond_shape)) {
                 return Some(format!(" if {}", cond_str));
             }
         }
 
         // Not enough space to put the guard after the pattern, try a newline.
-        let overhead = shape.indent.block_indent(context.config).width() + 4 + 5;
-        if overhead < shape.width {
-            let cond_str = guard.rewrite(context,
-                                         Shape::legacy(shape.width - overhead,
-                                                       // 3 == `if `
-                                                       shape.indent.block_indent(context.config) +
-                                                       3));
-            if let Some(cond_str) = cond_str {
+        // 3 == `if `
+        if let Some(cond_shape) = Shape::indented(shape.indent.block_indent(context.config) + 3,
+                                                  context.config)
+               .sub_width(3) {
+            if let Some(cond_str) = guard.rewrite(context, cond_shape) {
                 return Some(format!("\n{}if {}",
                                     shape
                                         .indent