From: Marcus Klaas Date: Fri, 2 Oct 2015 09:47:03 +0000 (+0200) Subject: Format indices X-Git-Url: https://git.lizzy.rs/?a=commitdiff_plain;h=ad2e3b8e2b58f016d61b33e17de52d6edb75ec0a;p=rust.git Format indices --- diff --git a/src/expr.rs b/src/expr.rs index ecc94773211..75630eb8301 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -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 { + 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, diff --git a/tests/source/expr.rs b/tests/source/expr.rs index 544a566cee9..16b9047dcf1 100644 --- a/tests/source/expr.rs +++ b/tests/source/expr.rs @@ -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 ]; +} diff --git a/tests/target/expr.rs b/tests/target/expr.rs index c23b0b050c8..ab25d489c1b 100644 --- a/tests/target/expr.rs +++ b/tests/target/expr.rs @@ -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]; +}