Skip to content

Fix bugs in width computation for bin op #124

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 3, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
};

Expand All @@ -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 {
Expand Down
4 changes: 3 additions & 1 deletion tests/source/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)))))))))
}
5 changes: 4 additions & 1 deletion tests/target/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)))))))))
}