Skip to content

Commit 5941299

Browse files
authored
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
1 parent a441d5e commit 5941299

File tree

3 files changed

+57
-12
lines changed

3 files changed

+57
-12
lines changed

src/formatting/items.rs

+35-12
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,8 @@ impl<'a> FmtVisitor<'a> {
478478
generics: &ast::Generics,
479479
span: Span,
480480
) {
481-
let enum_header = format_header(&self.get_context(), "enum ", ident, vis);
481+
let enum_header =
482+
format_header(&self.get_context(), "enum ", ident, vis, self.block_indent);
482483
self.push_str(&enum_header);
483484

484485
let enum_snippet = self.snippet(span);
@@ -1024,8 +1025,8 @@ pub(crate) struct StructParts<'a> {
10241025
}
10251026

10261027
impl<'a> StructParts<'a> {
1027-
fn format_header(&self, context: &RewriteContext<'_>) -> String {
1028-
format_header(context, self.prefix, self.ident, self.vis)
1028+
fn format_header(&self, context: &RewriteContext<'_>, offset: Indent) -> String {
1029+
format_header(context, self.prefix, self.ident, self.vis, offset)
10291030
}
10301031

10311032
fn from_variant(variant: &'a ast::Variant) -> Self {
@@ -1309,7 +1310,7 @@ fn format_unit_struct(
13091310
p: &StructParts<'_>,
13101311
offset: Indent,
13111312
) -> Option<String> {
1312-
let header_str = format_header(context, p.prefix, p.ident, p.vis);
1313+
let header_str = format_header(context, p.prefix, p.ident, p.vis, offset);
13131314
let generics_str = if let Some(generics) = p.generics {
13141315
let hi = context.snippet_provider.span_before(p.span, ";");
13151316
format_generics(
@@ -1338,7 +1339,7 @@ pub(crate) fn format_struct_struct(
13381339
let mut result = String::with_capacity(1024);
13391340
let span = struct_parts.span;
13401341

1341-
let header_str = struct_parts.format_header(context);
1342+
let header_str = struct_parts.format_header(context, offset);
13421343
result.push_str(&header_str);
13431344

13441345
let header_hi = struct_parts.ident.span.hi();
@@ -1476,7 +1477,7 @@ fn format_tuple_struct(
14761477
let mut result = String::with_capacity(1024);
14771478
let span = struct_parts.span;
14781479

1479-
let header_str = struct_parts.format_header(context);
1480+
let header_str = struct_parts.format_header(context, offset);
14801481
result.push_str(&header_str);
14811482

14821483
let body_lo = if fields.is_empty() {
@@ -2988,13 +2989,35 @@ fn format_header(
29882989
item_name: &str,
29892990
ident: symbol::Ident,
29902991
vis: &ast::Visibility,
2992+
offset: Indent,
29912993
) -> String {
2992-
format!(
2993-
"{}{}{}",
2994-
format_visibility(context, vis),
2995-
item_name,
2996-
rewrite_ident(context, ident)
2997-
)
2994+
let mut result = String::with_capacity(128);
2995+
let shape = Shape::indented(offset, context.config);
2996+
2997+
result.push_str(&format_visibility(context, vis).trim());
2998+
2999+
// Check for a missing comment between the visibility and the item name.
3000+
let after_vis = vis.span.hi();
3001+
if let Some(before_item_name) = context
3002+
.snippet_provider
3003+
.opt_span_before(mk_sp(vis.span().lo(), ident.span.hi()), item_name.trim())
3004+
{
3005+
let missing_span = mk_sp(after_vis, before_item_name);
3006+
if let Some(result_with_comment) = combine_strs_with_missing_comments(
3007+
context,
3008+
&result,
3009+
item_name,
3010+
missing_span,
3011+
shape,
3012+
/* allow_extend */ true,
3013+
) {
3014+
result = result_with_comment;
3015+
}
3016+
}
3017+
3018+
result.push_str(&rewrite_ident(context, ident));
3019+
3020+
result
29983021
}
29993022

30003023
#[derive(PartialEq, Eq, Clone, Copy)]

tests/source/issue-2781.rs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
pub // Oh, no. A line comment.
2+
struct Foo {}
3+
4+
pub /* Oh, no. A block comment. */ struct Foo {}
5+
6+
mod inner {
7+
pub // Oh, no. A line comment.
8+
struct Foo {}
9+
10+
pub /* Oh, no. A block comment. */ struct Foo {}
11+
}

tests/target/issue-2781.rs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
pub // Oh, no. A line comment.
2+
struct Foo {}
3+
4+
pub /* Oh, no. A block comment. */ struct Foo {}
5+
6+
mod inner {
7+
pub // Oh, no. A line comment.
8+
struct Foo {}
9+
10+
pub /* Oh, no. A block comment. */ struct Foo {}
11+
}

0 commit comments

Comments
 (0)