Update heuristic in rewrite_assign_rhs

Put the rhs of assignement on the next line when putting next to `lhs = ` will
cause rhs to go multi line, but putting on the next line makes it fits
in a single line.
This commit is contained in:
topecongiro 2017-07-05 18:30:11 +09:00
parent 101df143cf
commit 0237347abc

View file

@ -2937,26 +2937,21 @@ pub fn rewrite_assign_rhs<S: Into<String>>(
let orig_shape = try_opt!(shape.offset_left(last_line_width + 1));
let rhs = ex.rewrite(context, orig_shape);
fn count_line_breaks(src: &str) -> usize {
src.chars().filter(|&x| x == '\n').count()
}
match rhs {
Some(ref new_str) if count_line_breaks(new_str) < 2 => {
Some(ref new_str) if !new_str.contains('\n') => {
result.push(' ');
result.push_str(new_str);
}
_ => {
// Expression did not fit on the same line as the identifier or is
// at least three lines big. Try splitting the line and see
// if that works better.
// Expression did not fit on the same line as the identifier.
// Try splitting the line and see if that works better.
let new_shape = try_opt!(shape.block_left(context.config.tab_spaces()));
let new_rhs = ex.rewrite(context, new_shape);
// 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 => {
if prefer_next_line(orig_rhs, replacement_rhs) => {
result.push_str(&format!("\n{}", new_shape.indent.to_string(context.config)));
result.push_str(replacement_rhs);
}
@ -2976,6 +2971,16 @@ pub fn rewrite_assign_rhs<S: Into<String>>(
Some(result)
}
fn prefer_next_line(orig_rhs: &str, next_line_rhs: &str) -> bool {
fn count_line_breaks(src: &str) -> usize {
src.chars().filter(|&x| x == '\n').count()
}
!next_line_rhs.contains('\n') ||
count_line_breaks(orig_rhs) > count_line_breaks(next_line_rhs) + 1
}
fn rewrite_expr_addrof(
context: &RewriteContext,
mutability: ast::Mutability,