Skip to content

Commit

Permalink
Fix indentation issue of secont and following lines in multiline comm…
Browse files Browse the repository at this point in the history
…ent (enhanced1)
  • Loading branch information
davidBar-On authored and calebcartwright committed Feb 13, 2021
1 parent 98f2347 commit a36bcfa
Show file tree
Hide file tree
Showing 6 changed files with 242 additions and 47 deletions.
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

0 comments on commit a36bcfa

Please sign in to comment.