about summary refs log tree commit diff
diff options
context:
space:
mode:
authorNick Cameron <nrc@ncameron.org>2015-09-13 09:20:37 +1200
committerNick Cameron <nrc@ncameron.org>2015-09-13 09:20:37 +1200
commit4738d284f2c1c6d448b3a375f547b61da2c86adc (patch)
treed4553aaaf8280422cd64498423e25d6796611f2f
parent4debd4d302bd455d7feba2f1386992fe67bbbef5 (diff)
parent7deee6daf58cdd9487680d964871436c146656a3 (diff)
Merge pull request #309 from marcusklaas/array-literals
Format array literals
-rw-r--r--src/expr.rs57
-rw-r--r--tests/source/expr.rs44
-rw-r--r--tests/target/expr.rs17
3 files changed, 117 insertions, 1 deletions
diff --git a/src/expr.rs b/src/expr.rs
index b5fd09e0e01..e738976b5c5 100644
--- a/src/expr.rs
+++ b/src/expr.rs
@@ -31,12 +31,20 @@ use syntax::visit::Visitor;
 impl Rewrite for ast::Expr {
     fn rewrite(&self, context: &RewriteContext, width: usize, offset: usize) -> Option<String> {
         match self.node {
+            ast::Expr_::ExprVec(ref expr_vec) => {
+                rewrite_array(expr_vec.iter().map(|e| &**e), self.span, context, width, offset)
+            }
             ast::Expr_::ExprLit(ref l) => {
                 match l.node {
                     ast::Lit_::LitStr(_, ast::StrStyle::CookedStr) => {
                         rewrite_string_lit(context, l.span, width, offset)
                     }
-                    _ => Some(context.snippet(self.span)),
+                    _ => {
+                        wrap_str(context.snippet(self.span),
+                                 context.config.max_width,
+                                 width,
+                                 offset)
+                    }
                 }
             }
             ast::Expr_::ExprCall(ref callee, ref args) => {
@@ -152,6 +160,53 @@ impl Rewrite for ast::Expr {
     }
 }
 
+fn rewrite_array<'a, I>(expr_iter: I,
+                        span: Span,
+                        context: &RewriteContext,
+                        width: usize,
+                        offset: usize)
+                        -> Option<String>
+    where I: Iterator<Item = &'a ast::Expr> + ExactSizeIterator
+{
+    // 2 for brackets;
+    let max_item_width = try_opt!(width.checked_sub(2));
+    let items = itemize_list(context.codemap,
+                             expr_iter,
+                             "]",
+                             |item| item.span.lo,
+                             |item| item.span.hi,
+                             // 1 = [
+                             // FIXME(#133): itemize_list doesn't support
+                             // rewrite failure. This may not be its
+                             // responsibility, but that of write_list.
+                             |item| {
+                                 item.rewrite(context, max_item_width, offset + 1)
+                                     .unwrap_or_else(|| context.snippet(item.span))
+                             },
+                             span_after(span, "[", context.codemap),
+                             span.hi)
+                    .collect::<Vec<_>>();
+
+    let tactic = if items.iter().any(|li| li.item.len() > 10 || li.is_multiline()) {
+        ListTactic::HorizontalVertical
+    } else {
+        ListTactic::Mixed
+    };
+
+    let fmt = ListFormatting {
+        tactic: tactic,
+        separator: ",",
+        trailing_separator: SeparatorTactic::Never,
+        indent: offset + 1,
+        h_width: max_item_width,
+        v_width: max_item_width,
+        ends_with_newline: false,
+    };
+    let list_str = try_opt!(write_list(&items, &fmt));
+
+    Some(format!("[{}]", list_str))
+}
+
 // This functions is pretty messy because of the wrapping and unwrapping of
 // expressions into and from blocks. See rust issue #27872.
 fn rewrite_closure(capture: ast::CaptureClause,
diff --git a/tests/source/expr.rs b/tests/source/expr.rs
index 083715dc8e7..faa0d3775c7 100644
--- a/tests/source/expr.rs
+++ b/tests/source/expr.rs
@@ -129,3 +129,47 @@ fn issue184(source: &str) {
         }
     }
 }
+
+fn arrays() {
+    let x = [0,
+         1,
+         2,
+         3,
+         4,
+         5,
+         6,
+         7,
+         8,
+         9,
+         0,
+         1,
+         2,
+         3,
+         4,
+         5,
+         6,
+         7,
+         8,
+         9,
+         0,
+         7,
+         8,
+         9,
+         0,
+         1,
+         2,
+         3,
+         4,
+         5,
+         6,
+         7,
+         8,
+         9,
+         0];
+
+    let y = [/* comment */ 1, 2 /* post comment */, 3];
+
+    let z = [xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx, yyyyyyyyyyyyyyyyyyyyyyyyyyy, zzzzzzzzzzzzzzzzzz, q];
+
+    [ 1 +   3, 4 ,  5, 6, 7, 7, fncall::<Vec<_>>(3-1)]
+}
diff --git a/tests/target/expr.rs b/tests/target/expr.rs
index 9017fd8bee0..bb629f12126 100644
--- a/tests/target/expr.rs
+++ b/tests/target/expr.rs
@@ -166,3 +166,20 @@ fn issue184(source: &str) {
         }
     }
 }
+
+fn arrays() {
+    let x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 7, 8, 9, 0, 1, 2, 3, 4,
+             5, 6, 7, 8, 9, 0];
+
+    let y = [// comment
+             1,
+             2, // post comment
+             3];
+
+    let z = [xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx,
+             yyyyyyyyyyyyyyyyyyyyyyyyyyy,
+             zzzzzzzzzzzzzzzzzz,
+             q];
+
+    [1 + 3, 4, 5, 6, 7, 7, fncall::<Vec<_>>(3 - 1)]
+}