Format indices
This commit is contained in:
parent
603f2034a5
commit
ad2e3b8e2b
40
src/expr.rs
40
src/expr.rs
@ -111,8 +111,6 @@ impl Rewrite for ast::Expr {
|
||||
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 @@ impl Rewrite for ast::Expr {
|
||||
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 @@ impl Rewrite for ast::Expr {
|
||||
}
|
||||
}
|
||||
|
||||
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,
|
||||
|
@ -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 ];
|
||||
}
|
||||
|
@ -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];
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user