From 6455e9de0e165ccd2874cf02f602844d2f63d355 Mon Sep 17 00:00:00 2001 From: WhizSid Date: Fri, 9 Oct 2020 05:57:04 +0530 Subject: [PATCH] Fixed 'Comment removed between type name and =' issue (#4448) * Fixed Comment removed between type name and = issue * Fixed where clause issue and pass the full span * has_where condition inline * Fixed indentation error on where clause * Removed tmp file --- src/items.rs | 41 +++++++++++++++++++++++++++++++++++--- src/visitor.rs | 4 ++++ tests/source/issue-4244.rs | 16 +++++++++++++++ tests/target/issue-4244.rs | 20 +++++++++++++++++++ 4 files changed, 78 insertions(+), 3 deletions(-) create mode 100644 tests/source/issue-4244.rs create mode 100644 tests/target/issue-4244.rs diff --git a/src/items.rs b/src/items.rs index 57156aece33d..6fdca90b12f4 100644 --- a/src/items.rs +++ b/src/items.rs @@ -1495,6 +1495,7 @@ fn rewrite_type( generics: &ast::Generics, generic_bounds_opt: Option<&ast::GenericBounds>, rhs: Option<&R>, + span: Span, ) -> Option { let mut result = String::with_capacity(128); result.push_str(&format!("{}type ", format_visibility(context, vis))); @@ -1541,12 +1542,40 @@ fn rewrite_type( if let Some(ty) = rhs { // If there's a where clause, add a newline before the assignment. Otherwise just add a // space. - if !generics.where_clause.predicates.is_empty() { + let has_where = !generics.where_clause.predicates.is_empty(); + if has_where { result.push_str(&indent.to_string_with_newline(context.config)); } else { result.push(' '); } - let lhs = format!("{}=", result); + + let comment_span = context + .snippet_provider + .opt_span_before(span, "=") + .map(|op_lo| mk_sp(generics.where_clause.span.hi(), op_lo)); + + let lhs = match comment_span { + Some(comment_span) + if contains_comment(context.snippet_provider.span_to_snippet(comment_span)?) => + { + let comment_shape = if has_where { + Shape::indented(indent, context.config) + } else { + Shape::indented(indent, context.config) + .block_left(context.config.tab_spaces())? + }; + + combine_strs_with_missing_comments( + context, + result.trim_end(), + "=", + comment_span, + comment_shape, + true, + )? + } + _ => format!("{}=", result), + }; // 1 = `;` let shape = Shape::indented(indent, context.config).sub_width(1)?; @@ -1563,6 +1592,7 @@ pub(crate) fn rewrite_opaque_type( generic_bounds: &ast::GenericBounds, generics: &ast::Generics, vis: &ast::Visibility, + span: Span, ) -> Option { let opaque_type_bounds = OpaqueTypeBounds { generic_bounds }; rewrite_type( @@ -1573,6 +1603,7 @@ pub(crate) fn rewrite_opaque_type( generics, Some(generic_bounds), Some(&opaque_type_bounds), + span, ) } @@ -1801,6 +1832,7 @@ pub(crate) fn rewrite_type_alias( context: &RewriteContext<'_>, indent: Indent, vis: &ast::Visibility, + span: Span, ) -> Option { rewrite_type( context, @@ -1810,6 +1842,7 @@ pub(crate) fn rewrite_type_alias( generics, generic_bounds_opt, ty_opt, + span, ) } @@ -1859,8 +1892,9 @@ pub(crate) fn rewrite_associated_impl_type( generics: &ast::Generics, context: &RewriteContext<'_>, indent: Indent, + span: Span, ) -> Option { - let result = rewrite_type_alias(ident, ty_opt, generics, None, context, indent, vis)?; + let result = rewrite_type_alias(ident, ty_opt, generics, None, context, indent, vis, span)?; match defaultness { ast::Defaultness::Default(..) => Some(format!("default {}", result)), @@ -3118,6 +3152,7 @@ impl Rewrite for ast::ForeignItem { &context, shape.indent, &self.vis, + self.span, ), ast::ForeignItemKind::MacCall(ref mac) => { rewrite_macro(mac, None, context, shape, MacroPosition::Item) diff --git a/src/visitor.rs b/src/visitor.rs index 3b9b0a2f967a..fefeffac95c8 100644 --- a/src/visitor.rs +++ b/src/visitor.rs @@ -572,6 +572,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> { &self.get_context(), self.block_indent, &item.vis, + item.span, ); self.push_rewrite(item.span, rewrite); } @@ -583,6 +584,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> { generic_bounds, generics, &item.vis, + item.span, ); self.push_rewrite(item.span, rewrite); } @@ -649,6 +651,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> { &self.get_context(), self.block_indent, &ti.vis, + ti.span, ); self.push_rewrite(ti.span, rewrite); } @@ -695,6 +698,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> { &generics, &self.get_context(), self.block_indent, + ii.span, ) }; let rewrite = match ty { diff --git a/tests/source/issue-4244.rs b/tests/source/issue-4244.rs new file mode 100644 index 000000000000..34b51085e131 --- /dev/null +++ b/tests/source/issue-4244.rs @@ -0,0 +1,16 @@ +pub struct SS {} + +pub type A /* A Comment */ = SS; + +pub type B // Comment +// B += SS; + +pub type C + /* Comment C */ = SS; + +pub trait D { + type E /* Comment E */ = SS; +} + +type F<'a: 'static, T: Ord + 'static>: Eq + PartialEq where T: 'static + Copy /* x */ = Vec; diff --git a/tests/target/issue-4244.rs b/tests/target/issue-4244.rs new file mode 100644 index 000000000000..8958ba99e80a --- /dev/null +++ b/tests/target/issue-4244.rs @@ -0,0 +1,20 @@ +pub struct SS {} + +pub type A /* A Comment */ = SS; + +pub type B // Comment + // B + = SS; + +pub type C + /* Comment C */ + = SS; + +pub trait D { + type E /* Comment E */ = SS; +} + +type F<'a: 'static, T: Ord + 'static>: Eq + PartialEq +where + T: 'static + Copy, /* x */ += Vec;