Skip to content

Commit 1960ec1

Browse files
committed
Baport PR rust-lang#4658 and Fix issue rust-lang#4551 - second comment line indentation
1 parent a451a39 commit 1960ec1

15 files changed

+728
-9
lines changed

src/comment.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,24 +1001,29 @@ fn light_rewrite_comment(
10011001
config: &Config,
10021002
is_doc_comment: bool,
10031003
) -> String {
1004-
let lines: Vec<&str> = orig
1004+
let lines: Vec<String> = orig
10051005
.lines()
10061006
.map(|l| {
10071007
// This is basically just l.trim(), but in the case that a line starts
10081008
// with `*` we want to leave one space before it, so it aligns with the
10091009
// `*` in `/*`.
10101010
let first_non_whitespace = l.find(|c| !char::is_whitespace(c));
1011-
let left_trimmed = if let Some(fnw) = first_non_whitespace {
1012-
if l.as_bytes()[fnw] == b'*' && fnw > 0 {
1013-
&l[fnw - 1..]
1011+
let (blank, left_trimmed) = if let Some(fnw) = first_non_whitespace {
1012+
if l.as_bytes()[fnw] == b'*' {
1013+
// Ensure '*' is preceeded by blank and not by a tab.
1014+
(" ", &l[fnw..])
10141015
} else {
1015-
&l[fnw..]
1016+
("", &l[fnw..])
10161017
}
10171018
} else {
1018-
""
1019+
("", "")
10191020
};
10201021
// Preserve markdown's double-space line break syntax in doc comment.
1021-
trim_end_unless_two_whitespaces(left_trimmed, is_doc_comment)
1022+
format!(
1023+
"{}{}",
1024+
blank,
1025+
trim_end_unless_two_whitespaces(left_trimmed, is_doc_comment),
1026+
)
10221027
})
10231028
.collect();
10241029
lines.join(&format!("\n{}", offset.to_string(config)))

src/missed_spans.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,10 @@ impl<'a> FmtVisitor<'a> {
246246
let indent_str = self.block_indent.to_string(self.config);
247247
self.push_str(&indent_str);
248248
self.block_indent
249-
} else if self.config.version() == Version::Two && !snippet.starts_with('\n') {
249+
} else if self.config.version() == Version::Two
250+
&& !snippet.starts_with('\n')
251+
&& !subslice.starts_with("/*")
252+
{
250253
// The comment appears on the same line as the previous formatted code.
251254
// Assuming that comment is logically associated with that code, we want to keep it on
252255
// the same level and avoid mixing it with possible other comment.

src/visitor.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,10 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
296296

297297
let mut comment_shape =
298298
Shape::indented(self.block_indent, config).comment(config);
299-
if self.config.version() == Version::Two && comment_on_same_line {
299+
if self.config.version() == Version::Two
300+
&& comment_on_same_line
301+
&& !sub_slice.starts_with("/*")
302+
{
300303
self.push_str(" ");
301304
// put the first line of the comment on the same line as the
302305
// block's last line

tests/source/issue-4551/one.rs

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// rustfmt-version: One
2+
3+
// From #4551
4+
5+
fn main() {
6+
/* Comment1 line 1
7+
* Comment1 line 2 */
8+
let ys = 1;/* Comment2 line 1
9+
* Comment2 line 2 with preceeding tabs */
10+
}
11+
12+
fn main() {
13+
/* Comment1 line 1
14+
* Comment1 line 2 */
15+
// Following1 comment
16+
let y1 = 6;/* Comment2 line 1
17+
* Comment2 line 2 - prefixed by spaces and 1 tab */
18+
// Following2 comment
19+
let y2 = 6;/* Comment3 line 1
20+
* Comment3 line 2 - prefixed by spaces and 2 tabs */
21+
/* Following3 comment */
22+
let y3 = 8;/* Comment4 line 1
23+
* Comment4 line 2 - prefixed by spaces */
24+
/* Following4 comment
25+
* cont of Following4 comment */
26+
}
27+
28+
// Other tests
29+
30+
fn main() {
31+
let x = 111; /* First Comment with spaces prefix for each line - line 1
32+
* First Comment line 2
33+
* First Comment line 3
34+
*/
35+
// Following first comment
36+
}
37+
38+
fn main() {
39+
let x = 222; /* Second Comment with tab prefix for each line - line 1
40+
* Second Comment line 2
41+
* Second Comment line 3
42+
*/
43+
/* Following second comment line 1
44+
* Following second comment line 2
45+
* Following second comment line 3
46+
*/
47+
}
48+
49+
fn main() {
50+
let x = 333; /* Third Comment with spaces prefix for each line - line 1
51+
* Third Comment line 2
52+
* Third Comment line 3
53+
*/
54+
/* Following third comment line 1
55+
* Following third comment line 2
56+
* Following third comment line 3
57+
*/
58+
}
59+
60+
fn main() {
61+
let x = 222; /* Second Comment line 1
62+
* Second Comment line 2
63+
* Second Comment line 3
64+
*/
65+
let y = 333; // Following second comment line 1
66+
// Following second comment line 2
67+
// Following second comment line 3
68+
}
69+
70+
fn main() {
71+
let y3 = 8;/* Comment4 line 1
72+
* Comment4 line 2 */
73+
}
74+
75+
fn main() {
76+
let y3 = 8;/* Comment4 line 1
77+
* Comment4 line 2 */
78+
y4 = 9;
79+
}

tests/source/issue-4551/two.rs

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// rustfmt-version: Two
2+
3+
// From #4551
4+
5+
fn main() {
6+
/* Comment1 line 1
7+
* Comment1 line 2 */
8+
let ys = 1;/* Comment2 line 1
9+
* Comment2 line 2 with preceeding tabs */
10+
}
11+
12+
fn main() {
13+
/* Comment1 line 1
14+
* Comment1 line 2 */
15+
// Following1 comment
16+
let y1 = 6;/* Comment2 line 1
17+
* Comment2 line 2 - prefixed by spaces and 1 tab */
18+
// Following2 comment
19+
let y2 = 6;/* Comment3 line 1
20+
* Comment3 line 2 - prefixed by spaces and 2 tabs */
21+
/* Following3 comment */
22+
let y3 = 8;/* Comment4 line 1
23+
* Comment4 line 2 - prefixed by spaces */
24+
/* Following4 comment
25+
* cont of Following4 comment */
26+
}
27+
28+
// Other tests
29+
30+
fn main() {
31+
let x = 111; /* First Comment with spaces prefix for each line - line 1
32+
* First Comment line 2
33+
* First Comment line 3
34+
*/
35+
// Following first comment
36+
}
37+
38+
fn main() {
39+
let x = 222; /* Second Comment with tab prefix for each line - line 1
40+
* Second Comment line 2
41+
* Second Comment line 3
42+
*/
43+
/* Following second comment line 1
44+
* Following second comment line 2
45+
* Following second comment line 3
46+
*/
47+
}
48+
49+
fn main() {
50+
let x = 333; /* Third Comment with spaces prefix for each line - line 1
51+
* Third Comment line 2
52+
* Third Comment line 3
53+
*/
54+
/* Following third comment line 1
55+
* Following third comment line 2
56+
* Following third comment line 3
57+
*/
58+
}
59+
60+
fn main() {
61+
let x = 222; /* Second Comment line 1
62+
* Second Comment line 2
63+
* Second Comment line 3
64+
*/
65+
let y = 333; // Following second comment line 1
66+
// Following second comment line 2
67+
// Following second comment line 3
68+
}
69+
70+
fn main() {
71+
let y3 = 8;/* Comment4 line 1
72+
* Comment4 line 2 */
73+
}
74+
75+
fn main() {
76+
let y3 = 8;/* Comment4 line 1
77+
* Comment4 line 2 */
78+
y4 = 9;
79+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// rustfmt-version: One
2+
// rustfmt-hard_tabs: true
3+
4+
// Ensure multiline comments are indented properly,
5+
// including when second line is prefixed by tab or at the beginning of the line
6+
7+
/* First comment line
8+
* second comment line - no prefix
9+
* last comment line */
10+
11+
/* First comment line
12+
* second comment line - blank prefix
13+
* last comment line */
14+
15+
/* First comment line
16+
* second comment line - tab prefix
17+
* last comment line */
18+
19+
/* First comment line
20+
* second comment line - blank prefix
21+
* last comment line - no prefix */
22+
23+
/* First comment line
24+
* second comment line - blank prefix
25+
* last comment line */
26+
27+
type T1 = TT<
28+
u32, /* First comment line
29+
* second comment line - no prefix
30+
* last comment line */
31+
>;
32+
33+
type T2 = TT<
34+
u32, /* First comment line
35+
* second comment line - blank prefix
36+
* last comment line */
37+
>;
38+
39+
type T2 = TT<
40+
u32, /* First comment line
41+
* second comment line - tab prefix
42+
* last comment line */
43+
>;
44+
45+
type T3 = TT<
46+
u32, /* First comment line
47+
* second comment line - tab prefix
48+
* last comment line */
49+
>;
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// rustfmt-version: One
2+
// rustfmt-hard_tabs: false
3+
4+
// Ensure multiline comments are indented properly,
5+
// including when second line is prefixed by tab or at the beginning of the line
6+
7+
/* First comment line
8+
* second comment line - no prefix
9+
* last comment line */
10+
11+
/* First comment line
12+
* second comment line - blank prefix
13+
* last comment line */
14+
15+
/* First comment line
16+
* second comment line - tab prefix
17+
* last comment line */
18+
19+
/* First comment line
20+
* second comment line - blank prefix
21+
* last comment line - no prefix */
22+
23+
/* First comment line
24+
* second comment line - blank prefix
25+
* last comment line */
26+
27+
type T1 = TT<
28+
u32, /* First comment line
29+
* second comment line - no prefix
30+
* last comment line */
31+
>;
32+
33+
type T2 = TT<
34+
u32, /* First comment line
35+
* second comment line - blank prefix
36+
* last comment line */
37+
>;
38+
39+
type T2 = TT<
40+
u32, /* First comment line
41+
* second comment line - tab prefix
42+
* last comment line */
43+
>;
44+
45+
type T3 = TT<
46+
u32, /* First comment line
47+
* second comment line - tab prefix
48+
* last comment line */
49+
>;
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// rustfmt-version: Two
2+
// rustfmt-hard_tabs: true
3+
4+
// Ensure multiline comments are indented properly,
5+
// including when second line is prefixed by tab or at the beginning of the line
6+
7+
/* First comment line
8+
* second comment line - no prefix
9+
* last comment line */
10+
11+
/* First comment line
12+
* second comment line - blank prefix
13+
* last comment line */
14+
15+
/* First comment line
16+
* second comment line - tab prefix
17+
* last comment line */
18+
19+
/* First comment line
20+
* second comment line - blank prefix
21+
* last comment line - no prefix */
22+
23+
/* First comment line
24+
* second comment line - blank prefix
25+
* last comment line */
26+
27+
type T1 = TT<
28+
u32, /* First comment line
29+
* second comment line - no prefix
30+
* last comment line */
31+
>;
32+
33+
type T2 = TT<
34+
u32, /* First comment line
35+
* second comment line - blank prefix
36+
* last comment line */
37+
>;
38+
39+
type T2 = TT<
40+
u32, /* First comment line
41+
* second comment line - tab prefix
42+
* last comment line */
43+
>;
44+
45+
type T3 = TT<
46+
u32, /* First comment line
47+
* second comment line - tab prefix
48+
* last comment line */
49+
>;

0 commit comments

Comments
 (0)