about summary refs log tree commit diff
path: root/src/expr.rs
diff options
context:
space:
mode:
authorMarcus Klaas <mail@marcusklaas.nl>2016-05-09 20:11:25 +0200
committerMarcus Klaas <mail@marcusklaas.nl>2016-05-12 21:15:06 +0200
commitcd158cecc87cb2d4d63aecf8854952370cb9ea4b (patch)
tree8d1cc8700cd73e5892d7bb8208ee067d5c3a2dbb /src/expr.rs
parentee7b5805fcb1e4bc6334b7a6a814769d4606541d (diff)
Add try macro to try shorthand conversion tests
Diffstat (limited to 'src/expr.rs')
-rw-r--r--src/expr.rs52
1 files changed, 32 insertions, 20 deletions
diff --git a/src/expr.rs b/src/expr.rs
index ba2ed7aec09..1edacbf765d 100644
--- a/src/expr.rs
+++ b/src/expr.rs
@@ -148,7 +148,7 @@ impl Rewrite for ast::Expr {
             ast::ExprKind::Closure(capture, ref fn_decl, ref body, _) => {
                 rewrite_closure(capture, fn_decl, body, self.span, context, width, offset)
             }
-            // ast::ExprKind::Try(..) |
+            ast::ExprKind::Try(..) |
             ast::ExprKind::Field(..) |
             ast::ExprKind::TupField(..) |
             ast::ExprKind::MethodCall(..) => rewrite_chain(self, context, width, offset),
@@ -205,15 +205,10 @@ impl Rewrite for ast::Expr {
                     (None, None) => wrap_str(delim.into(), context.config.max_width, width, offset),
                 }
             }
-            ast::ExprKind::Try(ref expr) => {
-                rewrite_unary_suffix(context, "?", &**expr, width, offset)
-            }
             // We do not format these expressions yet, but they should still
             // satisfy our width restrictions.
             ast::ExprKind::InPlace(..) |
-            ast::ExprKind::InlineAsm(..) |
-            // TODO(#848): Handle type ascription
-            ast::ExprKind::Type(_, _) => {
+            ast::ExprKind::InlineAsm(..) => {
                 wrap_str(context.snippet(self.span),
                          context.config.max_width,
                          width,
@@ -1832,24 +1827,41 @@ pub fn rewrite_assign_rhs<S: Into<String>>(context: &RewriteContext,
     let max_width = try_opt!(width.checked_sub(last_line_width + 1));
     let rhs = ex.rewrite(&context, max_width, offset + last_line_width + 1);
 
+    fn count_line_breaks(src: &str) -> usize {
+        src.chars().filter(|&x| x == '\n').count()
+    }
+
     match rhs {
-        Some(new_str) => {
+        Some(ref new_str) if count_line_breaks(new_str) < 2 => {
             result.push(' ');
-            result.push_str(&new_str)
+            result.push_str(new_str);
         }
-        None => {
-            // Expression did not fit on the same line as the identifier. Retry
-            // on the next line.
+        _ => {
+            // Expression did not fit on the same line as the identifier or is
+            // at least three lines big. Try splitting the line and see
+            // if that works better.
             let new_offset = offset.block_indent(context.config);
-            result.push_str(&format!("\n{}", new_offset.to_string(context.config)));
-
-            // FIXME: we probably should related max_width to width instead of
-            // config.max_width where is the 1 coming from anyway?
-            let max_width = try_opt!(context.config.max_width.checked_sub(new_offset.width() + 1));
+            let max_width = try_opt!((width + offset.width()).checked_sub(new_offset.width()));
             let inner_context = context.nested_context();
-            let rhs = ex.rewrite(&inner_context, max_width, new_offset);
-
-            result.push_str(&&try_opt!(rhs));
+            let new_rhs = ex.rewrite(&inner_context, max_width, new_offset);
+
+            // FIXME: DRY!
+            match (rhs, new_rhs) {
+                (Some(ref orig_rhs), Some(ref replacement_rhs))
+                    if count_line_breaks(orig_rhs) > count_line_breaks(replacement_rhs) + 1 => {
+                    result.push_str(&format!("\n{}", new_offset.to_string(context.config)));
+                    result.push_str(replacement_rhs);
+                }
+                (None, Some(ref final_rhs)) => {
+                    result.push_str(&format!("\n{}", new_offset.to_string(context.config)));
+                    result.push_str(final_rhs);
+                }
+                (None, None) => return None,
+                (Some(ref orig_rhs), _) => {
+                    result.push(' ');
+                    result.push_str(orig_rhs);
+                }
+            }
         }
     }