Skip to content
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

Backport 4304 #5288

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
32 changes: 22 additions & 10 deletions src/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -607,16 +607,22 @@ impl<'a> CommentRewrite<'a> {
cr
}

fn join_block(s: &str, sep: &str) -> String {
fn join_block(s: &str, sep: &str, add_sep_prefix: bool) -> String {
let mut result = String::with_capacity(s.len() + 128);
let mut iter = s.lines().peekable();
while let Some(line) = iter.next() {
result.push_str(line);
result.push_str(match iter.peek() {
Some(next_line) if next_line.is_empty() => sep.trim_end(),
let get_sep = |line: Option<&&str>| -> &str {
match line {
Some(&"") => sep.trim_end(),
Some(..) => sep,
None => "",
});
}
};
if add_sep_prefix {
result.push_str(get_sep(iter.peek()));
}
while let Some(line) = iter.next() {
result.push_str(line);
result.push_str(get_sep(iter.peek()));
}
result
}
Expand All @@ -634,10 +640,10 @@ impl<'a> CommentRewrite<'a> {
if !self.code_block_buffer.is_empty() {
// There is a code block that is not properly enclosed by backticks.
// We will leave them untouched.
self.result.push_str(&self.comment_line_separator);
self.result.push_str(&Self::join_block(
&trim_custom_comment_prefix(&self.code_block_buffer),
&self.comment_line_separator,
true, /* add_sep_prefix */
));
}

Expand All @@ -660,10 +666,12 @@ impl<'a> CommentRewrite<'a> {
Some(s) => self.result.push_str(&Self::join_block(
&s,
&format!("{}{}", self.comment_line_separator, ib.line_start),
false, /* add_sep_prefix */
)),
None => self.result.push_str(&Self::join_block(
&ib.original_block_as_string(),
&self.comment_line_separator,
false, /* add_sep_prefix */
)),
};
}
Expand Down Expand Up @@ -715,10 +723,12 @@ impl<'a> CommentRewrite<'a> {
Some(s) => self.result.push_str(&Self::join_block(
&s,
&format!("{}{}", self.comment_line_separator, ib.line_start),
false, /* add_sep_prefix */
)),
None => self.result.push_str(&Self::join_block(
&ib.original_block_as_string(),
&self.comment_line_separator,
false, /* add_sep_prefix */
)),
};
} else if self.code_block_attr.is_some() {
Expand All @@ -741,9 +751,11 @@ impl<'a> CommentRewrite<'a> {
_ => trim_custom_comment_prefix(&self.code_block_buffer),
};
if !code_block.is_empty() {
self.result.push_str(&self.comment_line_separator);
self.result
.push_str(&Self::join_block(&code_block, &self.comment_line_separator));
self.result.push_str(&Self::join_block(
&code_block,
&self.comment_line_separator,
true, /* add_sep_prefix */
));
}
self.code_block_buffer.clear();
self.result.push_str(&self.comment_line_separator);
Expand Down
6 changes: 6 additions & 0 deletions tests/source/issue-4251.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// rustfmt-wrap_comments: true

//! ```
//!
//! use something;
//! ```
6 changes: 6 additions & 0 deletions tests/target/issue-4251.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// rustfmt-wrap_comments: true

//! ```
//!
//! use something;
//! ```