From 229a55248b49137d32af96bdd88c58913409106e Mon Sep 17 00:00:00 2001 From: Nick Cameron Date: Thu, 12 Jul 2018 22:21:07 +1200 Subject: [PATCH] address reviewer comments --- src/lists.rs | 3 ++- src/pairs.rs | 15 ++++++++++----- tests/target/expr.rs | 6 ++++-- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/lists.rs b/src/lists.rs index 29c7e40fb17..c415032ad67 100644 --- a/src/lists.rs +++ b/src/lists.rs @@ -419,7 +419,8 @@ where if first_line_width(&formatted_comment) + last_line_width(&result) + comment_alignment - + 1 > formatting.config.max_width() + + 1 + > formatting.config.max_width() { item_max_width = None; formatted_comment = rewrite_post_comment(&mut item_max_width)?; diff --git a/src/pairs.rs b/src/pairs.rs index 3cd044d606e..2c4358f7278 100644 --- a/src/pairs.rs +++ b/src/pairs.rs @@ -69,10 +69,7 @@ fn rewrite_pairs_one_line( let mut result = String::new(); let base_shape = shape.block(); - for (e, s) in list.list[..list.list.len()] - .iter() - .zip(list.separators.iter()) - { + for (e, s) in list.list.iter().zip(list.separators.iter()) { let cur_shape = base_shape.offset_left(last_line_width(&result))?; let rewrite = e.rewrite(context, cur_shape)?; @@ -137,7 +134,15 @@ fn rewrite_pairs_multiline( result.push_str(&rewrite); for (e, s) in list.list[1..].iter().zip(list.separators.iter()) { - if trimmed_last_line_width(&result) <= context.config.tab_spaces() { + // The following test checks if we should keep two subexprs on the same + // line. We do this if not doing so would create an orphan and there is + // enough space to do so. + let offset = if result.contains('\n') { + 0 + } else { + shape.used_width() + }; + if last_line_width(&result) + offset <= nested_shape.used_width() { // We must snuggle the next line onto the previous line to avoid an orphan. if let Some(line_shape) = shape.offset_left(s.len() + 2 + trimmed_last_line_width(&result)) diff --git a/tests/target/expr.rs b/tests/target/expr.rs index e8caa73387e..5684b6efd31 100644 --- a/tests/target/expr.rs +++ b/tests/target/expr.rs @@ -22,7 +22,8 @@ fn foo() -> bool { trivial_value, ); (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa - + a + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + + a + + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaa); { @@ -514,5 +515,6 @@ fn issue_2802() { * a_very_specific_length(specific_length_arg) * very_specific_length(Foo { a: some_much_much_longer_value, - }) * some_value + }) + * some_value }