Skip to content

Commit 54d2620

Browse files
committed
Merge pull request #124 from cassiersg/fix-bin-op
Fix bugs in width computation for bin op
2 parents 08b9837 + 163fbf6 commit 54d2620

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
lines changed

src/expr.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -287,13 +287,14 @@ fn rewrite_binary_op(context: &RewriteContext,
287287
let operator_str = context.codemap.span_to_snippet(op.span).unwrap();
288288

289289
// 1 = space between lhs expr and operator
290-
let mut result = try_opt!(lhs.rewrite(context, width - 1 - operator_str.len(), offset));
290+
let mut result =
291+
try_opt!(lhs.rewrite(context, context.config.max_width - offset - 1 - operator_str.len(), offset));
291292

292293
result.push(' ');
293294
result.push_str(&operator_str);
294295

295296
let remaining_width = match result.rfind('\n') {
296-
Some(idx) => (context.config.max_width + idx).checked_sub(result.len()).unwrap_or(0),
297+
Some(idx) => (offset + width + idx).checked_sub(result.len()).unwrap_or(0),
297298
None => width.checked_sub(result.len()).unwrap_or(0)
298299
};
299300

@@ -302,7 +303,9 @@ fn rewrite_binary_op(context: &RewriteContext,
302303
// operations with high precendence close together.
303304
let rhs_result = try_opt!(rhs.rewrite(context, width, offset));
304305

305-
if rhs_result.len() > remaining_width {
306+
// Second condition is needed in case of line break not caused by a
307+
// shortage of space, but by end-of-line comments, for example.
308+
if rhs_result.len() > remaining_width || rhs_result.contains('\n') {
306309
result.push('\n');
307310
result.push_str(&make_indent(offset));
308311
} else {

tests/source/expr.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,7 @@ fn foo() -> bool {
1010

1111
some_ridiculously_loooooooooooooooooooooong_function(10000 * 30000000000 + 40000 / 1002200000000
1212
- 50000 * sqrt(-1),
13-
trivial_value)
13+
trivial_value);
14+
(((((((((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + a +
15+
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaa)))))))))
1416
}

tests/target/expr.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,8 @@ fn foo() -> bool {
1010

1111
some_ridiculously_loooooooooooooooooooooong_function(10000 * 30000000000 +
1212
40000 / 1002200000000 - 50000 * sqrt(-1),
13-
trivial_value)
13+
trivial_value);
14+
(((((((((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +
15+
a + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +
16+
aaaaa)))))))))
1417
}

0 commit comments

Comments
 (0)