]> git.lizzy.rs Git - rust.git/commitdiff
Put multi-lined index on the next line if it fits in one line
authortopecongiro <seuchida@gmail.com>
Sun, 25 Jun 2017 22:57:06 +0000 (07:57 +0900)
committertopecongiro <seuchida@gmail.com>
Sun, 25 Jun 2017 22:57:06 +0000 (07:57 +0900)
src/expr.rs
tests/source/expr.rs
tests/target/expr.rs

index 35c028d24db4efe77986e8eea07b694a4e3a886f..12846049406a6719214e98fb24db99c96b02ba71 100644 (file)
@@ -2501,31 +2501,53 @@ fn rewrite_index(
     };
 
     let offset = expr_str.len() + lbr.len();
-    if let Some(index_shape) = shape.visual_indent(offset).sub_width(offset + rbr.len()) {
-        if let Some(index_str) = index.rewrite(context, index_shape) {
+    let orig_index_rw = shape
+        .visual_indent(offset)
+        .sub_width(offset + rbr.len())
+        .and_then(|index_shape| index.rewrite(context, index_shape));
+
+    // Return if everything fits in a single line.
+    match orig_index_rw {
+        Some(ref index_str) if !index_str.contains('\n') => {
             return Some(format!("{}{}{}{}", expr_str, lbr, index_str, rbr));
         }
+        _ => (),
     }
 
+    // Try putting index on the next line and see if it fits in a single line.
     let indent = shape.indent.block_indent(&context.config);
-    let indent = indent.to_string(&context.config);
-    // FIXME this is not right, since we don't take into account that shape.width
-    // might be reduced from max_width by something on the right.
-    let budget = try_opt!(
-        context
-            .config
-            .max_width()
-            .checked_sub(indent.len() + lbr.len() + rbr.len())
-    );
-    let index_str = try_opt!(index.rewrite(context, Shape::legacy(budget, shape.indent)));
-    Some(format!(
-        "{}\n{}{}{}{}",
-        expr_str,
-        indent,
-        lbr,
-        index_str,
-        rbr
-    ))
+    let rhs_overhead = context
+        .config
+        .max_width()
+        .checked_sub(shape.used_width() + shape.width)
+        .unwrap_or(0);
+    let index_shape = try_opt!(Shape::indented(indent, context.config).offset_left(lbr.len()));
+    let index_shape = try_opt!(index_shape.sub_width(rbr.len() + rhs_overhead));
+    let new_index_rw = index.rewrite(context, index_shape);
+    match (orig_index_rw, new_index_rw) {
+        (_, Some(ref new_index_str)) if !new_index_str.contains('\n') => {
+            Some(format!(
+                "{}\n{}{}{}{}",
+                expr_str,
+                indent.to_string(&context.config),
+                lbr,
+                new_index_str,
+                rbr
+            ))
+        }
+        (None, Some(ref new_index_str)) => {
+            Some(format!(
+                "{}\n{}{}{}{}",
+                expr_str,
+                indent.to_string(&context.config),
+                lbr,
+                new_index_str,
+                rbr
+            ))
+        }
+        (Some(ref index_str), _) => Some(format!("{}{}{}{}", expr_str, lbr, index_str, rbr)),
+        _ => None,
+    }
 }
 
 fn rewrite_struct_lit<'a>(
index 80e4f2b623f8605b0ea571b4c829e47c0cecf0f5..3d54e68c4093beac1bbc75a0207fc4e76cd27a20 100644 (file)
@@ -308,3 +308,16 @@ fn issue1714() {
     v = &mut {v}[mid..];
     let (left, right) = {v}.split_at_mut(mid);
 }
+
+// Multi-lined index should be put on the next line if it fits in one line.
+fn issue1749() {
+    {
+        {
+            {
+                if self.shape[(r as f32 + self.x_offset) as usize][(c as f32 + self.y_offset) as usize] != 0 {
+                    // hello
+                }
+            }
+        }
+    }
+}
index b650904071430c7340d2255f91c734e293cef8ad..624a3c7eb9451bd39186bcd0b948cc304679f004 100644 (file)
@@ -367,3 +367,18 @@ fn issue1714() {
     v = &mut { v }[mid..];
     let (left, right) = { v }.split_at_mut(mid);
 }
+
+// Multi-lined index should be put on the next line if it fits in one line.
+fn issue1749() {
+    {
+        {
+            {
+                if self.shape[(r as f32 + self.x_offset) as usize]
+                    [(c as f32 + self.y_offset) as usize] != 0
+                {
+                    // hello
+                }
+            }
+        }
+    }
+}