-
Notifications
You must be signed in to change notification settings - Fork 931
Remove blank lines at start or end of block #1872
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
Conversation
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.
Thank you for your PR! This looks good, I left a minor suggestion inline.
src/visitor.rs
Outdated
@@ -131,6 +131,27 @@ impl<'a> FmtVisitor<'a> { | |||
self.block_indent = self.block_indent.block_indent(self.config); | |||
self.buffer.push_str("{"); | |||
|
|||
if self.config.remove_blank_lines_at_start_or_end_of_block() { | |||
if let Some(stmt) = b.stmts.first() { | |||
let snippet = self.snippet(mk_sp(self.last_pos, stmt.span.lo)); |
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.
Could we use a byte pos from inner attributes to construct this snippet and avoid calling s.trim_left_matches(&[' ', '\t', '\r', '\n'][..]).len()]
below?
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.
Done.
src/visitor.rs
Outdated
.map(|attr| attr.span.lo) | ||
}) | ||
.or_else(|| { | ||
// syntax BUG: `attrs()` returns nothing for items |
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.
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.
I think we could avoid this by using Spanned
trait in lib.rs (first_stmt.span().lo
).
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.
I tried, but test failed within tests/target/attrib-extern-crate.rs
in the same way as first_stmt.attrs()
.
https://github.com/rust-lang-nursery/rustfmt/blob/0ee76bec0f6a76b7b26eebdbd5700e9c727a96b3/src/lib.rs#L112-L118 looks like it does nothing to StmtKind::Item
. Should this be modified to include attributes for items?
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.
It turned out that the behavior of attrs()
is intentional; attributes for an item in a statement position don't belong to the statement (rust-lang/rust@b968ee3). Perhaps Spanned
follows the convention.
src/visitor.rs
Outdated
.and_then(|attrs| { | ||
attrs | ||
.iter() | ||
.filter(|a| a.style == ast::AttrStyle::Inner) |
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.
We could use inner_attributes()
in utils.rs here.
Thank you for your update! I left inline comments again, please feel free to ask if there are anything unclear. |
Thanks! |
Fixes #1274