diff --git a/src/comment.rs b/src/comment.rs index b7d7a396a67..79406fd0440 100644 --- a/src/comment.rs +++ b/src/comment.rs @@ -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 diff --git a/src/missed_spans.rs b/src/missed_spans.rs index 384de1ce9ae..052da5a6c55 100644 --- a/src/missed_spans.rs +++ b/src/missed_spans.rs @@ -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; @@ -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 { @@ -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)) @@ -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); diff --git a/tests/source/issue-6339.rs b/tests/source/issue-6339.rs new file mode 100644 index 00000000000..a72e6e8f335 --- /dev/null +++ b/tests/source/issue-6339.rs @@ -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 + */ diff --git a/tests/target/issue-6339.rs b/tests/target/issue-6339.rs new file mode 100644 index 00000000000..a1405038d9c --- /dev/null +++ b/tests/target/issue-6339.rs @@ -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 + */