From 8536c288f206445395375cfd06932ef74cf9877d Mon Sep 17 00:00:00 2001 From: csmoe <35686186+csmoe@users.noreply.github.com> Date: Thu, 31 May 2018 17:58:48 +0800 Subject: [PATCH 1/5] suppress and compress --- src/items.rs | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/src/items.rs b/src/items.rs index 60673d4ebdee..2aa53a1632ba 100644 --- a/src/items.rs +++ b/src/items.rs @@ -649,8 +649,14 @@ pub fn format_impl( } else { context.budget(last_line_width(&result)) }; - let option = WhereClauseOption::snuggled(&ref_and_type); - let where_clause_str = rewrite_where_clause( + + let mut option = WhereClauseOption::snuggled(&ref_and_type); + if items.is_empty() && generics.where_clause.predicates.len() == 1 { + option.compress_where(); + option.suppress_comma(); + } + + let mut where_clause_str = rewrite_where_clause( context, &generics.where_clause, context.config.brace_style(), @@ -1380,9 +1386,9 @@ fn format_tuple_struct( // We need to put the where clause on a new line, but we didn't // know that earlier, so the where clause will not be indented properly. result.push('\n'); - result.push_str( - &(offset.block_only() + (context.config.tab_spaces() - 1)).to_string(context.config), - ); + result + .push_str(&(offset.block_only() + (context.config.tab_spaces() - 1)) + .to_string(context.config)); } result.push_str(&where_clause_str); @@ -2133,6 +2139,14 @@ impl WhereClauseOption { compress_where: false, } } + + pub fn suppress_comma(&mut self) { + self.suppress_comma = true + } + + pub fn compress_where(&mut self) { + self.compress_where = true + } } fn rewrite_args( From 0468c134f4d1fd72452a55a8a09eeaf93b70e557 Mon Sep 17 00:00:00 2001 From: csmoe <35686186+csmoe@users.noreply.github.com> Date: Thu, 31 May 2018 18:33:45 +0800 Subject: [PATCH 2/5] snuggle where --- src/items.rs | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/items.rs b/src/items.rs index 2aa53a1632ba..872d736f15f0 100644 --- a/src/items.rs +++ b/src/items.rs @@ -651,9 +651,15 @@ pub fn format_impl( }; let mut option = WhereClauseOption::snuggled(&ref_and_type); - if items.is_empty() && generics.where_clause.predicates.len() == 1 { - option.compress_where(); + let snippet = context.snippet(item.span); + let open_pos = snippet.find_uncommented("{")? + 1; + if !contains_comment(&snippet[open_pos..]) + && items.is_empty() + && generics.where_clause.predicates.len() == 1 + { option.suppress_comma(); + option.snuggle(); + option.compress_where(); } let mut where_clause_str = rewrite_where_clause( @@ -1386,9 +1392,9 @@ fn format_tuple_struct( // We need to put the where clause on a new line, but we didn't // know that earlier, so the where clause will not be indented properly. result.push('\n'); - result - .push_str(&(offset.block_only() + (context.config.tab_spaces() - 1)) - .to_string(context.config)); + result.push_str( + &(offset.block_only() + (context.config.tab_spaces() - 1)).to_string(context.config), + ); } result.push_str(&where_clause_str); @@ -2147,6 +2153,10 @@ impl WhereClauseOption { pub fn compress_where(&mut self) { self.compress_where = true } + + pub fn snuggle(&mut self) { + self.snuggle = true + } } fn rewrite_args( From d46852b3f71b3d5cf13ac5d724f816c32a629710 Mon Sep 17 00:00:00 2001 From: csmoe <35686186+csmoe@users.noreply.github.com> Date: Thu, 31 May 2018 18:34:06 +0800 Subject: [PATCH 3/5] fix tests --- tests/source/configs/where_single_line/true.rs | 2 ++ tests/target/configs/where_single_line/true.rs | 2 ++ tests/target/impl.rs | 2 +- tests/target/impls.rs | 12 ++---------- 4 files changed, 7 insertions(+), 11 deletions(-) diff --git a/tests/source/configs/where_single_line/true.rs b/tests/source/configs/where_single_line/true.rs index daaab865af21..9de98283b5e5 100644 --- a/tests/source/configs/where_single_line/true.rs +++ b/tests/source/configs/where_single_line/true.rs @@ -22,3 +22,5 @@ where fn lorem() -> T where Ipsum: Eq { // body } + +unsafe impl Sync for Foo where (): Send {} diff --git a/tests/target/configs/where_single_line/true.rs b/tests/target/configs/where_single_line/true.rs index c4a7f5d38020..7f816459e3c6 100644 --- a/tests/target/configs/where_single_line/true.rs +++ b/tests/target/configs/where_single_line/true.rs @@ -26,3 +26,5 @@ fn lorem() -> T where Ipsum: Eq { // body } + +unsafe impl Sync for Foo where (): Send {} diff --git a/tests/target/impl.rs b/tests/target/impl.rs index 5895c74bcc9f..b5ab07d623e1 100644 --- a/tests/target/impl.rs +++ b/tests/target/impl.rs @@ -28,7 +28,7 @@ impl Foo for T where // comment2 // blah - T: Clone, + T: Clone { } diff --git a/tests/target/impls.rs b/tests/target/impls.rs index 2175b5d7bd9d..3b0cb0a30d61 100644 --- a/tests/target/impls.rs +++ b/tests/target/impls.rs @@ -48,11 +48,7 @@ where } } -impl Foo for Bar -where - T: Baz, -{ -} +impl Foo for Bar where T: Baz {} impl Foo for Bar where @@ -133,11 +129,7 @@ mod m { } } - impl PartialEq for S - where - T: PartialEq, - { - } + impl PartialEq for S where T: PartialEq {} } impl From 8874c95a00c5c16ac6dbdeb1b3539d3fb8a7e021 Mon Sep 17 00:00:00 2001 From: csmoe <35686186+csmoe@users.noreply.github.com> Date: Mon, 4 Jun 2018 19:10:09 +0800 Subject: [PATCH 4/5] recover suppressed comma --- src/items.rs | 5 +++++ tests/target/impl.rs | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/items.rs b/src/items.rs index 872d736f15f0..2e776087aee3 100644 --- a/src/items.rs +++ b/src/items.rs @@ -696,6 +696,11 @@ pub fn format_impl( if is_impl_single_line(context, items, &result, &where_clause_str, item)? { result.push_str(&where_clause_str); if where_clause_str.contains('\n') || last_line_contains_single_line_comment(&result) { + // if the where_clause contains extra comments AND there is only one where clause predicate + // recover the suppressed comma in single line where_clause formatting + if generics.where_clause.predicates.len() == 1 { + result.push_str(","); + } result.push_str(&format!("{}{{{}}}", &sep, &sep)); } else { result.push_str(" {}"); diff --git a/tests/target/impl.rs b/tests/target/impl.rs index b5ab07d623e1..5895c74bcc9f 100644 --- a/tests/target/impl.rs +++ b/tests/target/impl.rs @@ -28,7 +28,7 @@ impl Foo for T where // comment2 // blah - T: Clone + T: Clone, { } From faa41168a96c40e1e3c9134c2bf915e8cebf8833 Mon Sep 17 00:00:00 2001 From: csmoe <35686186+csmoe@users.noreply.github.com> Date: Tue, 5 Jun 2018 07:35:51 +0800 Subject: [PATCH 5/5] format exceeded comments --- src/items.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/items.rs b/src/items.rs index 2e776087aee3..81e86b721969 100644 --- a/src/items.rs +++ b/src/items.rs @@ -696,7 +696,8 @@ pub fn format_impl( if is_impl_single_line(context, items, &result, &where_clause_str, item)? { result.push_str(&where_clause_str); if where_clause_str.contains('\n') || last_line_contains_single_line_comment(&result) { - // if the where_clause contains extra comments AND there is only one where clause predicate + // if the where_clause contains extra comments AND + // there is only one where clause predicate // recover the suppressed comma in single line where_clause formatting if generics.where_clause.predicates.len() == 1 { result.push_str(",");