Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 35 additions & 12 deletions src/formatting/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -1309,7 +1310,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(
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -2988,13 +2989,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)]
Expand Down
11 changes: 11 additions & 0 deletions tests/source/issue-2781.rs
Original file line number Diff line number Diff line change
@@ -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 {}
}
11 changes: 11 additions & 0 deletions tests/target/issue-2781.rs
Original file line number Diff line number Diff line change
@@ -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 {}
}