Use correct budget for chain

This commit is contained in:
topecongiro 2017-06-17 16:56:37 +09:00
parent 7132814a71
commit 92634a8f69
2 changed files with 12 additions and 6 deletions

View File

@ -124,7 +124,9 @@ pub fn rewrite_chain(expr: &ast::Expr, context: &RewriteContext, shape: Shape) -
let first_subexpr_is_try = subexpr_list.last().map_or(false, is_try);
let (nested_shape, extend) = if !parent_rewrite_contains_newline && is_continuable(&parent) {
let nested_shape = if first_subexpr_is_try {
parent_shape.block_indent(context.config.tab_spaces())
parent_shape
.block_indent(context.config.tab_spaces())
.with_max_width(context.config)
} else {
chain_indent(context, shape.add_offset(parent_rewrite.len()))
};
@ -143,7 +145,12 @@ pub fn rewrite_chain(expr: &ast::Expr, context: &RewriteContext, shape: Shape) -
} else if parent_rewrite_contains_newline {
(chain_indent(context, parent_shape), false)
} else {
(shape.block_indent(context.config.tab_spaces()), false)
(
shape
.block_indent(context.config.tab_spaces())
.with_max_width(context.config),
false,
)
};
let other_child_shape = nested_shape.with_max_width(context.config);
@ -234,7 +241,8 @@ pub fn rewrite_chain(expr: &ast::Expr, context: &RewriteContext, shape: Shape) -
}
}
// Try overflowing the last element if we are using block indent.
// Try overflowing the last element if we are using block indent and it goes multi line
// or it fits in a single line but goes over the max width.
if !fits_single_line && context.use_block_indent() {
let (init, last) = rewrites.split_at_mut(last_non_try_index);
let almost_single_line = init.iter().all(|s| !s.contains('\n'));

View File

@ -2761,9 +2761,7 @@ pub fn rewrite_assign_rhs<S: Into<String>>(
// FIXME: DRY!
match (rhs, new_rhs) {
(Some(ref orig_rhs), Some(ref replacement_rhs))
if count_line_breaks(orig_rhs) > count_line_breaks(replacement_rhs) + 1 ||
(orig_rhs.rewrite(context, shape).is_none() &&
replacement_rhs.rewrite(context, new_shape).is_some()) => {
if count_line_breaks(orig_rhs) > count_line_breaks(replacement_rhs) + 1 => {
result.push_str(&format!("\n{}", new_shape.indent.to_string(context.config)));
result.push_str(replacement_rhs);
}