]> git.lizzy.rs Git - rust.git/commitdiff
Format indices
authorMarcus Klaas <mail@marcusklaas.nl>
Fri, 2 Oct 2015 09:47:03 +0000 (11:47 +0200)
committerMarcus Klaas <mail@marcusklaas.nl>
Fri, 2 Oct 2015 09:47:03 +0000 (11:47 +0200)
src/expr.rs
tests/source/expr.rs
tests/target/expr.rs

index ecc9477321124fcd49e9096e92d0022a53e9aa93..75630eb8301468efe3332ce8d1e5c01f615c8144 100644 (file)
@@ -111,8 +111,6 @@ fn rewrite(&self, context: &RewriteContext, width: usize, offset: Indent) -> Opt
                                 offset,
                                 true)
             }
-            // We reformat it ourselves because rustc gives us a bad span
-            // for ranges, see rust#27162
             ast::Expr_::ExprRange(ref left, ref right) => {
                 rewrite_range(context,
                               left.as_ref().map(|e| &**e),
@@ -182,10 +180,12 @@ fn rewrite(&self, context: &RewriteContext, width: usize, offset: Indent) -> Opt
             ast::Expr_::ExprCast(ref expr, ref ty) => {
                 rewrite_cast(expr, ty, context, width, offset)
             }
+            ast::Expr_::ExprIndex(ref expr, ref index) => {
+                rewrite_index(expr, index, context, width, offset)
+            }
             // We do not format these expressions yet, but they should still
             // satisfy our width restrictions.
             ast::Expr_::ExprInPlace(..) |
-            ast::Expr_::ExprIndex(..) |
             ast::Expr_::ExprInlineAsm(..) |
             ast::Expr_::ExprRepeat(..) => {
                 wrap_str(context.snippet(self.span),
@@ -197,6 +197,38 @@ fn rewrite(&self, context: &RewriteContext, width: usize, offset: Indent) -> Opt
     }
 }
 
+fn rewrite_index(expr: &ast::Expr,
+                 index: &ast::Expr,
+                 context: &RewriteContext,
+                 width: usize,
+                 offset: Indent)
+                 -> Option<String> {
+    let max_width = try_opt!(width.checked_sub("[]".len()));
+
+    binary_search(1,
+                  max_width,
+                  |expr_budget| {
+                      let expr_str = match expr.rewrite(context, expr_budget, offset) {
+                          Some(result) => result,
+                          None => return Err(Ordering::Greater),
+                      };
+
+                      let last_line_width = last_line_width(&expr_str);
+                      let index_budget = match max_width.checked_sub(last_line_width) {
+                          Some(b) => b,
+                          None => return Err(Ordering::Less),
+                      };
+                      let index_indent = offset + last_line_width + "[".len();
+
+                      let index_str = match index.rewrite(context, index_budget, index_indent) {
+                          Some(result) => result,
+                          None => return Err(Ordering::Less),
+                      };
+
+                      Ok(format!("{}[{}]", expr_str, index_str))
+                  })
+}
+
 fn rewrite_cast(expr: &ast::Expr,
                 ty: &ast::Ty,
                 context: &RewriteContext,
@@ -218,7 +250,7 @@ fn rewrite_cast(expr: &ast::Expr,
                           Some(b) => b,
                           None => return Err(Ordering::Less),
                       };
-                      let ty_indent = offset + last_line_width;
+                      let ty_indent = offset + last_line_width + " as ".len();
 
                       let ty_str = match ty.rewrite(context, ty_budget, ty_indent) {
                           Some(result) => result,
index 544a566cee9b338f53afc2288fbb5bd681a9816f..16b9047dcf1e717f391d7ea94e4be00a7e304bfb 100644 (file)
@@ -197,3 +197,8 @@ fn casts() {
         as SomeTraitXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX;
     let slightly_longer_trait = yyyyyyyyy + yyyyyyyyyyy as SomeTraitYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY;
 }
+
+fn indices() {
+    let x = (aaaaaaaaaaaaaaaaaaaaaaaaaaaa+bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb+cccccccccccccccc) [ x + y + z ];
+    let y = (aaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccc)[ xxxxx + yyyyy + zzzzz ];
+}
index c23b0b050c87dbc8083f9ed25ecaa8d99797ad92..ab25d489c1b3bf24df5ec4e59666abb7abacbbc7 100644 (file)
@@ -208,3 +208,11 @@ fn casts() {
     let slightly_longer_trait = yyyyyyyyy +
                                 yyyyyyyyyyy as SomeTraitYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY;
 }
+
+fn indices() {
+    let x = (aaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccc)[x +
+                                                                                                y +
+                                                                                                z];
+    let y = (aaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +
+             cccccccccccccccc)[xxxxx + yyyyy + zzzzz];
+}