From 163fbf62ee5c5b9ad1a39482ebf2f8c2e464e6b5 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Ga=C3=ABtan=20Cassiers?= Date: Fri, 3 Jul 2015 00:50:55 +0200 Subject: [PATCH] Fix bugs in width computation --- src/expr.rs | 9 ++++++--- tests/source/expr.rs | 4 +++- tests/target/expr.rs | 5 ++++- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/expr.rs b/src/expr.rs index 2f97fa7e97a..6fff8be9fc7 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -287,13 +287,14 @@ fn rewrite_binary_op(context: &RewriteContext, let operator_str = context.codemap.span_to_snippet(op.span).unwrap(); // 1 = space between lhs expr and operator - let mut result = try_opt!(lhs.rewrite(context, width - 1 - operator_str.len(), offset)); + let mut result = + try_opt!(lhs.rewrite(context, context.config.max_width - offset - 1 - operator_str.len(), offset)); result.push(' '); result.push_str(&operator_str); let remaining_width = match result.rfind('\n') { - Some(idx) => (context.config.max_width + idx).checked_sub(result.len()).unwrap_or(0), + Some(idx) => (offset + width + idx).checked_sub(result.len()).unwrap_or(0), None => width.checked_sub(result.len()).unwrap_or(0) }; @@ -302,7 +303,9 @@ fn rewrite_binary_op(context: &RewriteContext, // operations with high precendence close together. let rhs_result = try_opt!(rhs.rewrite(context, width, offset)); - if rhs_result.len() > remaining_width { + // Second condition is needed in case of line break not caused by a + // shortage of space, but by end-of-line comments, for example. + if rhs_result.len() > remaining_width || rhs_result.contains('\n') { result.push('\n'); result.push_str(&make_indent(offset)); } else { diff --git a/tests/source/expr.rs b/tests/source/expr.rs index d7013a312f1..373e42d67c5 100644 --- a/tests/source/expr.rs +++ b/tests/source/expr.rs @@ -10,5 +10,7 @@ fn foo() -> bool { some_ridiculously_loooooooooooooooooooooong_function(10000 * 30000000000 + 40000 / 1002200000000 - 50000 * sqrt(-1), - trivial_value) + trivial_value); + (((((((((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + a + + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaa))))))))) } diff --git a/tests/target/expr.rs b/tests/target/expr.rs index c565c94b2ac..25c84511c41 100644 --- a/tests/target/expr.rs +++ b/tests/target/expr.rs @@ -10,5 +10,8 @@ fn foo() -> bool { some_ridiculously_loooooooooooooooooooooong_function(10000 * 30000000000 + 40000 / 1002200000000 - 50000 * sqrt(-1), - trivial_value) + trivial_value); + (((((((((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + + a + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + + aaaaa))))))))) } -- 2.44.0