about summary refs log tree commit diff
diff options
context:
space:
mode:
authorhafiz <20735482+ayazhafiz@users.noreply.github.com>2020-06-07 20:31:24 -0500
committerCaleb Cartwright <calebcartwright@users.noreply.github.com>2020-11-29 13:26:58 -0600
commit5e7fb4553394e0d6439d0d2131dd9f65245d49a2 (patch)
tree2b8d04d1f3ef16a4944fb8ad181815c8aa32c831
parentfb7e604538bb45bf6119ca264c332c9aa634ef31 (diff)
downloadrust-5e7fb4553394e0d6439d0d2131dd9f65245d49a2.tar.gz
rust-5e7fb4553394e0d6439d0d2131dd9f65245d49a2.zip
Pick up comments between visibility modifier and item name (#4239)
* Pick up comments between visibility modifier and item name

I don't think this hurts to fix. #2781, which surfaced this issue, has
a number of comments relating to similar but slightly different issues
(i.e. dropped comments in other places). I can mark #2781 as closed and
then will open new issues for the comments that are not already resolved
or tracked.

Closes #2781

* fixup! Pick up comments between visibility modifier and item name

* fixup! Pick up comments between visibility modifier and item name
-rw-r--r--src/items.rs47
-rw-r--r--tests/source/issue-2781.rs11
-rw-r--r--tests/target/issue-2781.rs11
3 files changed, 57 insertions, 12 deletions
diff --git a/src/items.rs b/src/items.rs
index 039dd1e1ae0..cd8364fd686 100644
--- a/src/items.rs
+++ b/src/items.rs
@@ -401,7 +401,8 @@ impl<'a> FmtVisitor<'a> {
         generics: &ast::Generics,
         span: Span,
     ) {
-        let enum_header = format_header(&self.get_context(), "enum ", ident, vis);
+        let enum_header =
+            format_header(&self.get_context(), "enum ", ident, vis, self.block_indent);
         self.push_str(&enum_header);
 
         let enum_snippet = self.snippet(span);
@@ -952,8 +953,8 @@ pub(crate) struct StructParts<'a> {
 }
 
 impl<'a> StructParts<'a> {
-    fn format_header(&self, context: &RewriteContext<'_>) -> String {
-        format_header(context, self.prefix, self.ident, self.vis)
+    fn format_header(&self, context: &RewriteContext<'_>, offset: Indent) -> String {
+        format_header(context, self.prefix, self.ident, self.vis, offset)
     }
 
     fn from_variant(variant: &'a ast::Variant) -> Self {
@@ -1236,7 +1237,7 @@ fn format_unit_struct(
     p: &StructParts<'_>,
     offset: Indent,
 ) -> Option<String> {
-    let header_str = format_header(context, p.prefix, p.ident, p.vis);
+    let header_str = format_header(context, p.prefix, p.ident, p.vis, offset);
     let generics_str = if let Some(generics) = p.generics {
         let hi = context.snippet_provider.span_before(p.span, ";");
         format_generics(
@@ -1265,7 +1266,7 @@ pub(crate) fn format_struct_struct(
     let mut result = String::with_capacity(1024);
     let span = struct_parts.span;
 
-    let header_str = struct_parts.format_header(context);
+    let header_str = struct_parts.format_header(context, offset);
     result.push_str(&header_str);
 
     let header_hi = struct_parts.ident.span.hi();
@@ -1401,7 +1402,7 @@ fn format_tuple_struct(
     let mut result = String::with_capacity(1024);
     let span = struct_parts.span;
 
-    let header_str = struct_parts.format_header(context);
+    let header_str = struct_parts.format_header(context, offset);
     result.push_str(&header_str);
 
     let body_lo = if fields.is_empty() {
@@ -2920,13 +2921,35 @@ fn format_header(
     item_name: &str,
     ident: symbol::Ident,
     vis: &ast::Visibility,
+    offset: Indent,
 ) -> String {
-    format!(
-        "{}{}{}",
-        format_visibility(context, vis),
-        item_name,
-        rewrite_ident(context, ident)
-    )
+    let mut result = String::with_capacity(128);
+    let shape = Shape::indented(offset, context.config);
+
+    result.push_str(&format_visibility(context, vis).trim());
+
+    // Check for a missing comment between the visibility and the item name.
+    let after_vis = vis.span.hi();
+    if let Some(before_item_name) = context
+        .snippet_provider
+        .opt_span_before(mk_sp(vis.span().lo(), ident.span.hi()), item_name.trim())
+    {
+        let missing_span = mk_sp(after_vis, before_item_name);
+        if let Some(result_with_comment) = combine_strs_with_missing_comments(
+            context,
+            &result,
+            item_name,
+            missing_span,
+            shape,
+            /* allow_extend */ true,
+        ) {
+            result = result_with_comment;
+        }
+    }
+
+    result.push_str(&rewrite_ident(context, ident));
+
+    result
 }
 
 #[derive(PartialEq, Eq, Clone, Copy)]
diff --git a/tests/source/issue-2781.rs b/tests/source/issue-2781.rs
new file mode 100644
index 00000000000..2c15b29b6dc
--- /dev/null
+++ b/tests/source/issue-2781.rs
@@ -0,0 +1,11 @@
+pub      // Oh, no. A line comment.
+struct Foo {}
+
+pub     /* Oh, no. A block comment. */     struct Foo {}
+
+mod inner {
+pub      // Oh, no. A line comment.
+struct Foo {}
+
+pub     /* Oh, no. A block comment. */     struct Foo {}
+}
diff --git a/tests/target/issue-2781.rs b/tests/target/issue-2781.rs
new file mode 100644
index 00000000000..f144d716be9
--- /dev/null
+++ b/tests/target/issue-2781.rs
@@ -0,0 +1,11 @@
+pub // Oh, no. A line comment.
+struct Foo {}
+
+pub /* Oh, no. A block comment. */ struct Foo {}
+
+mod inner {
+    pub // Oh, no. A line comment.
+    struct Foo {}
+
+    pub /* Oh, no. A block comment. */ struct Foo {}
+}