Remove remove_blank_lines_at_start_or_end_of_block

cc #1974
This commit is contained in:
Nick Cameron 2018-05-18 16:56:55 +12:00
parent dd9c15ad01
commit 2ee8b0e4c5
3 changed files with 50 additions and 95 deletions

View file

@ -1991,45 +1991,6 @@ fn bar() {
} }
``` ```
## `remove_blank_lines_at_start_or_end_of_block`
Remove blank lines at the start or the end of a block.
- **Default value**: `true`
- **Possible values**: `true`, `false`
- **Stable**: No
#### `true`
```rust
fn foo() {
let msg = {
let mut str = String::new();
str.push_str("hello, ");
str.push_str("world!");
str
};
println!("{}", msg);
}
```
#### `false`
```rust
fn foo() {
let msg = {
let mut str = String::new();
str.push_str("hello, ");
str.push_str("world!");
str
};
println!("{}", msg);
}
```
## `required_version` ## `required_version`

View file

@ -83,8 +83,6 @@ create_config! {
// Misc. // Misc.
remove_nested_parens: bool, true, true, "Remove nested parens."; remove_nested_parens: bool, true, true, "Remove nested parens.";
remove_blank_lines_at_start_or_end_of_block: bool, true, false,
"Remove blank lines at start or end of a block";
combine_control_expr: bool, true, false, "Combine control expressions with function calls."; combine_control_expr: bool, true, false, "Combine control expressions with function calls.";
struct_field_align_threshold: usize, 0, false, "Align struct fields if their diffs fits within \ struct_field_align_threshold: usize, 0, false, "Align struct fields if their diffs fits within \
threshold."; threshold.";

View file

@ -128,45 +128,43 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
self.block_indent = self.block_indent.block_indent(self.config); self.block_indent = self.block_indent.block_indent(self.config);
self.push_str("{"); self.push_str("{");
if self.config.remove_blank_lines_at_start_or_end_of_block() { if let Some(first_stmt) = b.stmts.first() {
if let Some(first_stmt) = b.stmts.first() { let attr_lo = inner_attrs
let attr_lo = inner_attrs .and_then(|attrs| inner_attributes(attrs).first().map(|attr| attr.span.lo()))
.and_then(|attrs| inner_attributes(attrs).first().map(|attr| attr.span.lo())) .or_else(|| {
.or_else(|| { // Attributes for an item in a statement position
// Attributes for an item in a statement position // do not belong to the statement. (rust-lang/rust#34459)
// do not belong to the statement. (rust-lang/rust#34459) if let ast::StmtKind::Item(ref item) = first_stmt.node {
if let ast::StmtKind::Item(ref item) = first_stmt.node { item.attrs.first()
item.attrs.first() } else {
} else { first_stmt.attrs().first()
first_stmt.attrs().first() }.and_then(|attr| {
}.and_then(|attr| { // Some stmts can have embedded attributes.
// Some stmts can have embedded attributes. // e.g. `match { #![attr] ... }`
// e.g. `match { #![attr] ... }` let attr_lo = attr.span.lo();
let attr_lo = attr.span.lo(); if attr_lo < first_stmt.span.lo() {
if attr_lo < first_stmt.span.lo() { Some(attr_lo)
Some(attr_lo)
} else {
None
}
})
});
let snippet = self.snippet(mk_sp(
self.last_pos,
attr_lo.unwrap_or_else(|| first_stmt.span.lo()),
));
let len = CommentCodeSlices::new(snippet)
.nth(0)
.and_then(|(kind, _, s)| {
if kind == CodeCharKind::Normal {
s.rfind('\n')
} else { } else {
None None
} }
}); })
if let Some(len) = len { });
self.last_pos = self.last_pos + BytePos::from_usize(len);
} let snippet = self.snippet(mk_sp(
self.last_pos,
attr_lo.unwrap_or_else(|| first_stmt.span.lo()),
));
let len = CommentCodeSlices::new(snippet)
.nth(0)
.and_then(|(kind, _, s)| {
if kind == CodeCharKind::Normal {
s.rfind('\n')
} else {
None
}
});
if let Some(len) = len {
self.last_pos = self.last_pos + BytePos::from_usize(len);
} }
} }
@ -195,24 +193,22 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
} }
let mut remove_len = BytePos(0); let mut remove_len = BytePos(0);
if self.config.remove_blank_lines_at_start_or_end_of_block() { if let Some(stmt) = b.stmts.last() {
if let Some(stmt) = b.stmts.last() { let snippet = self.snippet(mk_sp(
let snippet = self.snippet(mk_sp( stmt.span.hi(),
stmt.span.hi(), source!(self, b.span).hi() - brace_compensation,
source!(self, b.span).hi() - brace_compensation, ));
)); let len = CommentCodeSlices::new(snippet)
let len = CommentCodeSlices::new(snippet) .last()
.last() .and_then(|(kind, _, s)| {
.and_then(|(kind, _, s)| { if kind == CodeCharKind::Normal && s.trim().is_empty() {
if kind == CodeCharKind::Normal && s.trim().is_empty() { Some(s.len())
Some(s.len()) } else {
} else { None
None }
} });
}); if let Some(len) = len {
if let Some(len) = len { remove_len = BytePos::from_usize(len);
remove_len = BytePos::from_usize(len);
}
} }
} }