Skip to content

Commit 79a23d2

Browse files
committed
Respect --file-lines when making various whitespace-related changes.
Various whitespace-related changes did not so far respect `--file-lines`, see also [#5136](tests/target/issue-5136-4.rs). This PR fixes this in all of the cases reported in #5136: * Newlines at the beginning of the file * Spaces at the beginning of the file * Missing newline at the end of the file * Space after a doc comment * Space before a comment in the last line of the file (without trailing newline) The PR includes tests that fail without the fixes.
1 parent 5e73127 commit 79a23d2

13 files changed

Lines changed: 99 additions & 12 deletions

src/formatting.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,14 @@ impl<'a, T: FormatHandler + 'a> FormatContext<'a, T> {
230230

231231
// For some reason, the source_map does not include terminating
232232
// newlines so we must add one on for each file. This is sad.
233-
source_file::append_newline(&mut visitor.buffer, self.config.style_edition());
233+
let num_newlines = count_newlines(&visitor.buffer);
234+
if self
235+
.config
236+
.file_lines()
237+
.contains_line(&path, num_newlines + 1)
238+
{
239+
source_file::append_newline(&mut visitor.buffer, self.config.style_edition());
240+
}
234241

235242
format_lines(
236243
&mut visitor.buffer,

src/missed_spans.rs

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,10 @@ impl<'a> FmtVisitor<'a> {
6363
let config = self.config;
6464
self.format_missing_inner(end, |this, last_snippet, snippet| {
6565
this.push_str(last_snippet.trim_end());
66-
if last_snippet == snippet && !this.output_at_start() {
66+
if last_snippet == snippet
67+
&& !this.output_at_start()
68+
&& !out_of_file_lines_range!(this, mk_sp(this.last_pos, end))
69+
{
6770
// No new lines in the snippet.
6871
this.push_str("\n");
6972
}
@@ -100,7 +103,11 @@ impl<'a> FmtVisitor<'a> {
100103
let snippet = self.snippet(span);
101104

102105
// Do nothing for spaces in the beginning of the file
103-
if start == BytePos(0) && end.0 as usize == snippet.len() && snippet.trim().is_empty() {
106+
if start == BytePos(0)
107+
&& end.0 as usize == snippet.len()
108+
&& snippet.trim().is_empty()
109+
&& !out_of_file_lines_range!(self, span)
110+
{
104111
return;
105112
}
106113

@@ -357,11 +364,20 @@ impl<'a> FmtVisitor<'a> {
357364
}
358365
}
359366

360-
let remaining = snippet[status.line_start..subslice.len() + offset].trim();
361-
if !remaining.is_empty() {
362-
self.push_str(&self.block_indent.to_string(self.config));
363-
self.push_str(remaining);
364-
status.line_start = subslice.len() + offset;
367+
let mut remaining = &snippet[status.line_start..subslice.len() + offset];
368+
status.line_start = subslice.len() + offset;
369+
370+
let skip_this_line = !self
371+
.config
372+
.file_lines()
373+
.contains_line(file_name, status.cur_line);
374+
if !skip_this_line {
375+
remaining = remaining.trim();
376+
if !remaining.is_empty() {
377+
self.push_str(&self.block_indent.to_string(self.config));
378+
}
365379
}
380+
381+
self.push_str(remaining);
366382
}
367383
}

src/visitor.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -895,8 +895,12 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
895895
return false;
896896
}
897897

898-
let rewrite = attrs.rewrite(&self.get_context(), self.shape());
899898
let span = mk_sp(attrs[0].span.lo(), attrs[attrs.len() - 1].span.hi());
899+
if out_of_file_lines_range!(self, span) {
900+
return false;
901+
}
902+
903+
let rewrite = attrs.rewrite(&self.get_context(), self.shape());
900904
self.push_rewrite(span, rewrite);
901905

902906
false
@@ -1028,12 +1032,16 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
10281032
.snippet_provider
10291033
.opt_span_after(self.next_span(end_pos), "\n")
10301034
{
1035+
let span = self.next_span(pos);
10311036
if let Some(snippet) = self.opt_snippet(self.next_span(pos)) {
1032-
if snippet.trim().is_empty() {
1033-
self.last_pos = pos;
1034-
} else {
1037+
if !snippet.trim().is_empty() {
1038+
return;
1039+
}
1040+
1041+
if out_of_file_lines_range!(self, span) {
10351042
return;
10361043
}
1044+
self.last_pos = pos;
10371045
}
10381046
}
10391047
}

tests/source/issue-5136-1.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
3+
// Test that newlines at the top of this file are preserved when they're not
4+
// in the --file-lines range.
5+
6+
// This should prevent rustfmt from many any formatting changes at all:
7+
// rustfmt-file_lines: []

tests/source/issue-5136-2.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
use std;
2+
// Test that whitespace at beginning of file is preserved when not in
3+
// --file-lines range.
4+
// This should prevent rustfmt from making any formatting changes at all:
5+
// rustfmt-file_lines: []

tests/source/issue-5136-3.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// rustfmt-file_lines: []
2+
// Test that a missing newline at the end of the file is preserved when the last
3+
// line is not in the --file-lines range.
4+
// Important: When editing this file, make sure not to add a newline at the end
5+
// of the last line.
6+
use std;

tests/source/issue-5136-4.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// rustfmt-file_lines: []
2+
// Test that a missing space at the end of a doc comment is preserved when the
3+
// line is not in the --file-lines range.
4+
//!

tests/source/issue-5136-5.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// rustfmt-file_lines: []
2+
// Test that the space before the comment is not removed if the line is not
3+
// contained in `--file-lines`.
4+
// Note: It's important for the bug to repro that there is no newline at the
5+
// end of the comment
6+
fn f(){} // what

tests/target/issue-5136-1.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
3+
// Test that newlines at the top of this file are preserved when they're not
4+
// in the --file-lines range.
5+
6+
// This should prevent rustfmt from many any formatting changes at all:
7+
// rustfmt-file_lines: []

tests/target/issue-5136-2.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
use std;
2+
// Test that whitespace at beginning of file is preserved when not in
3+
// --file-lines range.
4+
// This should prevent rustfmt from making any formatting changes at all:
5+
// rustfmt-file_lines: []

0 commit comments

Comments
 (0)