Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/formatting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,14 @@ impl<'a, T: FormatHandler + 'a> FormatContext<'a, T> {

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

format_lines(
&mut visitor.buffer,
Expand Down
30 changes: 23 additions & 7 deletions src/missed_spans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ impl<'a> FmtVisitor<'a> {
let config = self.config;
self.format_missing_inner(end, |this, last_snippet, snippet| {
this.push_str(last_snippet.trim_end());
if last_snippet == snippet && !this.output_at_start() {
if last_snippet == snippet
&& !this.output_at_start()
&& !out_of_file_lines_range!(this, mk_sp(this.last_pos, end))
{
// No new lines in the snippet.
this.push_str("\n");
}
Expand Down Expand Up @@ -100,7 +103,11 @@ impl<'a> FmtVisitor<'a> {
let snippet = self.snippet(span);

// Do nothing for spaces in the beginning of the file
if start == BytePos(0) && end.0 as usize == snippet.len() && snippet.trim().is_empty() {
if start == BytePos(0)
&& end.0 as usize == snippet.len()
&& snippet.trim().is_empty()
&& !out_of_file_lines_range!(self, span)
{
return;
}

Expand Down Expand Up @@ -357,11 +364,20 @@ impl<'a> FmtVisitor<'a> {
}
}

let remaining = snippet[status.line_start..subslice.len() + offset].trim();
if !remaining.is_empty() {
self.push_str(&self.block_indent.to_string(self.config));
self.push_str(remaining);
status.line_start = subslice.len() + offset;
let mut remaining = &snippet[status.line_start..subslice.len() + offset];
status.line_start = subslice.len() + offset;

let skip_this_line = !self
.config
.file_lines()
.contains_line(file_name, status.cur_line);
if !skip_this_line {
remaining = remaining.trim();
if !remaining.is_empty() {
self.push_str(&self.block_indent.to_string(self.config));
}
}

self.push_str(remaining);
}
}
16 changes: 12 additions & 4 deletions src/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -895,8 +895,12 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
return false;
}

let rewrite = attrs.rewrite(&self.get_context(), self.shape());
let span = mk_sp(attrs[0].span.lo(), attrs[attrs.len() - 1].span.hi());
if out_of_file_lines_range!(self, span) {
return false;
}

let rewrite = attrs.rewrite(&self.get_context(), self.shape());
self.push_rewrite(span, rewrite);

false
Expand Down Expand Up @@ -1028,12 +1032,16 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
.snippet_provider
.opt_span_after(self.next_span(end_pos), "\n")
{
let span = self.next_span(pos);
if let Some(snippet) = self.opt_snippet(self.next_span(pos)) {
if snippet.trim().is_empty() {
self.last_pos = pos;
} else {
if !snippet.trim().is_empty() {
return;
}

if out_of_file_lines_range!(self, span) {
return;
}
self.last_pos = pos;
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions tests/source/issue-5136-1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@


// Test that newlines at the top of this file are preserved when they're not
// in the --file-lines range.

// This should prevent rustfmt from many any formatting changes at all:
// rustfmt-file_lines: []
5 changes: 5 additions & 0 deletions tests/source/issue-5136-2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
use std;
// Test that whitespace at beginning of file is preserved when not in
// --file-lines range.
// This should prevent rustfmt from making any formatting changes at all:
// rustfmt-file_lines: []
6 changes: 6 additions & 0 deletions tests/source/issue-5136-3.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// rustfmt-file_lines: []
// Test that a missing newline at the end of the file is preserved when the last
// line is not in the --file-lines range.
// Important: When editing this file, make sure not to add a newline at the end
// of the last line.
use std;
4 changes: 4 additions & 0 deletions tests/source/issue-5136-4.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// rustfmt-file_lines: []
// Test that a missing space at the end of a doc comment is preserved when the
// line is not in the --file-lines range.
//!
6 changes: 6 additions & 0 deletions tests/source/issue-5136-5.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// rustfmt-file_lines: []
// Test that the space before the comment is not removed if the line is not
// contained in `--file-lines`.
// Note: It's important for the bug to repro that there is no newline at the
// end of the comment
fn f(){} // what
7 changes: 7 additions & 0 deletions tests/target/issue-5136-1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@


// Test that newlines at the top of this file are preserved when they're not
// in the --file-lines range.

// This should prevent rustfmt from many any formatting changes at all:
// rustfmt-file_lines: []
5 changes: 5 additions & 0 deletions tests/target/issue-5136-2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
use std;
// Test that whitespace at beginning of file is preserved when not in
// --file-lines range.
// This should prevent rustfmt from making any formatting changes at all:
// rustfmt-file_lines: []
6 changes: 6 additions & 0 deletions tests/target/issue-5136-3.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// rustfmt-file_lines: []
// Test that a missing newline at the end of the file is preserved when the last
// line is not in the --file-lines range.
// Important: When editing this file, make sure not to add a newline at the end
// of the last line.
use std;
4 changes: 4 additions & 0 deletions tests/target/issue-5136-4.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// rustfmt-file_lines: []
// Test that a missing space at the end of a doc comment is preserved when the
// line is not in the --file-lines range.
//!
6 changes: 6 additions & 0 deletions tests/target/issue-5136-5.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// rustfmt-file_lines: []
// Test that the space before the comment is not removed if the line is not
// contained in `--file-lines`.
// Note: It's important for the bug to repro that there is no newline at the
// end of the comment
fn f(){} // what
Loading