Skip to content

Commit d9b37a0

Browse files
committed
Optionally remove leading newlines in recover_missing_comment_in_span
A new parameter was added to recover_missing_comment_in_span to control whether leading newline characters should always be removed from its output.
1 parent 7e1a92f commit d9b37a0

File tree

3 files changed

+10
-1
lines changed

3 files changed

+10
-1
lines changed

src/attr.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,7 @@ impl Rewrite for [ast::Attribute] {
419419
shape.with_max_width(context.config),
420420
context,
421421
0,
422+
false,
422423
)?;
423424
let comment = if comment.is_empty() {
424425
format!("\n{}", mlb)
@@ -449,6 +450,7 @@ impl Rewrite for [ast::Attribute] {
449450
shape.with_max_width(context.config),
450451
context,
451452
0,
453+
false,
452454
)?;
453455
result.push_str(&comment);
454456
if let Some(next) = attrs.get(derives.len()) {
@@ -482,6 +484,7 @@ impl Rewrite for [ast::Attribute] {
482484
shape.with_max_width(context.config),
483485
context,
484486
0,
487+
false,
485488
)?;
486489
result.push_str(&comment);
487490
if let Some(next) = attrs.get(1) {

src/comment.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -965,6 +965,7 @@ pub(crate) fn recover_missing_comment_in_span(
965965
shape: Shape,
966966
context: &RewriteContext<'_>,
967967
used_width: usize,
968+
remove_leading_newlines: bool,
968969
) -> Option<String> {
969970
let missing_comment = rewrite_missing_comment(span, shape, context)?;
970971
if missing_comment.is_empty() {
@@ -976,7 +977,9 @@ pub(crate) fn recover_missing_comment_in_span(
976977
let total_width = missing_comment.len() + used_width + 1;
977978
let force_new_line_before_comment =
978979
missing_snippet[..pos].contains('\n') || total_width > context.config.max_width();
979-
let sep = if force_new_line_before_comment {
980+
let sep = if remove_leading_newlines && force_new_line_before_comment {
981+
shape.indent.to_string(context.config)
982+
} else if force_new_line_before_comment {
980983
shape.indent.to_string_with_newline(context.config)
981984
} else {
982985
Cow::from(" ")

src/items.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,7 @@ pub(crate) fn format_impl(
713713
Shape::indented(offset, context.config),
714714
context,
715715
last_line_width(&result),
716+
false,
716717
) {
717718
Some(ref missing_comment) if !missing_comment.is_empty() => {
718719
result.push_str(missing_comment);
@@ -1092,6 +1093,7 @@ pub(crate) fn format_trait(
10921093
Shape::indented(offset, context.config),
10931094
context,
10941095
last_line_width(&result),
1096+
false,
10951097
) {
10961098
Some(ref missing_comment) if !missing_comment.is_empty() => {
10971099
result.push_str(missing_comment);
@@ -2482,6 +2484,7 @@ fn rewrite_fn_base(
24822484
shape,
24832485
context,
24842486
last_line_width(&result),
2487+
false,
24852488
) {
24862489
Some(ref missing_comment) if !missing_comment.is_empty() => {
24872490
result.push_str(missing_comment);

0 commit comments

Comments
 (0)