diff options
| author | Nick Cameron <nrc@ncameron.org> | 2015-07-02 17:58:02 -0700 |
|---|---|---|
| committer | Nick Cameron <nrc@ncameron.org> | 2015-07-02 17:58:02 -0700 |
| commit | 54d2620ead1dd8ace78f5877e58971b96cd14767 (patch) | |
| tree | c25d8a31c854417cd81f0cd3c06c5c0540a01dd2 /src | |
| parent | 08b9837a1f5fe49380ac7309d70bf0f2e34e33bc (diff) | |
| parent | 163fbf62ee5c5b9ad1a39482ebf2f8c2e464e6b5 (diff) | |
Merge pull request #124 from cassiersg/fix-bin-op
Fix bugs in width computation for bin op
Diffstat (limited to 'src')
| -rw-r--r-- | src/expr.rs | 9 |
1 files changed, 6 insertions, 3 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 { |
