Fix formatting of comments in empty structs (#5171)
* Fix formatting of comments in empty structs * Add tests * Add single line tests * Fix block comments * Revert changes of test source files
This commit is contained in:
parent
5c558e2c11
commit
b4a4bf0bf8
10
src/items.rs
10
src/items.rs
@ -1374,17 +1374,21 @@ fn format_empty_struct_or_tuple(
|
||||
result.push_str(&offset.to_string_with_newline(context.config))
|
||||
}
|
||||
result.push_str(opener);
|
||||
match rewrite_missing_comment(span, Shape::indented(offset, context.config), context) {
|
||||
|
||||
// indented shape for proper indenting of multi-line comments
|
||||
let shape = Shape::indented(offset.block_indent(context.config), context.config);
|
||||
match rewrite_missing_comment(span, shape, context) {
|
||||
Some(ref s) if s.is_empty() => (),
|
||||
Some(ref s) => {
|
||||
if !is_single_line(s) || first_line_contains_single_line_comment(s) {
|
||||
let is_multi_line = !is_single_line(s);
|
||||
if is_multi_line || first_line_contains_single_line_comment(s) {
|
||||
let nested_indent_str = offset
|
||||
.block_indent(context.config)
|
||||
.to_string_with_newline(context.config);
|
||||
result.push_str(&nested_indent_str);
|
||||
}
|
||||
result.push_str(s);
|
||||
if last_line_contains_single_line_comment(s) {
|
||||
if is_multi_line || last_line_contains_single_line_comment(s) {
|
||||
result.push_str(&offset.to_string_with_newline(context.config));
|
||||
}
|
||||
}
|
||||
|
113
tests/source/issue_4854.rs
Normal file
113
tests/source/issue_4854.rs
Normal file
@ -0,0 +1,113 @@
|
||||
struct Struct {
|
||||
// Multiline comment
|
||||
// should be formatted
|
||||
// properly.
|
||||
}
|
||||
|
||||
struct Struct2 {
|
||||
// This formatting
|
||||
// Should be changed
|
||||
}
|
||||
|
||||
struct Struct3(
|
||||
// This
|
||||
// is
|
||||
// correct
|
||||
);
|
||||
|
||||
struct Struct4(
|
||||
// This
|
||||
// is
|
||||
// not
|
||||
// correct
|
||||
);
|
||||
|
||||
struct Struct5 {
|
||||
/*
|
||||
Comment block
|
||||
with many lines.
|
||||
*/
|
||||
}
|
||||
|
||||
struct Struct6(
|
||||
/*
|
||||
Comment block
|
||||
with many lines.
|
||||
*/
|
||||
);
|
||||
|
||||
struct Struct7 {
|
||||
/*
|
||||
Invalid
|
||||
format
|
||||
*/
|
||||
}
|
||||
|
||||
struct Struct8(
|
||||
/*
|
||||
Invalid
|
||||
format
|
||||
*/
|
||||
);
|
||||
|
||||
struct Struct9 { /* bar */ }
|
||||
|
||||
struct Struct10 { /* bar
|
||||
baz
|
||||
*/ }
|
||||
|
||||
mod module {
|
||||
struct Struct {
|
||||
// Multiline comment
|
||||
// should be formatted
|
||||
// properly.
|
||||
}
|
||||
|
||||
struct Struct2 {
|
||||
// This formatting
|
||||
// Should be changed
|
||||
}
|
||||
|
||||
struct Struct3(
|
||||
// This
|
||||
// is
|
||||
// correct
|
||||
);
|
||||
|
||||
struct Struct4(
|
||||
// This
|
||||
// is
|
||||
// not
|
||||
// correct
|
||||
);
|
||||
|
||||
struct Struct5 {
|
||||
/*
|
||||
Comment block
|
||||
with many lines.
|
||||
*/
|
||||
}
|
||||
|
||||
struct Struct6(
|
||||
/*
|
||||
Comment block
|
||||
with many lines.
|
||||
*/
|
||||
);
|
||||
|
||||
struct Struct7 {
|
||||
/*
|
||||
Invalid
|
||||
format
|
||||
*/
|
||||
}
|
||||
|
||||
struct Struct8(
|
||||
/*
|
||||
Invalid
|
||||
format
|
||||
*/
|
||||
);
|
||||
|
||||
struct Struct9 { /* bar */ }
|
||||
}
|
@ -25,9 +25,8 @@ pub enum E {
|
||||
}
|
||||
|
||||
pub enum E2 {
|
||||
// This can be changed once https://github.com/rust-lang/rustfmt/issues/4854 is fixed
|
||||
// Expand as needed, numbers should be ascending according to the stage
|
||||
// through the inclusion pipeline, or according to the descriptions
|
||||
// Expand as needed, numbers should be ascending according to the stage
|
||||
// through the inclusion pipeline, or according to the descriptions
|
||||
}
|
||||
|
||||
pub struct S {
|
||||
@ -42,9 +41,8 @@ pub struct S {
|
||||
}
|
||||
|
||||
pub struct S2 {
|
||||
// This can be changed once https://github.com/rust-lang/rustfmt/issues/4854 is fixed
|
||||
// Expand as needed, numbers should be ascending according to the stage
|
||||
// through the inclusion pipeline, or according to the descriptions
|
||||
// Expand as needed, numbers should be ascending according to the stage
|
||||
// through the inclusion pipeline, or according to the descriptions
|
||||
}
|
||||
|
||||
fn foo(
|
||||
|
@ -13,9 +13,8 @@ pub enum E {
|
||||
}
|
||||
|
||||
pub enum E2 {
|
||||
// This can be changed once https://github.com/rust-lang/rustfmt/issues/4854 is fixed
|
||||
// Expand as needed, numbers should be ascending according to the stage
|
||||
// through the inclusion pipeline, or according to the descriptions
|
||||
// Expand as needed, numbers should be ascending according to the stage
|
||||
// through the inclusion pipeline, or according to the descriptions
|
||||
}
|
||||
|
||||
pub struct S {
|
||||
@ -30,9 +29,8 @@ pub struct S {
|
||||
}
|
||||
|
||||
pub struct S2 {
|
||||
// This can be changed once https://github.com/rust-lang/rustfmt/issues/4854 is fixed
|
||||
// Expand as needed, numbers should be ascending according to the stage
|
||||
// through the inclusion pipeline, or according to the descriptions
|
||||
// Expand as needed, numbers should be ascending according to the stage
|
||||
// through the inclusion pipeline, or according to the descriptions
|
||||
}
|
||||
|
||||
fn foo(
|
||||
|
@ -14,8 +14,8 @@ pub enum E {
|
||||
|
||||
pub enum E2 {
|
||||
// This can be changed once https://github.com/rust-lang/rustfmt/issues/4854 is fixed
|
||||
// Expand as needed, numbers should be ascending according to the stage
|
||||
// through the inclusion pipeline, or according to the descriptions
|
||||
// Expand as needed, numbers should be ascending according to the stage
|
||||
// through the inclusion pipeline, or according to the descriptions
|
||||
}
|
||||
|
||||
pub enum E3 {
|
||||
@ -42,8 +42,8 @@ pub struct S {
|
||||
|
||||
pub struct S2 {
|
||||
// This can be changed once https://github.com/rust-lang/rustfmt/issues/4854 is fixed
|
||||
// Expand as needed, numbers should be ascending according to the stage
|
||||
// through the inclusion pipeline, or according to the descriptions
|
||||
// Expand as needed, numbers should be ascending according to the stage
|
||||
// through the inclusion pipeline, or according to the descriptions
|
||||
}
|
||||
|
||||
pub struct S3 {
|
||||
|
@ -15,8 +15,8 @@ pub enum E {
|
||||
|
||||
pub enum E2 {
|
||||
// This can be changed once https://github.com/rust-lang/rustfmt/issues/4854 is fixed
|
||||
// Expand as needed, numbers should be ascending according to the stage
|
||||
// through the inclusion pipeline, or according to the descriptions
|
||||
// Expand as needed, numbers should be ascending according to the stage
|
||||
// through the inclusion pipeline, or according to the descriptions
|
||||
}
|
||||
|
||||
pub enum E3 {
|
||||
@ -43,8 +43,8 @@ pub struct S {
|
||||
|
||||
pub struct S2 {
|
||||
// This can be changed once https://github.com/rust-lang/rustfmt/issues/4854 is fixed
|
||||
// Expand as needed, numbers should be ascending according to the stage
|
||||
// through the inclusion pipeline, or according to the descriptions
|
||||
// Expand as needed, numbers should be ascending according to the stage
|
||||
// through the inclusion pipeline, or according to the descriptions
|
||||
}
|
||||
|
||||
pub struct S3 {
|
||||
|
115
tests/target/issue_4854.rs
Normal file
115
tests/target/issue_4854.rs
Normal file
@ -0,0 +1,115 @@
|
||||
struct Struct {
|
||||
// Multiline comment
|
||||
// should be formatted
|
||||
// properly.
|
||||
}
|
||||
|
||||
struct Struct2 {
|
||||
// This formatting
|
||||
// Should be changed
|
||||
}
|
||||
|
||||
struct Struct3(
|
||||
// This
|
||||
// is
|
||||
// correct
|
||||
);
|
||||
|
||||
struct Struct4(
|
||||
// This
|
||||
// is
|
||||
// not
|
||||
// correct
|
||||
);
|
||||
|
||||
struct Struct5 {
|
||||
/*
|
||||
Comment block
|
||||
with many lines.
|
||||
*/
|
||||
}
|
||||
|
||||
struct Struct6(
|
||||
/*
|
||||
Comment block
|
||||
with many lines.
|
||||
*/
|
||||
);
|
||||
|
||||
struct Struct7 {
|
||||
/*
|
||||
Invalid
|
||||
format
|
||||
*/
|
||||
}
|
||||
|
||||
struct Struct8(
|
||||
/*
|
||||
Invalid
|
||||
format
|
||||
*/
|
||||
);
|
||||
|
||||
struct Struct9 {/* bar */}
|
||||
|
||||
struct Struct10 {
|
||||
/* bar
|
||||
baz
|
||||
*/
|
||||
}
|
||||
|
||||
mod module {
|
||||
struct Struct {
|
||||
// Multiline comment
|
||||
// should be formatted
|
||||
// properly.
|
||||
}
|
||||
|
||||
struct Struct2 {
|
||||
// This formatting
|
||||
// Should be changed
|
||||
}
|
||||
|
||||
struct Struct3(
|
||||
// This
|
||||
// is
|
||||
// correct
|
||||
);
|
||||
|
||||
struct Struct4(
|
||||
// This
|
||||
// is
|
||||
// not
|
||||
// correct
|
||||
);
|
||||
|
||||
struct Struct5 {
|
||||
/*
|
||||
Comment block
|
||||
with many lines.
|
||||
*/
|
||||
}
|
||||
|
||||
struct Struct6(
|
||||
/*
|
||||
Comment block
|
||||
with many lines.
|
||||
*/
|
||||
);
|
||||
|
||||
struct Struct7 {
|
||||
/*
|
||||
Invalid
|
||||
format
|
||||
*/
|
||||
}
|
||||
|
||||
struct Struct8(
|
||||
/*
|
||||
Invalid
|
||||
format
|
||||
*/
|
||||
);
|
||||
|
||||
struct Struct9 {/* bar */}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user