Skip to content

Commit 47a6dca

Browse files
committed
Preserve normalized comments after last list item
1 parent 2c442cc commit 47a6dca

File tree

6 files changed

+535
-4
lines changed

6 files changed

+535
-4
lines changed

Diff for: src/lists.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -444,10 +444,15 @@ where
444444
let offset = formatting.shape.indent + overhead;
445445
let comment_shape = Shape::legacy(width, offset);
446446

447-
// Use block-style only for the last item or multiline comments.
448-
let block_style = !formatting.ends_with_newline && last
449-
|| comment.trim().contains('\n')
450-
|| comment.trim().len() > width;
447+
let block_style = if !formatting.ends_with_newline && last {
448+
true
449+
} else if starts_with_newline(comment) {
450+
false
451+
} else if comment.trim().contains('\n') || comment.trim().len() > width {
452+
true
453+
} else {
454+
false
455+
};
451456

452457
rewrite_comment(
453458
comment.trim_start(),
+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
// rustfmt-wrap_comments: true
2+
3+
pub enum E {
4+
// Expand as needed, numbers should be ascending according to the stage
5+
// through the inclusion pipeline, or according to the descriptions
6+
Variant1,
7+
// Expand as needed, numbers should be ascending according to the stage
8+
// through the inclusion pipeline, or according to the descriptions
9+
Variant2,
10+
// Expand as needed, numbers should be ascending according to the stage
11+
// through the inclusion pipeline, or according to the descriptions
12+
}
13+
14+
pub enum E2 {
15+
// This can be changed once https://github.com/rust-lang/rustfmt/issues/4854 is fixed
16+
// Expand as needed, numbers should be ascending according to the stage
17+
// through the inclusion pipeline, or according to the descriptions
18+
}
19+
20+
pub enum E3 {
21+
// Expand as needed, numbers should be ascending according to the stage through the inclusion pipeline, or according to the descriptions
22+
Variant1,
23+
// Expand as needed, numbers should be ascending according to the stage through the inclusion pipeline, or according to the descriptions
24+
Variant2,
25+
// Expand as needed, numbers should be ascending according to the stage through the inclusion pipeline, or according to the descriptions
26+
27+
}
28+
29+
pub struct S {
30+
// Expand as needed, numbers should be ascending according to the stage
31+
// through the inclusion pipeline, or according to the descriptions
32+
some_field: usize,
33+
// Expand as needed, numbers should be ascending according to the stage
34+
// through the inclusion pipeline, or according to the descriptions
35+
last_field: usize,
36+
// Expand as needed, numbers should be ascending according to the stage
37+
// through the inclusion pipeline, or according to the descriptions
38+
}
39+
40+
pub struct S2 {
41+
// This can be changed once https://github.com/rust-lang/rustfmt/issues/4854 is fixed
42+
// Expand as needed, numbers should be ascending according to the stage
43+
// through the inclusion pipeline, or according to the descriptions
44+
}
45+
46+
pub struct S3 {
47+
// Expand as needed, numbers should be ascending according to the stage through the inclusion pipeline, or according to the descriptions
48+
some_field: usize,
49+
// Expand as needed, numbers should be ascending according to the stage through the inclusion pipeline, or according to the descriptions
50+
last_field: usize,
51+
// Expand as needed, numbers should be ascending according to the stage through the inclusion pipeline, or according to the descriptions
52+
}
53+
54+
fn foo(
55+
// Expand as needed, numbers should be ascending according to the stage
56+
// through the inclusion pipeline, or according to the descriptions
57+
a: usize,
58+
// Expand as needed, numbers should be ascending according to the stage
59+
// through the inclusion pipeline, or according to the descriptions
60+
b: usize,
61+
// Expand as needed, numbers should be ascending according to the stage
62+
// through the inclusion pipeline, or according to the descriptions
63+
) -> usize {
64+
5
65+
}
66+
67+
fn foo2(// Expand as needed, numbers should be ascending according to the stage
68+
// through the inclusion pipeline, or according to the descriptions
69+
) -> usize {
70+
5
71+
}
72+
73+
fn foo3(
74+
// Expand as needed, numbers should be ascending according to the stage through the inclusion pipeline, or according to the descriptions
75+
a: usize,
76+
// Expand as needed, numbers should be ascending according to the stage through the inclusion pipeline, or according to the descriptions
77+
b: usize,
78+
// Expand as needed, numbers should be ascending according to the stage through the inclusion pipeline, or according to the descriptions
79+
80+
) -> usize {
81+
5
82+
}
83+
84+
fn main() {
85+
let v = vec![
86+
// Expand as needed, numbers should be ascending according to the stage
87+
// through the inclusion pipeline, or according to the descriptions
88+
1,
89+
// Expand as needed, numbers should be ascending according to the stage
90+
// through the inclusion pipeline, or according to the descriptions
91+
2,
92+
// Expand as needed, numbers should be ascending according to the stage
93+
// through the inclusion pipeline, or according to the descriptions
94+
];
95+
96+
let v2: Vec<i32> = vec![
97+
// Expand as needed, numbers should be ascending according to the stage
98+
// through the inclusion pipeline, or according to the descriptions
99+
];
100+
101+
let v3 = vec![
102+
// Expand as needed, numbers should be ascending according to the stage through the inclusion pipeline, or according to the descriptions
103+
1,
104+
// Expand as needed, numbers should be ascending according to the stage through the inclusion pipeline, or according to the descriptions
105+
2,
106+
// Expand as needed, numbers should be ascending according to the stage through the inclusion pipeline, or according to the descriptions
107+
];
108+
}

Diff for: tests/source/issue-4909/wrap-comments-true.rs

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
// rustfmt-normalize_comments: true
2+
// rustfmt-wrap_comments: true
3+
4+
pub enum E {
5+
// Expand as needed, numbers should be ascending according to the stage
6+
// through the inclusion pipeline, or according to the descriptions
7+
Variant1,
8+
// Expand as needed, numbers should be ascending according to the stage
9+
// through the inclusion pipeline, or according to the descriptions
10+
Variant2,
11+
// Expand as needed, numbers should be ascending according to the stage
12+
// through the inclusion pipeline, or according to the descriptions
13+
}
14+
15+
pub enum E2 {
16+
// This can be changed once https://github.com/rust-lang/rustfmt/issues/4854 is fixed
17+
// Expand as needed, numbers should be ascending according to the stage
18+
// through the inclusion pipeline, or according to the descriptions
19+
}
20+
21+
pub enum E3 {
22+
// Expand as needed, numbers should be ascending according to the stage through the inclusion pipeline, or according to the descriptions
23+
Variant1,
24+
// Expand as needed, numbers should be ascending according to the stage through the inclusion pipeline, or according to the descriptions
25+
Variant2,
26+
// Expand as needed, numbers should be ascending according to the stage through the inclusion pipeline, or according to the descriptions
27+
28+
}
29+
30+
pub struct S {
31+
// Expand as needed, numbers should be ascending according to the stage
32+
// through the inclusion pipeline, or according to the descriptions
33+
some_field: usize,
34+
// Expand as needed, numbers should be ascending according to the stage
35+
// through the inclusion pipeline, or according to the descriptions
36+
last_field: usize,
37+
// Expand as needed, numbers should be ascending according to the stage
38+
// through the inclusion pipeline, or according to the descriptions
39+
}
40+
41+
pub struct S2 {
42+
// This can be changed once https://github.com/rust-lang/rustfmt/issues/4854 is fixed
43+
// Expand as needed, numbers should be ascending according to the stage
44+
// through the inclusion pipeline, or according to the descriptions
45+
}
46+
47+
pub struct S3 {
48+
// Expand as needed, numbers should be ascending according to the stage through the inclusion pipeline, or according to the descriptions
49+
some_field: usize,
50+
// Expand as needed, numbers should be ascending according to the stage through the inclusion pipeline, or according to the descriptions
51+
last_field: usize,
52+
// Expand as needed, numbers should be ascending according to the stage through the inclusion pipeline, or according to the descriptions
53+
}
54+
55+
fn foo(
56+
// Expand as needed, numbers should be ascending according to the stage
57+
// through the inclusion pipeline, or according to the descriptions
58+
a: usize,
59+
// Expand as needed, numbers should be ascending according to the stage
60+
// through the inclusion pipeline, or according to the descriptions
61+
b: usize,
62+
// Expand as needed, numbers should be ascending according to the stage
63+
// through the inclusion pipeline, or according to the descriptions
64+
) -> usize {
65+
5
66+
}
67+
68+
fn foo2(// Expand as needed, numbers should be ascending according to the stage
69+
// through the inclusion pipeline, or according to the descriptions
70+
) -> usize {
71+
5
72+
}
73+
74+
fn foo3(
75+
// Expand as needed, numbers should be ascending according to the stage through the inclusion pipeline, or according to the descriptions
76+
a: usize,
77+
// Expand as needed, numbers should be ascending according to the stage through the inclusion pipeline, or according to the descriptions
78+
b: usize,
79+
// Expand as needed, numbers should be ascending according to the stage through the inclusion pipeline, or according to the descriptions
80+
81+
) -> usize {
82+
5
83+
}
84+
85+
fn main() {
86+
let v = vec![
87+
// Expand as needed, numbers should be ascending according to the stage
88+
// through the inclusion pipeline, or according to the descriptions
89+
1,
90+
// Expand as needed, numbers should be ascending according to the stage
91+
// through the inclusion pipeline, or according to the descriptions
92+
2,
93+
// Expand as needed, numbers should be ascending according to the stage
94+
// through the inclusion pipeline, or according to the descriptions
95+
];
96+
97+
let v2: Vec<i32> = vec![
98+
// Expand as needed, numbers should be ascending according to the stage
99+
// through the inclusion pipeline, or according to the descriptions
100+
];
101+
102+
let v3 = vec![
103+
// Expand as needed, numbers should be ascending according to the stage through the inclusion pipeline, or according to the descriptions
104+
1,
105+
// Expand as needed, numbers should be ascending according to the stage through the inclusion pipeline, or according to the descriptions
106+
2,
107+
// Expand as needed, numbers should be ascending according to the stage through the inclusion pipeline, or according to the descriptions
108+
];
109+
}

Diff for: tests/target/issue-4909/wrap-comments-false.rs

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// rustfmt-normalize_comments: true
2+
3+
pub enum E {
4+
// Expand as needed, numbers should be ascending according to the stage
5+
// through the inclusion pipeline, or according to the descriptions
6+
Variant1,
7+
// Expand as needed, numbers should be ascending according to the stage
8+
// through the inclusion pipeline, or according to the descriptions
9+
Variant2,
10+
// Expand as needed, numbers should be ascending according to the stage
11+
// through the inclusion pipeline, or according to the descriptions
12+
}
13+
14+
pub enum E2 {
15+
// This can be changed once https://github.com/rust-lang/rustfmt/issues/4854 is fixed
16+
// Expand as needed, numbers should be ascending according to the stage
17+
// through the inclusion pipeline, or according to the descriptions
18+
}
19+
20+
pub struct S {
21+
// Expand as needed, numbers should be ascending according to the stage
22+
// through the inclusion pipeline, or according to the descriptions
23+
some_field: usize,
24+
// Expand as needed, numbers should be ascending according to the stage
25+
// through the inclusion pipeline, or according to the descriptions
26+
last_field: usize,
27+
// Expand as needed, numbers should be ascending according to the stage
28+
// through the inclusion pipeline, or according to the descriptions
29+
}
30+
31+
pub struct S2 {
32+
// This can be changed once https://github.com/rust-lang/rustfmt/issues/4854 is fixed
33+
// Expand as needed, numbers should be ascending according to the stage
34+
// through the inclusion pipeline, or according to the descriptions
35+
}
36+
37+
fn foo(
38+
// Expand as needed, numbers should be ascending according to the stage
39+
// through the inclusion pipeline, or according to the descriptions
40+
a: usize,
41+
// Expand as needed, numbers should be ascending according to the stage
42+
// through the inclusion pipeline, or according to the descriptions
43+
b: usize,
44+
// Expand as needed, numbers should be ascending according to the stage
45+
// through the inclusion pipeline, or according to the descriptions
46+
) -> usize {
47+
5
48+
}
49+
50+
fn foo2(// Expand as needed, numbers should be ascending according to the stage
51+
// through the inclusion pipeline, or according to the descriptions
52+
) -> usize {
53+
5
54+
}
55+
56+
fn main() {
57+
let v = vec![
58+
// Expand as needed, numbers should be ascending according to the stage
59+
// through the inclusion pipeline, or according to the descriptions
60+
1,
61+
// Expand as needed, numbers should be ascending according to the stage
62+
// through the inclusion pipeline, or according to the descriptions
63+
2,
64+
// Expand as needed, numbers should be ascending according to the stage
65+
// through the inclusion pipeline, or according to the descriptions
66+
];
67+
68+
let v2: Vec<i32> = vec![
69+
// Expand as needed, numbers should be ascending according to the stage
70+
// through the inclusion pipeline, or according to the descriptions
71+
];
72+
}

0 commit comments

Comments
 (0)