Format indices
This commit is contained in:
parent
603f2034a5
commit
ad2e3b8e2b
3 changed files with 49 additions and 4 deletions
40
src/expr.rs
40
src/expr.rs
|
@ -111,8 +111,6 @@ impl Rewrite for ast::Expr {
|
||||||
offset,
|
offset,
|
||||||
true)
|
true)
|
||||||
}
|
}
|
||||||
// We reformat it ourselves because rustc gives us a bad span
|
|
||||||
// for ranges, see rust#27162
|
|
||||||
ast::Expr_::ExprRange(ref left, ref right) => {
|
ast::Expr_::ExprRange(ref left, ref right) => {
|
||||||
rewrite_range(context,
|
rewrite_range(context,
|
||||||
left.as_ref().map(|e| &**e),
|
left.as_ref().map(|e| &**e),
|
||||||
|
@ -182,10 +180,12 @@ impl Rewrite for ast::Expr {
|
||||||
ast::Expr_::ExprCast(ref expr, ref ty) => {
|
ast::Expr_::ExprCast(ref expr, ref ty) => {
|
||||||
rewrite_cast(expr, ty, context, width, offset)
|
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
|
// We do not format these expressions yet, but they should still
|
||||||
// satisfy our width restrictions.
|
// satisfy our width restrictions.
|
||||||
ast::Expr_::ExprInPlace(..) |
|
ast::Expr_::ExprInPlace(..) |
|
||||||
ast::Expr_::ExprIndex(..) |
|
|
||||||
ast::Expr_::ExprInlineAsm(..) |
|
ast::Expr_::ExprInlineAsm(..) |
|
||||||
ast::Expr_::ExprRepeat(..) => {
|
ast::Expr_::ExprRepeat(..) => {
|
||||||
wrap_str(context.snippet(self.span),
|
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,
|
fn rewrite_cast(expr: &ast::Expr,
|
||||||
ty: &ast::Ty,
|
ty: &ast::Ty,
|
||||||
context: &RewriteContext,
|
context: &RewriteContext,
|
||||||
|
@ -218,7 +250,7 @@ fn rewrite_cast(expr: &ast::Expr,
|
||||||
Some(b) => b,
|
Some(b) => b,
|
||||||
None => return Err(Ordering::Less),
|
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) {
|
let ty_str = match ty.rewrite(context, ty_budget, ty_indent) {
|
||||||
Some(result) => result,
|
Some(result) => result,
|
||||||
|
|
|
@ -197,3 +197,8 @@ fn casts() {
|
||||||
as SomeTraitXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX;
|
as SomeTraitXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX;
|
||||||
let slightly_longer_trait = yyyyyyyyy + yyyyyyyyyyy as SomeTraitYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY;
|
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 +
|
let slightly_longer_trait = yyyyyyyyy +
|
||||||
yyyyyyyyyyy as SomeTraitYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY;
|
yyyyyyyyyyy as SomeTraitYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn indices() {
|
||||||
|
let x = (aaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccc)[x +
|
||||||
|
y +
|
||||||
|
z];
|
||||||
|
let y = (aaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +
|
||||||
|
cccccccccccccccc)[xxxxx + yyyyy + zzzzz];
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue