Skip to content

Commit 7159095

Browse files
authored
Do not normalize vertical spaces unless they are between items (#4295)
2 parents cccf7fe + 146c3e6 commit 7159095

16 files changed

+123
-12
lines changed

Diff for: Configurations.md

-3
Original file line numberDiff line numberDiff line change
@@ -164,14 +164,11 @@ fn bar() {
164164
#### `1`
165165
```rust
166166
fn foo() {
167-
168167
println!("a");
169168
}
170169

171170
fn bar() {
172-
173171
println!("b");
174-
175172
println!("c");
176173
}
177174
```

Diff for: src/formatting/macros.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ impl Rewrite for ast::Item {
7575
let mut visitor = FmtVisitor::from_context(context);
7676
visitor.block_indent = shape.indent;
7777
visitor.last_pos = self.span().lo();
78-
visitor.visit_item(self);
78+
visitor.visit_item(self, false);
7979
Some(visitor.buffer.to_owned())
8080
}
8181
}
@@ -1562,7 +1562,7 @@ fn rewrite_macro_with_items(
15621562
MacroArg::Item(item) => item,
15631563
_ => return None,
15641564
};
1565-
visitor.visit_item(&item);
1565+
visitor.visit_item(&item, false);
15661566
}
15671567

15681568
let mut result = String::with_capacity(256);

Diff for: src/formatting/missed_spans.rs

+17-4
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ impl<'a> FmtVisitor<'a> {
5050
self.last_pos = end;
5151
return;
5252
}
53-
self.format_missing_inner(end, |this, last_snippet, _| this.push_str(last_snippet))
53+
self.format_missing_inner(end, |this, last_snippet, _| this.push_str(last_snippet));
54+
self.normalize_vertical_spaces = false;
5455
}
5556

5657
pub(crate) fn format_missing_with_indent(&mut self, end: BytePos) {
@@ -63,13 +64,15 @@ impl<'a> FmtVisitor<'a> {
6364
}
6465
let indent = this.block_indent.to_string(config);
6566
this.push_str(&indent);
66-
})
67+
});
68+
self.normalize_vertical_spaces = false;
6769
}
6870

6971
pub(crate) fn format_missing_no_indent(&mut self, end: BytePos) {
7072
self.format_missing_inner(end, |this, last_snippet, _| {
7173
this.push_str(last_snippet.trim_end());
72-
})
74+
});
75+
self.normalize_vertical_spaces = false;
7376
}
7477

7578
fn format_missing_inner<F: Fn(&mut FmtVisitor<'_>, &str, &str)>(
@@ -113,7 +116,7 @@ impl<'a> FmtVisitor<'a> {
113116
}
114117
}
115118

116-
fn push_vertical_spaces(&mut self, mut newline_count: usize) {
119+
fn normalize_newline_count(&self, mut newline_count: usize) -> usize {
117120
let offset = self.buffer.chars().rev().take_while(|c| *c == '\n').count();
118121
let newline_upper_bound = self.config.blank_lines_upper_bound() + 1;
119122
let newline_lower_bound = self.config.blank_lines_lower_bound() + 1;
@@ -132,6 +135,16 @@ impl<'a> FmtVisitor<'a> {
132135
}
133136
}
134137

138+
newline_count
139+
}
140+
141+
fn push_vertical_spaces(&mut self, mut newline_count: usize) {
142+
if self.normalize_vertical_spaces {
143+
newline_count = self.normalize_newline_count(newline_count);
144+
} else if newline_count < 1 {
145+
newline_count = 1;
146+
}
147+
135148
let blank_lines = "\n".repeat(newline_count);
136149
self.push_str(&blank_lines);
137150
}

Diff for: src/formatting/reorder.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
351351
.any(|item| !out_of_file_lines_range!(self, item.span));
352352

353353
if at_least_one_in_file_lines && !items.is_empty() {
354+
self.normalize_vertical_spaces = true;
354355
let lo = items.first().unwrap().span().lo();
355356
let hi = items.last().unwrap().span().hi();
356357
let span = mk_sp(lo, hi);
@@ -382,7 +383,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
382383
// Reaching here means items were not reordered. There must be at least
383384
// one item left in `items`, so calling `unwrap()` here is safe.
384385
let (item, rest) = items.split_first().unwrap();
385-
self.visit_item(item);
386+
self.visit_item(item, true);
386387
items = rest;
387388
}
388389
}

Diff for: src/formatting/visitor.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ pub(crate) struct FmtVisitor<'a> {
8888
pub(crate) macro_rewrite_failure: bool,
8989
pub(crate) report: FormatReport,
9090
pub(crate) skip_context: SkipContext,
91+
/// If set to `true`, normalize number of vertical spaces on formatting missing snippets.
92+
pub(crate) normalize_vertical_spaces: bool,
9193
}
9294

