From a16307a70f250708d31582b06584e084d5cd82cf Mon Sep 17 00:00:00 2001 From: topecongiro Date: Fri, 21 Jul 2017 17:53:03 +0900 Subject: [PATCH 1/2] Allow block-like rhs expression to stay on the same line as lhs --- src/expr.rs | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/src/expr.rs b/src/expr.rs index 8225f0165dd..2ba08cd3390 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -377,9 +377,9 @@ where // Note that this is non-conservative, but its just to see if it's even // worth trying to put everything on one line. let rhs_shape = try_opt!(shape.sub_width(suffix.len())); - let rhs_result = rhs.rewrite(context, rhs_shape); + let rhs_orig_result = rhs.rewrite(context, rhs_shape); - if let Some(rhs_result) = rhs_result { + if let Some(ref rhs_result) = rhs_orig_result { // This 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.contains('\n') { @@ -419,6 +419,7 @@ where // We have to use multiple lines. // Re-evaluate the rhs because we have more space now: + let sep = if infix.ends_with(' ') { " " } else { "" }; let infix = infix.trim_right(); let rhs_shape = match context.config.control_style() { Style::Legacy => { @@ -439,12 +440,24 @@ where width: try_opt!(context.config.max_width().checked_sub(lhs_overhead)), ..shape }; - let lhs_result = try_opt!(lhs.rewrite(context, lhs_shape)); + let lhs_result = try_opt!( + lhs.rewrite(context, lhs_shape) + .map(|lhs_str| format!("{}{}{}", prefix, lhs_str, infix)) + ); + if let Some(ref rhs_str) = rhs_orig_result { + if rhs_str.lines().count() <= rhs_result.lines().count() && + rhs_str + .lines() + .next() + .map_or(false, |first_line| first_line.ends_with('{')) && + last_line_width(&lhs_result) + sep.len() + first_line_width(rhs_str) <= shape.width + { + return Some(format!("{}{}{}{}", lhs_result, sep, rhs_str, suffix)); + } + } Some(format!( - "{}{}{}\n{}{}{}", - prefix, + "{}\n{}{}{}", lhs_result, - infix, rhs_shape.indent.to_string(context.config), rhs_result, suffix From 68c6fe70fdf441b46d5b88bc344e6d68f8f0068d Mon Sep 17 00:00:00 2001 From: topecongiro Date: Fri, 21 Jul 2017 17:55:52 +0900 Subject: [PATCH 2/2] Format source codes and update tests --- src/expr.rs | 11 +++++------ src/items.rs | 11 +++++------ src/visitor.rs | 14 ++++++-------- tests/target/chains-visual.rs | 11 +++++------ tests/target/chains.rs | 9 ++++----- tests/target/issue-831.rs | 9 +++++++++ 6 files changed, 34 insertions(+), 31 deletions(-) create mode 100644 tests/target/issue-831.rs diff --git a/src/expr.rs b/src/expr.rs index 2ba08cd3390..c1d5e8ccc39 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -2899,12 +2899,11 @@ pub fn rewrite_assign_rhs>( shape: Shape, ) -> Option { let lhs = lhs.into(); - let last_line_width = last_line_width(&lhs) - - if lhs.contains('\n') { - shape.indent.width() - } else { - 0 - }; + let last_line_width = last_line_width(&lhs) - if lhs.contains('\n') { + shape.indent.width() + } else { + 0 + }; // 1 = space between operator and rhs. let orig_shape = try_opt!(shape.offset_left(last_line_width + 1)); let rhs = try_opt!(choose_rhs( diff --git a/src/items.rs b/src/items.rs index 22ac1354e34..087b5615f02 100644 --- a/src/items.rs +++ b/src/items.rs @@ -829,12 +829,11 @@ fn rewrite_trait_ref( result_len: usize, ) -> Option { // 1 = space between generics and trait_ref - let used_space = 1 + polarity_str.len() + - if generics_str.contains('\n') { - last_line_width(&generics_str) - } else { - result_len + generics_str.len() - }; + let used_space = 1 + polarity_str.len() + if generics_str.contains('\n') { + last_line_width(&generics_str) + } else { + result_len + generics_str.len() + }; let shape = Shape::indented(offset + used_space, context.config); if let Some(trait_ref_str) = trait_ref.rewrite(context, shape) { if !(retry && trait_ref_str.contains('\n')) { diff --git a/src/visitor.rs b/src/visitor.rs index e6e7f038ce7..e3233e1dba3 100644 --- a/src/visitor.rs +++ b/src/visitor.rs @@ -630,14 +630,12 @@ impl<'a> FmtVisitor<'a> { let use_item_length = items_left .iter() .take_while(|ppi| { - is_use_item(&***ppi) && - (!reorder_imports_in_group || - { - let current = self.codemap.lookup_line_range(item_bound(&ppi)); - let in_same_group = current.lo < last.hi + 2; - last = current; - in_same_group - }) + is_use_item(&***ppi) && (!reorder_imports_in_group || { + let current = self.codemap.lookup_line_range(item_bound(&ppi)); + let in_same_group = current.lo < last.hi + 2; + last = current; + in_same_group + }) }) .count(); let (use_items, rest) = items_left.split_at(use_item_length); diff --git a/tests/target/chains-visual.rs b/tests/target/chains-visual.rs index 3962bdcbbcd..c2aecd26105 100644 --- a/tests/target/chains-visual.rs +++ b/tests/target/chains-visual.rs @@ -118,12 +118,11 @@ fn floaters() { }) .quux(); - a + - match x { - true => "yay!", - false => "boo!", - } - .bar() + a + match x { + true => "yay!", + false => "boo!", + } + .bar() } fn is_replaced_content() -> bool { diff --git a/tests/target/chains.rs b/tests/target/chains.rs index d930f0a49ba..4955817da5c 100644 --- a/tests/target/chains.rs +++ b/tests/target/chains.rs @@ -114,11 +114,10 @@ fn floaters() { }) .quux(); - a + - match x { - true => "yay!", - false => "boo!", - }.bar() + a + match x { + true => "yay!", + false => "boo!", + }.bar() } fn is_replaced_content() -> bool { diff --git a/tests/target/issue-831.rs b/tests/target/issue-831.rs new file mode 100644 index 00000000000..1d6327c2158 --- /dev/null +++ b/tests/target/issue-831.rs @@ -0,0 +1,9 @@ +fn main() { + let y = a.iter().any(|x| { + println!("a"); + }) || b.iter().any(|x| { + println!("b"); + }) || c.iter().any(|x| { + println!("c"); + }); +}