Skip to content

Commit e3ab83e

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

File tree

4 files changed

+309
-4
lines changed

4 files changed

+309
-4
lines changed

src/lists.rs

Lines changed: 9 additions & 4 deletions
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(),
Lines changed: 109 additions & 0 deletions
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+
}
Lines changed: 72 additions & 0 deletions
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+
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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
23+
// pipeline, or according to the descriptions
24+
Variant1,
25+
// Expand as needed, numbers should be ascending according to the stage through the inclusion
26+
// pipeline, or according to the descriptions
27+
Variant2,
28+
// Expand as needed, numbers should be ascending according to the stage through the inclusion
29+
// pipeline, or according to the descriptions
30+
}
31+
32+
pub struct S {
33+
// Expand as needed, numbers should be ascending according to the stage
34+
// through the inclusion pipeline, or according to the descriptions
35+
some_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+
last_field: usize,
39+
// Expand as needed, numbers should be ascending according to the stage
40+
// through the inclusion pipeline, or according to the descriptions
41+
}
42+
43+
pub struct S2 {
44+
// This can be changed once https://github.com/rust-lang/rustfmt/issues/4854 is fixed
45+
// Expand as needed, numbers should be ascending according to the stage
46+
// through the inclusion pipeline, or according to the descriptions
47+
}
48+
49+
pub struct S3 {
50+
// Expand as needed, numbers should be ascending according to the stage through the inclusion
51+
// pipeline, or according to the descriptions
52+
some_field: usize,
53+
// Expand as needed, numbers should be ascending according to the stage through the inclusion
54+
// pipeline, or according to the descriptions
55+
last_field: usize,
56+
// Expand as needed, numbers should be ascending according to the stage through the inclusion
57+
// pipeline, or according to the descriptions
58+
}
59+
60+
fn foo(
61+
// Expand as needed, numbers should be ascending according to the stage
62+
// through the inclusion pipeline, or according to the descriptions
63+
a: usize,
64+
// Expand as needed, numbers should be ascending according to the stage
65+
// through the inclusion pipeline, or according to the descriptions
66+
b: usize,
67+
// 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 foo2(// Expand as needed, numbers should be ascending according to the stage
74+
// through the inclusion pipeline, or according to the descriptions
75+
) -> usize {
76+
5
77+
}
78+
79+
fn foo3(
80+
// Expand as needed, numbers should be ascending according to the stage through the inclusion
81+
// pipeline, or according to the descriptions
82+
a: usize,
83+
// Expand as needed, numbers should be ascending according to the stage through the inclusion
84+
// pipeline, or according to the descriptions
85+
b: usize,
86+
// Expand as needed, numbers should be ascending according to the stage through the inclusion
87+
// pipeline, or according to the descriptions
88+
) -> usize {
89+
5
90+
}
91+
92+
fn main() {
93+
let v = vec![
94+
// Expand as needed, numbers should be ascending according to the stage
95+
// through the inclusion pipeline, or according to the descriptions
96+
1,
97+
// Expand as needed, numbers should be ascending according to the stage
98+
// through the inclusion pipeline, or according to the descriptions
99+
2,
100+
// Expand as needed, numbers should be ascending according to the stage
101+
// through the inclusion pipeline, or according to the descriptions
102+
];
103+
104+
let v2: Vec<i32> = vec![
105+
// Expand as needed, numbers should be ascending according to the stage
106+
// through the inclusion pipeline, or according to the descriptions
107+
];
108+
109+
let v3 = vec![
110+
// Expand as needed, numbers should be ascending according to the stage through the
111+
// inclusion pipeline, or according to the descriptions
112+
1,
113+
// Expand as needed, numbers should be ascending according to the stage through the
114+
// inclusion pipeline, or according to the descriptions
115+
2,
116+
// Expand as needed, numbers should be ascending according to the stage through the
117+
// inclusion pipeline, or according to the descriptions
118+
];
119+
}

0 commit comments

Comments
 (0)