9395
impl<'a> Drop for FmtVisitor<'a> {
@@ -141,7 +143,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
141143

142144
match stmt.as_ast_node().kind {
143145
ast::StmtKind::Item(ref item) => {
144-
self.visit_item(item);
146+
self.visit_item(item, true);
145147
// If the item requires a trailing ";" (like `struct Foo;`), we should have already
146148
// handled it. Otherwise there still may be a trailing ";", but it is unnecessary.
147149
// Drop it by fast-forwarding the visitor to the end of the item.
@@ -408,7 +410,12 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
408410
self.visit_block(block, inner_attrs, true)
409411
}
410412

411-
pub(crate) fn visit_item(&mut self, item: &ast::Item) {
413+
pub(crate) fn visit_item(&mut self, item: &ast::Item, normalize_spaces: bool) {
414+
self.normalize_vertical_spaces = normalize_spaces;
415+
self.visit_item_inner(item);
416+
}
417+
418+
fn visit_item_inner(&mut self, item: &ast::Item) {
412419
skip_out_of_file_lines_range_visitor!(self, item.span);
413420

414421
// This is where we bail out if there is a skip attribute. This is only
@@ -819,6 +826,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
819826
macro_rewrite_failure: false,
820827
report,
821828
skip_context: Default::default(),
829+
normalize_vertical_spaces: false,
822830
}
823831
}
824832

Diff for: tests/source/configs/blank_lines_lower_bound/2.rs

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// rustfmt-blank_lines_lower_bound: 2
2+
// rustfmt-blank_lines_upper_bound: 3
3+
4+
#[foo]
5+
fn foo() {
6+
println!("a");
7+
}
8+
#[bar]
9+
#[barbar]
10+
fn bar() {
11+
println!("b");
12+
println!("c");
13+
}
14+
struct Foo {}
15+
enum Bar {}
16+
use std::io;
17+
extern crate foobar;
18+
extern crate foo;
19+
extern crate bar;
20+
trait Foo = Bar;
21+
impl Foo {}
22+
mac!();
23+
#[temp]
24+
use std::fs;
25+
use std::alloc;
26+
use std::ascii;

Diff for: tests/target/assignment.rs

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ fn main() {
1616
single_line_fit = 5;
1717
single_lit_fit >>= 10;
1818

19+
1920
// #2791
2021
let x = 2;
2122
}

Diff for: tests/target/configs/blank_lines_lower_bound/2.rs

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// rustfmt-blank_lines_lower_bound: 2
2+
// rustfmt-blank_lines_upper_bound: 3
3+
4+
5+
#[foo]
6+
fn foo() {
7+
println!("a");
8+
}
9+
10+
11+
#[bar]
12+
#[barbar]
13+
fn bar() {
14+
println!("b");
15+
println!("c");
16+
}
17+
18+
19+
struct Foo {}
20+
21+
22+
enum Bar {}
23+
24+
25+
use std::io;
26+
27+
28+
extern crate bar;
29+
extern crate foo;
30+
extern crate foobar;
31+
32+
33+
trait Foo = Bar;
34+
35+
36+
impl Foo {}
37+
38+
39+
mac!();
40+
41+
42+
use std::alloc;
43+
use std::ascii;
44+
#[temp]
45+
use std::fs;

Diff for: tests/target/control-brace-style-always-next-line.rs

+4
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,28 @@ fn main() {
77
let bar = ();
88
}
99

10+
1011
'label: loop
1112
// loop comment
1213
{
1314
let foo = ();
1415
}
1516

17+
1618
cond = true;
1719
while cond
1820
{
1921
let foo = ();
2022
}
2123

24+
2225
'while_label: while cond
2326
{
2427
// while comment
2528
let foo = ();
2629
}
2730

31+
2832
for obj in iter
2933
{
3034
for sub_obj in obj

Diff for: tests/target/control-brace-style-always-same-line.rs

+4
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,26 @@ fn main() {
44
let bar = ();
55
}
66

7+
78
'label: loop
89
// loop comment
910
{
1011
let foo = ();
1112
}
1213

14+
1315
cond = true;
1416
while cond {
1517
let foo = ();
1618
}
1719

20+
1821
'while_label: while cond {
1922
// while comment
2023
let foo = ();
2124
}
2225

26+
2327
for obj in iter {
2428
for sub_obj in obj {
2529
'nested_while_label: while cond {

Diff for: tests/target/else-if-brace-style-always-next-line.rs

+2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@ fn main() {
1414
let bar = ();
1515
}
1616

17+
1718
let a = if 0 > 1 { unreachable!() } else { 0x0 };
1819

20+
1921
if true
2022
{
2123
let foo = ();

Diff for: tests/target/else-if-brace-style-always-same-line.rs

+2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ fn main() {
1111
let bar = ();
1212
}
1313

14+
1415
let a = if 0 > 1 { unreachable!() } else { 0x0 };
1516

17+
1618
if true {
1719
let foo = ();
1820
} else if false {

Diff for: tests/target/else-if-brace-style-closing-next-line.rs

+2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ fn main() {
1313
let bar = ();
1414
}
1515

16+
1617
let a = if 0 > 1 { unreachable!() } else { 0x0 };
1718

19+
1820
if true {
1921
let foo = ();
2022
}

Diff for: tests/target/extern.rs

+1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ extern "C" {
7474
file: *mut FILE,
7575
) -> *mut FILE;
7676

77+
7778
async fn foo() -> *mut Bar;
7879
const fn foo() -> *mut Bar;
7980
unsafe fn foo() -> *mut Bar;

Diff for: tests/target/multiple.rs

+1
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ fn main() {
129129
println!("{}", i);
130130
}
131131

132+
132133
while true {
133134
hello();
134135
}

Diff for: tests/target/remove_blank_lines.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
fn main() {
22
let x = 1;
33

4+
45
let y = 2;
56

7+
68
println!("x + y = {}", x + y);
79
}
810

@@ -20,9 +22,11 @@ fn bar() {
2022
let x = 1;
2123
// comment after statement
2224

25+
2326
// comment before statement
2427
let y = 2;
2528
let z = 3;
2629

30+
2731
println!("x + y + z = {}", x + y + z);
2832
}

0 commit comments

Comments
 (0)