Use correct one line budget when rewriting index

This commit is contained in:
topecongiro 2017-07-10 14:23:29 +09:00
parent 0a42648678
commit 7ab4bdfd85
3 changed files with 22 additions and 7 deletions

View file

@ -2494,13 +2494,18 @@ fn rewrite_index(
("[", "]")
};
let offset = expr_str.len() + lbr.len();
let orig_index_rw = shape
.visual_indent(offset)
.sub_width(offset + rbr.len())
.and_then(|index_shape| index.rewrite(context, index_shape));
let offset = last_line_width(&expr_str) + lbr.len();
let rhs_overhead = shape.rhs_overhead(context.config);
let index_shape = if expr_str.contains('\n') {
Shape::legacy(context.config.max_width(), shape.indent)
.offset_left(offset)
.and_then(|shape| shape.sub_width(rbr.len() + rhs_overhead))
} else {
shape.visual_indent(offset).sub_width(offset + rbr.len())
};
let orig_index_rw = index_shape.and_then(|s| index.rewrite(context, s));
// Return if everything fits in a single line.
// Return if index fits in a single line.
match orig_index_rw {
Some(ref index_str) if !index_str.contains('\n') => {
return Some(format!("{}{}{}{}", expr_str, lbr, index_str, rbr));
@ -2510,7 +2515,6 @@ fn rewrite_index(
// Try putting index on the next line and see if it fits in a single line.
let indent = shape.indent.block_indent(context.config);
let rhs_overhead = shape.rhs_overhead(context.config);
let index_shape = try_opt!(Shape::indented(indent, context.config).offset_left(lbr.len()));
let index_shape = try_opt!(index_shape.sub_width(rbr.len() + rhs_overhead));
let new_index_rw = index.rewrite(context, index_shape);

View file

@ -222,6 +222,8 @@ fn casts() {
fn indices() {
let x = (aaaaaaaaaaaaaaaaaaaaaaaaaaaa+bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb+cccccccccccccccc) [ x + y + z ];
let y = (aaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccc)[ xxxxx + yyyyy + zzzzz ];
let z = xxxxxxxxxx.x().y().zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz()[aaaaa];
let z = xxxxxxxxxx.x().y().zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz()[aaaaa];
}
fn repeats() {

View file

@ -283,6 +283,15 @@ fn indices() {
[x + y + z];
let y = (aaaaaaaaaaaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccc)
[xxxxx + yyyyy + zzzzz];
let z = xxxxxxxxxx
.x()
.y()
.zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz()[aaaaa];
let z = xxxxxxxxxx
.x()
.y()
.zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz()
[aaaaa];
}
fn repeats() {