Skip to content

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

Merged
merged 4 commits into from
Aug 14, 2017

Conversation

sinkuu
Copy link
Contributor

@sinkuu sinkuu commented Aug 11, 2017

Fixes #1274

Copy link
Contributor

@topecongiro topecongiro left a 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));
Copy link
Contributor

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?

Copy link
Contributor Author

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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

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).

Copy link
Contributor Author

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?

Copy link
Contributor Author

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)
Copy link
Contributor

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.

@topecongiro
Copy link
Contributor

topecongiro commented Aug 12, 2017

Thank you for your update! I left inline comments again, please feel free to ask if there are anything unclear.

@topecongiro topecongiro merged commit a1d28bf into rust-lang:master Aug 14, 2017
@topecongiro
Copy link
Contributor

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants