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

Wrap multi lined trailing block comments (#6339) #6358

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
6 changes: 6 additions & 0 deletions src/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,12 @@ pub(crate) fn is_last_comment_block(s: &str) -> bool {
s.trim_end().ends_with("*/")
}

/// Return true if a block-comment contains endline character
pub(crate) fn is_multi_lined_block_comment(s: &str) -> bool {
let style = comment_style(s, false);
style.is_block_comment() && count_newlines(s) >= 1
}

/// Combine `prev_str` and `next_str` into a single `String`. `span` may contain
/// comments between two strings. If there are such comments, then that will be
/// recovered. If `allow_extend` is true and there is no comment between the two
Expand Down
15 changes: 12 additions & 3 deletions src/missed_spans.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use rustc_span::{BytePos, Pos, Span};
use tracing::debug;

use crate::comment::{CodeCharKind, CommentCodeSlices, is_last_comment_block, rewrite_comment};
use crate::comment::{
CodeCharKind, CommentCodeSlices, is_last_comment_block, is_multi_lined_block_comment,
rewrite_comment,
};
use crate::config::FileName;
use crate::config::StyleEdition;
use crate::config::file_lines::FileLines;
Expand Down Expand Up @@ -239,6 +242,7 @@ impl<'a> FmtVisitor<'a> {

let fix_indent = last_char.map_or(true, |rev_c| ['{', '\n'].contains(&rev_c));
let mut on_same_line = false;
let multi_lined_block_comment = is_multi_lined_block_comment(subslice);

let comment_indent = if fix_indent {
if let Some('{') = last_char {
Expand All @@ -255,7 +259,12 @@ impl<'a> FmtVisitor<'a> {
// the same level and avoid mixing it with possible other comment.
on_same_line = true;
self.push_str(" ");
self.block_indent

if multi_lined_block_comment {
Indent::from_width(self.config, last_line_width(&self.buffer))
} else {
self.block_indent
}
} else {
self.push_str(" ");
Indent::from_width(self.config, last_line_width(&self.buffer))
Expand All @@ -267,7 +276,7 @@ impl<'a> FmtVisitor<'a> {
);
let comment_shape = Shape::legacy(comment_width, comment_indent);

if on_same_line {
if on_same_line && !multi_lined_block_comment {
match subslice.find('\n') {
None => {
self.push_str(subslice);
Expand Down
37 changes: 37 additions & 0 deletions tests/source/issue-6339.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// rustfmt-style_edition: 2024
// rustfmt-wrap_comments: true

use MultiLinedBlockComment; /* a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a
* b b b b b b */

use MultiLinedNestedBlockComment1; /* a a a a a a a /* a a a a a a a a a a a a a a a a a a a a a a a a a a a a */ a a a a
* b b b b b b */

use MultiLinedNestedBlockComment2; /* a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a // a
* b b b b b b */

use SingleLinedBlockComment1; /* a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a */

use SingleLinedBlockComment2; /* a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a */
/* b b b b b b b b b b b b b b b b b b b b b b b b */

use MultiLinedLineComment; // a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a
// b b b b b b

use MultiLinedBlockAndLineComment; /* a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a
* b b b b b b */
// c c c c c
// d d d d d

use ShortMultiLineBlockComment; /* a a a a a a a a a a
* a a a a a a a a a a
* a a a a a a a a a a
* a a a a a a a a a a
* a a a a a a a a a a
* a a a a a a a a a a
* a a a a a a a a a a
* a a a a a a a a a a
* a a a a a a a a a a
* a a a a a a a a a a
* a a a a a a a a a a
*/
37 changes: 37 additions & 0 deletions tests/target/issue-6339.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// rustfmt-style_edition: 2024
// rustfmt-wrap_comments: true

use MultiLinedBlockComment; /* a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a
* a a b b b b b b */

use MultiLinedNestedBlockComment1; /* a a a a a a a /* a a a a a a a a a a a a a a a a a a a a a a a a a a a a
* */ a a a a b b b b b b */

use MultiLinedNestedBlockComment2; /* a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a
* a // a b b b b b b */

use SingleLinedBlockComment1; /* a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a */

use SingleLinedBlockComment2; /* a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a */
/* b b b b b b b b b b b b b b b b b b b b b b b b */

use MultiLinedLineComment; // a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a
// b b b b b b

use MultiLinedBlockAndLineComment; /* a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a
* a a b b b b b b */
// c c c c c
// d d d d d

use ShortMultiLineBlockComment; /* a a a a a a a a a a
* a a a a a a a a a a
* a a a a a a a a a a
* a a a a a a a a a a
* a a a a a a a a a a
* a a a a a a a a a a
* a a a a a a a a a a
* a a a a a a a a a a
* a a a a a a a a a a
* a a a a a a a a a a
* a a a a a a a a a a
*/
Loading