-
Notifications
You must be signed in to change notification settings - Fork 922
Do not normalize vertical spaces unless they are between items #4295
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
topecongiro
merged 2 commits into
rust-lang:master
from
topecongiro:normalize-vertical-spaces-between-items
Jul 3, 2020
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -50,7 +50,8 @@ impl<'a> FmtVisitor<'a> { | |||||
self.last_pos = end; | ||||||
return; | ||||||
} | ||||||
self.format_missing_inner(end, |this, last_snippet, _| this.push_str(last_snippet)) | ||||||
self.format_missing_inner(end, |this, last_snippet, _| this.push_str(last_snippet)); | ||||||
self.normalize_vertical_spaces = false; | ||||||
} | ||||||
|
||||||
pub(crate) fn format_missing_with_indent(&mut self, end: BytePos) { | ||||||
|
@@ -63,13 +64,15 @@ impl<'a> FmtVisitor<'a> { | |||||
} | ||||||
let indent = this.block_indent.to_string(config); | ||||||
this.push_str(&indent); | ||||||
}) | ||||||
}); | ||||||
self.normalize_vertical_spaces = false; | ||||||
} | ||||||
|
||||||
pub(crate) fn format_missing_no_indent(&mut self, end: BytePos) { | ||||||
self.format_missing_inner(end, |this, last_snippet, _| { | ||||||
this.push_str(last_snippet.trim_end()); | ||||||
}) | ||||||
}); | ||||||
self.normalize_vertical_spaces = false; | ||||||
} | ||||||
|
||||||
fn format_missing_inner<F: Fn(&mut FmtVisitor<'_>, &str, &str)>( | ||||||
|
@@ -113,7 +116,7 @@ impl<'a> FmtVisitor<'a> { | |||||
} | ||||||
} | ||||||
|
||||||
fn push_vertical_spaces(&mut self, mut newline_count: usize) { | ||||||
fn normalize_newline_count(&self, mut newline_count: usize) -> usize { | ||||||
let offset = self.buffer.chars().rev().take_while(|c| *c == '\n').count(); | ||||||
let newline_upper_bound = self.config.blank_lines_upper_bound() + 1; | ||||||
let newline_lower_bound = self.config.blank_lines_lower_bound() + 1; | ||||||
|
@@ -132,6 +135,16 @@ impl<'a> FmtVisitor<'a> { | |||||
} | ||||||
} | ||||||
|
||||||
newline_count | ||||||
} | ||||||
|
||||||
fn push_vertical_spaces(&mut self, mut newline_count: usize) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
if self.normalize_vertical_spaces { | ||||||
newline_count = self.normalize_newline_count(newline_count); | ||||||
} else if newline_count < 1 { | ||||||
newline_count = 1; | ||||||
} | ||||||
|
||||||
let blank_lines = "\n".repeat(newline_count); | ||||||
self.push_str(&blank_lines); | ||||||
} | ||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// rustfmt-blank_lines_lower_bound: 2 | ||
// rustfmt-blank_lines_upper_bound: 3 | ||
|
||
#[foo] | ||
fn foo() { | ||
println!("a"); | ||
} | ||
#[bar] | ||
#[barbar] | ||
fn bar() { | ||
println!("b"); | ||
println!("c"); | ||
} | ||
struct Foo {} | ||
enum Bar {} | ||
use std::io; | ||
extern crate foobar; | ||
extern crate foo; | ||
extern crate bar; | ||
trait Foo = Bar; | ||
impl Foo {} | ||
mac!(); | ||
#[temp] | ||
use std::fs; | ||
use std::alloc; | ||
use std::ascii; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ fn main() { | |
single_line_fit = 5; | ||
single_lit_fit >>= 10; | ||
|
||
|
||
// #2791 | ||
let x = 2; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// rustfmt-blank_lines_lower_bound: 2 | ||
// rustfmt-blank_lines_upper_bound: 3 | ||
|
||
|
||
#[foo] | ||
fn foo() { | ||
println!("a"); | ||
} | ||
|
||
|
||
#[bar] | ||
#[barbar] | ||
fn bar() { | ||
println!("b"); | ||
println!("c"); | ||
} | ||
|
||
|
||
struct Foo {} | ||
|
||
|
||
enum Bar {} | ||
|
||
|
||
use std::io; | ||
|
||
|
||
extern crate bar; | ||
extern crate foo; | ||
extern crate foobar; | ||
|
||
|
||
trait Foo = Bar; | ||
|
||
|
||
impl Foo {} | ||
|
||
|
||
mac!(); | ||
|
||
|
||
use std::alloc; | ||
use std::ascii; | ||
#[temp] | ||
use std::fs; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,24 +7,28 @@ fn main() { | |
let bar = (); | ||
} | ||
|
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am adding blank lines to this and subsequent targets to match the number of blank lines in the corresponding source. |
||
'label: loop | ||
// loop comment | ||
{ | ||
let foo = (); | ||
} | ||
|
||
|
||
cond = true; | ||
while cond | ||
{ | ||
let foo = (); | ||
} | ||
|
||
|
||
'while_label: while cond | ||
{ | ||
// while comment | ||
let foo = (); | ||
} | ||
|
||
|
||
for obj in iter | ||
{ | ||
for sub_obj in obj | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -129,6 +129,7 @@ fn main() { | |
println!("{}", i); | ||
} | ||
|
||
|
||
while true { | ||
hello(); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for the borderline pun, but could we normalize the variable/function names and replace the old
vertical_spaces
withnewline
consistently everywhere?