Skip to content

Commit 5e7fb45

Browse files
ayazhafizcalebcartwright
authored andcommitted
Pick up comments between visibility modifier and item name (rust-lang#4239)
* Pick up comments between visibility modifier and item name I don't think this hurts to fix. rust-lang#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 rust-lang#2781 as closed and then will open new issues for the comments that are not already resolved or tracked. Closes rust-lang#2781 * fixup! Pick up comments between visibility modifier and item name * fixup! Pick up comments between visibility modifier and item name
1 parent fb7e604 commit 5e7fb45

File tree

3 files changed

+57
-12
lines changed

3 files changed

+57
-12
lines changed

src/items.rs

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,8 @@ impl<'a> FmtVisitor<'a> {
401401
generics: &ast::Generics,
402402
span: Span,
403403
) {
404-
let enum_header = format_header(&self.get_context(), "enum ", ident, vis);
404+
let enum_header =
405+
format_header(&self.get_context(), "enum ", ident, vis, self.block_indent);
405406
self.push_str(&enum_header);
406407

407408
let enum_snippet = self.snippet(span);
@@ -952,8 +953,8 @@ pub(crate) struct StructParts<'a> {
952953
}
953954

954955
impl<'a> StructParts<'a> {
955-
fn format_header(&self, context: &RewriteContext<'_>) -> String {
956-
format_header(context, self.prefix, self.ident, self.vis)
956+
fn format_header(&self, context: &RewriteContext<'_>, offset: Indent) -> String {
957+
format_header(context, self.prefix, self.ident, self.vis, offset)
957958
}
958959

959960
fn from_variant(variant: &'a ast::Variant) -> Self {
@@ -1236,7 +1237,7 @@ fn format_unit_struct(
12361237
p: &StructParts<'_>,
12371238
offset: Indent,
12381239
) -> Option<String> {
1239-
let header_str = format_header(context, p.prefix, p.ident, p.vis);
1240+
let header_str = format_header(context, p.prefix, p.ident, p.vis, offset);
12401241
let generics_str = if let Some(generics) = p.generics {
12411242
let hi = context.snippet_provider.span_before(p.span, ";");
12421243
format_generics(
@@ -1265,7 +1266,7 @@ pub(crate) fn format_struct_struct(
12651266
let mut result = String::with_capacity(1024);
12661267
let span = struct_parts.span;
12671268

1268-
let header_str = struct_parts.format_header(context);
1269+
let header_str = struct_parts.format_header(context, offset);
12691270
result.push_str(&header_str);
12701271

12711272
let header_hi = struct_parts.ident.span.hi();
@@ -1401,7 +1402,7 @@ fn format_tuple_struct(
14011402
let mut result = String::with_capacity(1024);
14021403
let span = struct_parts.span;
14031404

1404-
let header_str = struct_parts.format_header(context);
1405+
let header_str = struct_parts.format_header(context, offset);
14051406
result.push_str(&header_str);
14061407

14071408
let body_lo = if fields.is_empty() {
@@ -2920,13 +2921,35 @@ fn format_header(
29202921
item_name: &str,
29212922
ident: symbol::Ident,
29222923
vis: &ast::Visibility,
2924+
offset: Indent,
29232925
) -> String {
2924-
format!(
2925-
"{}{}{}",
2926-
format_visibility(context, vis),
2927-
item_name,
2928-
rewrite_ident(context, ident)
2929-
)
2926+
let mut result = String::with_capacity(128);
2927+
let shape = Shape::indented(offset, context.config);
2928+
2929+
result.push_str(&format_visibility(context, vis).trim());
2930+
2931+
// Check for a missing comment between the visibility and the item name.
2932+
let after_vis = vis.span.hi();
2933+
if let Some(before_item_name) = context
2934+
.snippet_provider
2935+
.opt_span_before(mk_sp(vis.span().lo(), ident.span.hi()), item_name.trim())
2936+
{
2937+
let missing_span = mk_sp(after_vis, before_item_name);
2938+
if let Some(result_with_comment) = combine_strs_with_missing_comments(
2939+
context,
2940+
&result,
2941+
item_name,
2942+
missing_span,
2943+
shape,
2944+
/* allow_extend */ true,
2945+
) {
2946+
result = result_with_comment;
2947+
}
2948+
}
2949+
2950+
result.push_str(&rewrite_ident(context, ident));
2951+
2952+
result
29302953
}
29312954

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

tests/source/issue-2781.rs

Lines changed: 11 additions & 0 deletions
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

Lines changed: 11 additions & 0 deletions
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)