From 32e624098a4b5f9f4e0e789a362db3e3f9cc7bdd Mon Sep 17 00:00:00 2001 From: Ayaz Hafiz Date: Sat, 6 Jun 2020 09:23:48 -0700 Subject: [PATCH 1/3] 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 --- src/formatting/items.rs | 44 +++++++++++++++++++++++++++----------- tests/source/issue-2781.rs | 11 ++++++++++ tests/target/issue-2781.rs | 11 ++++++++++ 3 files changed, 54 insertions(+), 12 deletions(-) create mode 100644 tests/source/issue-2781.rs create mode 100644 tests/target/issue-2781.rs diff --git a/src/formatting/items.rs b/src/formatting/items.rs index 5ea8aa73197..31ba504c000 100644 --- a/src/formatting/items.rs +++ b/src/formatting/items.rs @@ -478,7 +478,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); @@ -1024,8 +1025,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 { @@ -1309,7 +1310,7 @@ fn format_unit_struct( p: &StructParts<'_>, offset: Indent, ) -> Option { - 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( @@ -1338,7 +1339,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(); @@ -1476,7 +1477,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() { @@ -2988,13 +2989,32 @@ 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)); + + // Check for a missing comment between the visibility and the item name. + let after_vis = vis.span.hi(); + let before_item_name = context + .snippet_provider + .span_before(mk_sp(vis.span().lo(), ident.span.hi()), item_name.trim()); + if let Some(cmt) = rewrite_missing_comment(mk_sp(after_vis, before_item_name), shape, context) { + result.push_str(&cmt); + let need_newline = last_line_contains_single_line_comment(&result); + if need_newline { + result.push_str(&offset.to_string_with_newline(context.config)); + } else if cmt.len() > 0 { + result.push(' '); + } + } + + result.push_str(item_name); + 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 {} +} From 3716aba306a18faa8723a61da4aadf9a796b2958 Mon Sep 17 00:00:00 2001 From: Ayaz Hafiz Date: Sat, 6 Jun 2020 11:27:04 -0700 Subject: [PATCH 2/3] fixup! Pick up comments between visibility modifier and item name --- src/formatting/items.rs | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/formatting/items.rs b/src/formatting/items.rs index 31ba504c000..8d01ad6255c 100644 --- a/src/formatting/items.rs +++ b/src/formatting/items.rs @@ -2998,16 +2998,20 @@ fn format_header( // Check for a missing comment between the visibility and the item name. let after_vis = vis.span.hi(); - let before_item_name = context + if let Some(before_item_name) = context .snippet_provider - .span_before(mk_sp(vis.span().lo(), ident.span.hi()), item_name.trim()); - if let Some(cmt) = rewrite_missing_comment(mk_sp(after_vis, before_item_name), shape, context) { - result.push_str(&cmt); - let need_newline = last_line_contains_single_line_comment(&result); - if need_newline { - result.push_str(&offset.to_string_with_newline(context.config)); - } else if cmt.len() > 0 { - result.push(' '); + .opt_span_before(mk_sp(vis.span().lo(), ident.span.hi()), item_name.trim()) + { + if let Some(cmt) = + rewrite_missing_comment(mk_sp(after_vis, before_item_name), shape, context) + { + result.push_str(&cmt); + let need_newline = last_line_contains_single_line_comment(&result); + if need_newline { + result.push_str(&offset.to_string_with_newline(context.config)); + } else if cmt.len() > 0 { + result.push(' '); + } } } From 82ae83f51c79b546bf81da25a1c6a8ba632b2c2c Mon Sep 17 00:00:00 2001 From: Ayaz Hafiz Date: Sat, 6 Jun 2020 20:45:39 -0700 Subject: [PATCH 3/3] fixup! Pick up comments between visibility modifier and item name --- src/formatting/items.rs | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/src/formatting/items.rs b/src/formatting/items.rs index 8d01ad6255c..3b446b76a53 100644 --- a/src/formatting/items.rs +++ b/src/formatting/items.rs @@ -2994,7 +2994,7 @@ fn format_header( let mut result = String::with_capacity(128); let shape = Shape::indented(offset, context.config); - result.push_str(&format_visibility(context, vis)); + 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(); @@ -3002,20 +3002,19 @@ fn format_header( .snippet_provider .opt_span_before(mk_sp(vis.span().lo(), ident.span.hi()), item_name.trim()) { - if let Some(cmt) = - rewrite_missing_comment(mk_sp(after_vis, before_item_name), shape, context) - { - result.push_str(&cmt); - let need_newline = last_line_contains_single_line_comment(&result); - if need_newline { - result.push_str(&offset.to_string_with_newline(context.config)); - } else if cmt.len() > 0 { - result.push(' '); - } + 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(item_name); result.push_str(&rewrite_ident(context, ident)); result