Skip to content

Commit

Permalink
fix: handle block comments with trailing line comments (rust-lang#3842)
Browse files Browse the repository at this point in the history
  • Loading branch information
calebcartwright authored and topecongiro committed Oct 8, 2019
1 parent 8073244 commit 6dcbc5d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/lists.rs
Original file line number Diff line number Diff line change
Expand Up @@ -574,9 +574,17 @@ where

pub(crate) fn extract_pre_comment(pre_snippet: &str) -> (Option<String>, ListItemCommentStyle) {
let trimmed_pre_snippet = pre_snippet.trim();
let has_block_comment = trimmed_pre_snippet.ends_with("*/");
let has_single_line_comment = trimmed_pre_snippet.starts_with("//");
if has_block_comment {
// Both start and end are checked to support keeping a block comment inline with
// the item, even if there are preceeding line comments, while still supporting
// a snippet that starts with a block comment but also contains one or more
// trailing single line comments.
// https://github.com/rust-lang/rustfmt/issues/3025
// https://github.com/rust-lang/rustfmt/pull/3048
// https://github.com/rust-lang/rustfmt/issues/3839
let starts_with_block_comment = trimmed_pre_snippet.starts_with("/*");
let ends_with_block_comment = trimmed_pre_snippet.ends_with("*/");
let starts_with_single_line_comment = trimmed_pre_snippet.starts_with("//");
if ends_with_block_comment {
let comment_end = pre_snippet.rfind(|c| c == '/').unwrap();
if pre_snippet[comment_end..].contains('\n') {
(
Expand All @@ -589,7 +597,7 @@ pub(crate) fn extract_pre_comment(pre_snippet: &str) -> (Option<String>, ListIte
ListItemCommentStyle::SameLine,
)
}
} else if has_single_line_comment {
} else if starts_with_single_line_comment || starts_with_block_comment {
(
Some(trimmed_pre_snippet.to_owned()),
ListItemCommentStyle::DifferentLine,
Expand Down
8 changes: 8 additions & 0 deletions tests/source/issue_3839.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
struct Foo {
a: i32,
/*
asd
*/
// foo
b: i32,
}
8 changes: 8 additions & 0 deletions tests/target/issue_3839.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
struct Foo {
a: i32,
/*
asd
*/
// foo
b: i32,
}

0 comments on commit 6dcbc5d

Please sign in to comment.