Skip to content

Commit 58149d7

Browse files
committed
Fix indentation issue of secont and following lines in multiline comment
1 parent fec65a6 commit 58149d7

File tree

3 files changed

+104
-7
lines changed

3 files changed

+104
-7
lines changed

src/formatting/comment.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -948,25 +948,30 @@ fn light_rewrite_comment(
948948
config: &Config,
949949
is_doc_comment: bool,
950950
) -> String {
951-
let lines: Vec<&str> = orig
951+
let lines: Vec<String> = orig
952952
.lines()
953953
.map(|l| {
954954
// This is basically just l.trim(), but in the case that a line starts
955955
// with `*` we want to leave one space before it, so it aligns with the
956956
// `*` in `/*`.
957957
let first_non_whitespace = l.find(|c| !char::is_whitespace(c));
958-
let left_trimmed = if let Some(fnw) = first_non_whitespace {
959-
if l.as_bytes()[fnw] == b'*' && fnw > 0 {
960-
&l[fnw - 1..]
958+
let (blank, left_trimmed) = if let Some(fnw) = first_non_whitespace {
959+
if l.as_bytes()[fnw] == b'*' {
960+
// Ensure '*' is preceeded by blank and not by a tab.
961+
(" ", &l[fnw..])
961962
} else {
962-
&l[fnw..]
963+
("", &l[fnw..])
963964
}
964965
} else {
965-
""
966+
("", "")
966967
};
967968
// Preserve markdown's double-space line break syntax in doc comment.
968-
trim_end_unless_two_whitespaces(left_trimmed, is_doc_comment)
969+
(
970+
blank,
971+
trim_end_unless_two_whitespaces(left_trimmed, is_doc_comment),
972+
)
969973
})
974+
.map(|(b, l)| format!("{}{}", b, l))
970975
.collect();
971976
lines.join(&format!("\n{}", offset.to_string(config)))
972977
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Ensure multiline comments are indented properly,
2+
// incluyding when second line is prefixed by tab or at the beginning of the line
3+
4+
/* First comment line
5+
* second comment line - no prefix
6+
* last comment line */
7+
8+
/* First comment line
9+
* second comment line - blank prefix
10+
* last comment line */
11+
12+
/* First comment line
13+
* second comment line - tab prefix
14+
* last comment line */
15+
16+
/* First comment line
17+
* second comment line - blank prefix
18+
* last comment line - no prefix */
19+
20+
/* First comment line
21+
* second comment line - blank prefix
22+
* last comment line */
23+
24+
type T1 = TT<
25+
u32, /* First comment line
26+
* second comment line - no prefix
27+
* last comment line */
28+
>;
29+
30+
type T2 = TT<
31+
u32, /* First comment line
32+
* second comment line - blank prefix
33+
* last comment line */
34+
>;
35+
36+
type T2 = TT<
37+
u32, /* First comment line
38+
* second comment line - tab prefix
39+
* last comment line */
40+
>;
41+
42+
type T3 = TT<
43+
u32, /* First comment line
44+
* second comment line - tab prefix
45+
* last comment line */
46+
>;
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Ensure multiline comments are indented properly,
2+
// incluyding when second line is prefixed by tab or at the beginning of the line
3+
4+
/* First comment line
5+
* second comment line - no prefix
6+
* last comment line */
7+
8+
/* First comment line
9+
* second comment line - blank prefix
10+
* last comment line */
11+
12+
/* First comment line
13+
* second comment line - tab prefix
14+
* last comment line */
15+
16+
/* First comment line
17+
* second comment line - blank prefix
18+
* last comment line - no prefix */
19+
20+
/* First comment line
21+
* second comment line - blank prefix
22+
* last comment line */
23+
24+
type T1 = TT<
25+
u32, /* First comment line
26+
* second comment line - no prefix
27+
* last comment line */
28+
>;
29+
30+
type T2 = TT<
31+
u32, /* First comment line
32+
* second comment line - blank prefix
33+
* last comment line */
34+
>;
35+
36+
type T2 = TT<
37+
u32, /* First comment line
38+
* second comment line - tab prefix
39+
* last comment line */
40+
>;
41+
42+
type T3 = TT<
43+
u32, /* First comment line
44+
* second comment line - tab prefix
45+
* last comment line */
46+
>;

0 commit comments

Comments
 (0)