Skip to content

Indentation of secont and following lines in multiline comment #4658

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 42 additions & 40 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 12 additions & 7 deletions src/formatting/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -948,24 +948,29 @@ fn light_rewrite_comment(
config: &Config,
is_doc_comment: bool,
) -> String {
let lines: Vec<&str> = orig
let lines: Vec<String> = orig
.lines()
.map(|l| {
// This is basically just l.trim(), but in the case that a line starts
// with `*` we want to leave one space before it, so it aligns with the
// `*` in `/*`.
let first_non_whitespace = l.find(|c| !char::is_whitespace(c));
let left_trimmed = if let Some(fnw) = first_non_whitespace {
if l.as_bytes()[fnw] == b'*' && fnw > 0 {
&l[fnw - 1..]
let (blank, left_trimmed) = if let Some(fnw) = first_non_whitespace {
if l.as_bytes()[fnw] == b'*' {
// Ensure '*' is preceeded by blank and not by a tab.
(" ", &l[fnw..])
} else {
&l[fnw..]
("", &l[fnw..])
}
} else {
""
("", "")
};
// Preserve markdown's double-space line break syntax in doc comment.
trim_end_unless_two_whitespaces(left_trimmed, is_doc_comment)
format!(
"{}{}",
blank,
trim_end_unless_two_whitespaces(left_trimmed, is_doc_comment),
)
})
.collect();
lines.join(&format!("\n{}", offset.to_string(config)))
Expand Down
47 changes: 47 additions & 0 deletions tests/source/comment-multiline-indentation-hard-tabs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// rustfmt-hard_tabs: true
// Ensure multiline comments are indented properly,
// including when second line is prefixed by tab or at the beginning of the line

/* First comment line
* second comment line - no prefix
* last comment line */

/* First comment line
* second comment line - blank prefix
* last comment line */

/* First comment line
* second comment line - tab prefix
* last comment line */

/* First comment line
* second comment line - blank prefix
* last comment line - no prefix */

/* First comment line
* second comment line - blank prefix
* last comment line */

type T1 = TT<
u32, /* First comment line
* second comment line - no prefix
* last comment line */
>;

type T2 = TT<
u32, /* First comment line
* second comment line - blank prefix
* last comment line */
>;

type T2 = TT<
u32, /* First comment line
* second comment line - tab prefix
* last comment line */
>;

type T3 = TT<
u32, /* First comment line
* second comment line - tab prefix
* last comment line */
>;
47 changes: 47 additions & 0 deletions tests/source/comment-multiline-indentation.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// rustfmt-hard_tabs: false
// Ensure multiline comments are indented properly,
// including when second line is prefixed by tab or at the beginning of the line

/* First comment line
* second comment line - no prefix
* last comment line */

/* First comment line
* second comment line - blank prefix
* last comment line */

/* First comment line
* second comment line - tab prefix
* last comment line */

/* First comment line
* second comment line - blank prefix
* last comment line - no prefix */

/* First comment line
* second comment line - blank prefix
* last comment line */

type T1 = TT<
u32, /* First comment line
* second comment line - no prefix
* last comment line */
>;

type T2 = TT<
u32, /* First comment line
* second comment line - blank prefix
* last comment line */
>;

type T2 = TT<
u32, /* First comment line
* second comment line - tab prefix
* last comment line */
>;

type T3 = TT<
u32, /* First comment line
* second comment line - tab prefix
* last comment line */
>;
Loading