Skip to content

Configuration option to preserve blank lines at start of block #4303

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
Jul 14, 2020

Conversation

ayazhafiz
Copy link
Contributor

Adds the option for

  • Functions
  • Control flow blocks
  • Expression statements (i.e. let x = { ... };)
  • Traits
  • Trait Impls
  • Mods
  • Foreign Mods

If I am missing a kind of item, let me know :)

Closes #2868

@ayazhafiz
Copy link
Contributor Author

(don't let the 900 line diff scare you; ~800 of it is tests)

@calebcartwright
Copy link
Member

(don't let the 900 line diff scare you; ~800 of it is tests)

That made me chuckle, as my eyes did pop at first before I saw that note 👀

@calebcartwright
Copy link
Member

I know the original issue only referenced start of blocks, but I have a suspicion that a decent size chunk of users that would leverage this config option may also be interested in something similar for the end of blocks.

Do you think an equivalent end-of-block config and behavior would be feasible, and if so would it make sense for those to be two distinct config options or easier to use as one? Based on the explanation in the linked issue it sounds like there's scenarios where folks may want to keep the lines at the beginning but still trim at the end so separate would probably be best for more granular control.

@ayazhafiz
Copy link
Contributor Author

That makes sense to me, if people want one they may want the other. I definitely think it's feasible (and probably easily doable, but those are famous last words). However I also think it's worth waiting and seeing if there are any requests for such a configuration option, as it's easier to add an option than remove one (from a stability and "how many people are using this?" point of view). What do you think?

@ayazhafiz
Copy link
Contributor Author

This is totally orthogonal, but it would be interesting to pull GitHub data and see what are the most common rustfmt options are too.

@calebcartwright
Copy link
Member

However I also think it's worth waiting and seeing if there are any requests for such a configuration option, as it's easier to add an option than remove one (from a stability and "how many people are using this?" point of view). What do you think?

Agreed, and certainly no need to do anything with end of block lines as part of this PR, I'm betting it'll get requested sooner or later though.

but it would be interesting to pull GitHub data and see what are the most common rustfmt options are too.

Definitely! Funny you should mention this, as just the other day I was thinking about how beneficial it would be to have quantitative data about config option usage. There's some that we know are used pretty extensively in their respective corners (indent_style, hard_tabs, use_small_heuristics, etc.) but there's plenty of others where we don't really know.

Copy link
Member

@calebcartwright calebcartwright left a comment

Choose a reason for hiding this comment

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

Looks pretty good, thank you so much! I've left a few thoughts/questions inline for your review, but since this is adding a new config option I'll defer to @topecongiro for approval and merging

@ayazhafiz ayazhafiz requested a review from calebcartwright July 2, 2020 23:12
@calebcartwright
Copy link
Member

Thanks for the updates! I'll try to get back to this soon, though have also requested a review from @topecongiro for the reasons mentioned earlier in the thread.

One of the things that I can't quite shake is a feeling that there's a little too much going on with advance_to_first_block_item and/or some of the internal logic is leaking out a bit that has to be conditionally interpreted by consumers of the function.

For example, I can see 3 different branches internal to the function that can all result in function returning the exact same thing, an empty string, and the length of the returned string (or more precisely whether or not it's empty) is significant for at least some consumers.

I don't have any specific suggestions/modifications just yet (although maybe wrapping the return string in an Option could help?) and I wouldn't say this is a blocking concern for me; just wanted to share my thinking. Properly (and idempotently) maintaining line spacing is certainly a nontrivial task and you've done a great job here!

@ayazhafiz
Copy link
Contributor Author

a little too much going on with advance_to_first_block_item

I tried to condense the logic a bit, lmk what you think

@ayazhafiz
Copy link
Contributor Author

https://github.com/rust-lang/rustfmt/pull/4295/files broke this, so there is probably a better way to handle this using the code there...

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.

@ayazhafiz @calebcartwright Thank you forks! The PR LGTM, and additional thanks for writing more tests ❤️

ayazhafiz added 4 commits July 9, 2020 19:25
Adds the option for

- Functions
- Control flow blocks
- Expression statements (i.e. `let x = { ... };`)
- Traits
- Trait Impls
- Mods
- Foreign Mods

If I am missing a kind of item, let me know :)

Closes rust-lang#2868
@calebcartwright
Copy link
Member

Thanks for the changes @ayazhafiz! I know it was fairly minor but I definitely appreciate the updated advance_to_first_block_item 👍

@calebcartwright calebcartwright merged commit 4b7e90d into rust-lang:master Jul 14, 2020
@murchu27 murchu27 mentioned this pull request Jan 21, 2022
wafrelka pushed a commit to wafrelka/rustfmt that referenced this pull request May 31, 2022
…lang#4303)

* Configuration option to preserve blank lines at start of block

Adds the option for

- Functions
- Control flow blocks
- Expression statements (i.e. `let x = { ... };`)
- Traits
- Trait Impls
- Mods
- Foreign Mods

If I am missing a kind of item, let me know :)

Closes rust-lang#2868

* fixup! Configuration option to preserve blank lines at start of block

* Simplify `advance_to_first_block_item`

* fixup! Simplify `advance_to_first_block_item`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Configuration option to preserve blank lines at beginning of block
4 participants