Skip to content

Commit 5b8c410

Browse files
committed
Use blank_lines_* to control newlines between module level attrs
Closes 4082 Now semantic meaning created by grouping attributes into "paragraphs" is maintained. To prevent breaking formatting changes newlines are only affected when `version=Two` is set.
1 parent fba7492 commit 5b8c410

File tree

8 files changed

+150
-1
lines changed

8 files changed

+150
-1
lines changed

src/attr.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ use self::doc_comment::DocCommentFormatter;
88
use crate::comment::{contains_comment, rewrite_doc_comment, CommentStyle};
99
use crate::config::lists::*;
1010
use crate::config::IndentStyle;
11+
use crate::config::Version;
1112
use crate::expr::rewrite_literal;
1213
use crate::lists::{definitive_tactic, itemize_list, write_list, ListFormatting, Separator};
14+
use crate::missed_spans::push_vertical_spaces;
1315
use crate::overflow;
1416
use crate::rewrite::{Rewrite, RewriteContext};
1517
use crate::shape::Shape;
@@ -478,13 +480,25 @@ impl Rewrite for [ast::Attribute] {
478480
let missing_span = attrs
479481
.get(1)
480482
.map(|next| mk_sp(attrs[0].span.hi(), next.span.lo()));
483+
484+
let mut vertical_lines_pushed = 0;
481485
if let Some(missing_span) = missing_span {
486+
let snippet = context.snippet(missing_span);
487+
if let Some((whitespace, _)) = snippet.split_once(|c: char| !c.is_whitespace()) {
488+
let newlines = count_newlines(whitespace);
489+
// Version gate this change as it leads to breaking changes with Version::One
490+
if newlines > 0 && context.config.version() == Version::Two {
491+
vertical_lines_pushed =
492+
push_vertical_spaces(&mut result, context.config, newlines);
493+
}
494+
}
495+
// remove leading newlines if we already pushed vertical space into the result
482496
let comment = crate::comment::recover_missing_comment_in_span(
483497
missing_span,
484498
shape.with_max_width(context.config),
485499
context,
486500
0,
487-
false,
501+
vertical_lines_pushed > 0,
488502
)?;
489503
result.push_str(&comment);
490504
if let Some(next) = attrs.get(1) {
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// rustfmt-version: One
2+
3+
#![feature(core_intrinsics)]
4+
#![feature(lang_items)]
5+
#![feature(libc)]
6+
#![feature(nll)]
7+
#![feature(panic_unwind)]
8+
#![feature(staged_api)]
9+
#![feature(std_internals)]
10+
#![feature(unwind_attributes)]
11+
#![feature(abi_thiscall)]
12+
#![feature(rustc_attrs)]
13+
#![feature(raw)]
14+
#![panic_runtime]
15+
#![feature(panic_runtime)]
16+
17+
// `real_imp` is unused with Miri, so silence warnings.
18+
#![cfg_attr(miri, allow(dead_code))]
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// rustfmt-version: Two
2+
// rustfmt-blank_lines_upper_bound: 2
3+
// rustfmt-blank_lines_lower_bound: 2
4+
#![feature(core_intrinsics)]
5+
#![feature(lang_items)]
6+
#![feature(libc)]
7+
#![feature(nll)]
8+
#![feature(panic_unwind)]
9+
#![feature(staged_api)]
10+
#![feature(std_internals)]
11+
#![feature(unwind_attributes)]
12+
#![feature(abi_thiscall)]
13+
#![feature(rustc_attrs)]
14+
#![feature(raw)]
15+
#![panic_runtime]
16+
#![feature(panic_runtime)]
17+
18+
// `real_imp` is unused with Miri, so silence warnings.
19+
#![cfg_attr(miri, allow(dead_code))]
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// rustfmt-version: Two
2+
// rustfmt-blank_lines_upper_bound: 2
3+
// rustfmt-blank_lines_lower_bound: 2
4+
#![feature(core_intrinsics)]
5+
#![feature(lang_items)]
6+
#![feature(libc)]
7+
#![feature(nll)]
8+
#![feature(panic_unwind)]
9+
#![feature(staged_api)]
10+
#![feature(std_internals)]
11+
#![feature(unwind_attributes)]
12+
#![feature(abi_thiscall)]
13+
#![feature(rustc_attrs)]
14+
#![feature(raw)]
15+
#![panic_runtime]
16+
#![feature(panic_runtime)]
17+
18+
19+
20+
21+
22+
// `real_imp` is unused with Miri, so silence warnings.
23+
#![cfg_attr(miri, allow(dead_code))]
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// rustfmt-version: One
2+
3+
#![feature(core_intrinsics)]
4+
#![feature(lang_items)]
5+
#![feature(libc)]
6+
#![feature(nll)]
7+
#![feature(panic_unwind)]
8+
#![feature(staged_api)]
9+
#![feature(std_internals)]
10+
#![feature(unwind_attributes)]
11+
#![feature(abi_thiscall)]
12+
#![feature(rustc_attrs)]
13+
#![feature(raw)]
14+
#![panic_runtime]
15+
#![feature(panic_runtime)]
16+
// `real_imp` is unused with Miri, so silence warnings.
17+
#![cfg_attr(miri, allow(dead_code))]
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// rustfmt-version: Two
2+
// rustfmt-blank_lines_upper_bound: 2
3+
// rustfmt-blank_lines_lower_bound: 2
4+
#![feature(core_intrinsics)]
5+
#![feature(lang_items)]
6+
#![feature(libc)]
7+
#![feature(nll)]
8+
#![feature(panic_unwind)]
9+
#![feature(staged_api)]
10+
#![feature(std_internals)]
11+
#![feature(unwind_attributes)]
12+
#![feature(abi_thiscall)]
13+
#![feature(rustc_attrs)]
14+
#![feature(raw)]
15+
#![panic_runtime]
16+
#![feature(panic_runtime)]
17+
18+
19+
// `real_imp` is unused with Miri, so silence warnings.
20+
#![cfg_attr(miri, allow(dead_code))]
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// rustfmt-version: Two
2+
3+
#![feature(core_intrinsics)]
4+
#![feature(lang_items)]
5+
#![feature(libc)]
6+
#![feature(nll)]
7+
#![feature(panic_unwind)]
8+
#![feature(staged_api)]
9+
#![feature(std_internals)]
10+
#![feature(unwind_attributes)]
11+
#![feature(abi_thiscall)]
12+
#![feature(rustc_attrs)]
13+
#![feature(raw)]
14+
#![panic_runtime]
15+
#![feature(panic_runtime)]
16+
17+
// `real_imp` is unused with Miri, so silence warnings.
18+
#![cfg_attr(miri, allow(dead_code))]
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// rustfmt-version: Two
2+
// rustfmt-blank_lines_upper_bound: 2
3+
// rustfmt-blank_lines_lower_bound: 2
4+
#![feature(core_intrinsics)]
5+
#![feature(lang_items)]
6+
#![feature(libc)]
7+
#![feature(nll)]
8+
#![feature(panic_unwind)]
9+
#![feature(staged_api)]
10+
#![feature(std_internals)]
11+
#![feature(unwind_attributes)]
12+
#![feature(abi_thiscall)]
13+
#![feature(rustc_attrs)]
14+
#![feature(raw)]
15+
#![panic_runtime]
16+
#![feature(panic_runtime)]
17+
18+
19+
// `real_imp` is unused with Miri, so silence warnings.
20+
#![cfg_attr(miri, allow(dead_code))]

0 commit comments

Comments
 (0)