Skip to content

Commit a36bcfa

Browse files
davidBar-Oncalebcartwright
authored andcommitted
Fix indentation issue of secont and following lines in multiline comment (enhanced1)
1 parent 98f2347 commit a36bcfa

6 files changed

+242
-47
lines changed

Diff for: Cargo.lock

+42-40
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: src/formatting/comment.rs

+12-7
Original file line numberDiff line numberDiff line change
@@ -948,24 +948,29 @@ 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+
format!(
970+
"{}{}",
971+
blank,
972+
trim_end_unless_two_whitespaces(left_trimmed, is_doc_comment),
973+
)
969974
})
970975
.collect();
971976
lines.join(&format!("\n{}", offset.to_string(config)))
+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// rustfmt-hard_tabs: true
2+
// Ensure multiline comments are indented properly,
3+
// including when second line is prefixed by tab or at the beginning of the line
4+
5+
/* First comment line
6+
* second comment line - no prefix
7+
* last comment line */
8+
9+
/* First comment line
10+
* second comment line - blank prefix
11+
* last comment line */
12+
13+
/* First comment line
14+
* second comment line - tab prefix
15+
* last comment line */
16+
17+
/* First comment line
18+
* second comment line - blank prefix
19+
* last comment line - no prefix */
20+
21+
/* First comment line
22+
* second comment line - blank prefix
23+
* last comment line */
24+
25+
type T1 = TT<
26+
u32, /* First comment line
27+
* second comment line - no prefix
28+
* last comment line */
29+
>;
30+
31+
type T2 = TT<
32+
u32, /* First comment line
33+
* second comment line - blank prefix
34+
* last comment line */
35+
>;
36+
37+
type T2 = TT<
38+
u32, /* First comment line
39+
* second comment line - tab prefix
40+
* last comment line */
41+
>;
42+
43+
type T3 = TT<
44+
u32, /* First comment line
45+
* second comment line - tab prefix
46+
* last comment line */
47+
>;

Diff for: tests/source/comment-multiline-indentation.rs

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// rustfmt-hard_tabs: false
2+
// Ensure multiline comments are indented properly,
3+
// including when second line is prefixed by tab or at the beginning of the line
4+
5+
/* First comment line
6+
* second comment line - no prefix
7+
* last comment line */
8+
9+
/* First comment line
10+
* second comment line - blank prefix
11+
* last comment line */
12+
13+
/* First comment line
14+
* second comment line - tab prefix
15+
* last comment line */
16+
17+
/* First comment line
18+
* second comment line - blank prefix
19+
* last comment line - no prefix */
20+
21+
/* First comment line
22+
* second comment line - blank prefix
23+
* last comment line */
24+
25+
type T1 = TT<
26+
u32, /* First comment line
27+
* second comment line - no prefix
28+
* last comment line */
29+
>;
30+
31+
type T2 = TT<
32+
u32, /* First comment line
33+
* second comment line - blank prefix
34+
* last comment line */
35+
>;
36+
37+
type T2 = TT<
38+
u32, /* First comment line
39+
* second comment line - tab prefix
40+
* last comment line */
41+
>;
42+
43+
type T3 = TT<
44+
u32, /* First comment line
45+
* second comment line - tab prefix
46+
* last comment line */
47+
>;

0 commit comments

Comments
 (0)