Skip to content

Commit 146c3e6

Browse files
committed
Do not normalize vertical spaces unless they are between items
1 parent f44a5ed commit 146c3e6

File tree

4 files changed

+31
-9
lines changed

4 files changed

+31
-9
lines changed

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

0 commit comments

Comments
 (0)