From c7e9bcadaab1665b5e46797984c6a8ab3885718a Mon Sep 17 00:00:00 2001
From: Seiichi Uchida <topecongiro@localhost.localdomain>
Date: Mon, 1 May 2017 18:52:43 +0900
Subject: [PATCH] Use block indent when visual indent exceeds max_width

---
 src/expr.rs               |  8 +++++---
 tests/source/large_vec.rs | 29 +++++++++++++++++++++++++++++
 tests/target/large_vec.rs | 29 +++++++++++++++++++++++++++++
 3 files changed, 63 insertions(+), 3 deletions(-)
 create mode 100644 tests/source/large_vec.rs
 create mode 100644 tests/target/large_vec.rs

diff --git a/src/expr.rs b/src/expr.rs
index 2723c4099c1..f78dd7bbc5f 100644
--- a/src/expr.rs
+++ b/src/expr.rs
@@ -2091,13 +2091,15 @@ pub fn rewrite_assign_rhs<S: Into<String>>(context: &RewriteContext,
             let new_offset = shape.indent.block_indent(context.config);
             let max_width = try_opt!((shape.width + shape.indent.width())
                                          .checked_sub(new_offset.width()));
-            let new_rhs = ex.rewrite(context, Shape::legacy(max_width, new_offset));
+            let new_shape = Shape::legacy(max_width, new_offset);
+            let new_rhs = ex.rewrite(context, new_shape);
 
             // 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 => {
+                    if count_line_breaks(orig_rhs) > count_line_breaks(replacement_rhs) + 1 ||
+                       (orig_rhs.rewrite(context, shape).is_none() &&
+                        replacement_rhs.rewrite(context, new_shape).is_some()) => {
                     result.push_str(&format!("\n{}", new_offset.to_string(context.config)));
                     result.push_str(replacement_rhs);
                 }
diff --git a/tests/source/large_vec.rs b/tests/source/large_vec.rs
new file mode 100644
index 00000000000..34d5bf39954
--- /dev/null
+++ b/tests/source/large_vec.rs
@@ -0,0 +1,29 @@
+// See #1470.
+
+impl Environment {
+    pub fn new_root() -> Rc<RefCell<Environment>> {
+        let mut env = Environment::new();
+        let builtin_functions = &[("println",
+                                   Function::NativeVoid(CallSign {
+                                                            num_params: 0,
+                                                            variadic: true,
+                                                            param_types: vec![],
+                                                        },
+                                                        native_println)),
+                                  ("run_http_server",
+                                   Function::NativeVoid(CallSign {
+                                                            num_params: 1,
+                                                            variadic: false,
+                                                            param_types:
+                                                                vec![Some(ConstraintType::Function)],
+                                                        },
+                                                        native_run_http_server)),
+                                  ("len",
+                                   Function::NativeReturning(CallSign {
+                                                                 num_params: 1,
+                                                                 variadic: false,
+                                                                 param_types: vec![None],
+                                                             },
+                                                             native_len))];
+    }
+}
diff --git a/tests/target/large_vec.rs b/tests/target/large_vec.rs
new file mode 100644
index 00000000000..44f6d52650c
--- /dev/null
+++ b/tests/target/large_vec.rs
@@ -0,0 +1,29 @@
+// See #1470.
+
+impl Environment {
+    pub fn new_root() -> Rc<RefCell<Environment>> {
+        let mut env = Environment::new();
+        let builtin_functions =
+            &[("println",
+               Function::NativeVoid(CallSign {
+                                        num_params: 0,
+                                        variadic: true,
+                                        param_types: vec![],
+                                    },
+                                    native_println)),
+              ("run_http_server",
+               Function::NativeVoid(CallSign {
+                                        num_params: 1,
+                                        variadic: false,
+                                        param_types: vec![Some(ConstraintType::Function)],
+                                    },
+                                    native_run_http_server)),
+              ("len",
+               Function::NativeReturning(CallSign {
+                                             num_params: 1,
+                                             variadic: false,
+                                             param_types: vec![None],
+                                         },
+                                         native_len))];
+    }
+}