-
Notifications
You must be signed in to change notification settings - Fork 889
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
Add imports_granularity="Item". #4639
Add imports_granularity="Item". #4639
Conversation
5ca731a
to
22e437d
Compare
Thank you for the PR! A few important things:
|
b8b21ef
to
ba12968
Compare
ae2735e
to
47fc453
Compare
Done! Let me know what you think. I also filed #4641 after noticing some strange inconsistencies with |
src/formatting/reorder.rs
Outdated
} | ||
ImportGranularity::Preserve => normalized_items, | ||
}; | ||
for item in normalized_items.iter_mut() { |
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.
Should this transformation be part of merge_use_trees?
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.
(Although this transformation should be skipped altogether, IIUC)
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 moved the logic to merge_use_trees, and changed it to rewrite paths ending in ::self
into ::{self}
. This also allowed me to remove some logic from the start of UseTree::flatten
, which did the same thing but only for one-element lists.
Perhaps this should be part of UseTree::normalize
, and that should be called again post-merge? Also, I noticed it seems to remove final ::self
:
rustfmt/src/formatting/imports.rs
Line 430 in a97fd77
// Remove foo::{} or self without attributes. |
9e16646
to
93b4f7e
Compare
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.
Thanks for the updates! Looking good overall, though left some inline feedback to see if there's an opportunity to shuffle the logic around a bit
93b4f7e
to
ff07d6a
Compare
Something like this? The logic to make |
Excellent! I think this is a better approach 👍
Yeah, though I'm happy to accept that tradeoff all things considered. It's a tiny piece of code and the implementation for Item reads more clearly now IMO. |
Do you want to squash up the commits a bit? Otherwise I can just squash on merging |
9c9ad03
to
b333b5d
Compare
This option splits all imports into their own `use` statement.
b333b5d
to
3e936aa
Compare
Squashed and rebased onto master. Ready to go! |
Fantastic, and thanks again for this! A lot of folks seemed quite keen on having this style available so I anticipate there will be many ready to utilize this addition! |
…anxiyn update rustfmt to v1.4.34 Short summary: Various formatting fixes (several const generic related) and introduction of `imports_granularity` config option Long summary copied from changelog: #### Changed - `merge_imports` configuration has been deprecated in favor of the new `imports_granularity` option. Any existing usage of `merge_imports` will be automatically mapped to the corresponding value on `imports_granularity` with a warning message printed to encourage users to update their config files. #### Added - New `imports_granularity` option has been added which succeeds `merge_imports`. This new option supports several additional variants which allow users to merge imports at different levels (crate or module), and even flatten imports to have a single use statement per item. ([PR rust-lang/rustfmt#4634](rust-lang/rustfmt#4634), [PR rust-lang/rustfmt#4639](rust-lang/rustfmt#4639)) See the section on the configuration site for more information https://rust-lang.github.io/rustfmt/?version=v1.4.33&search=#imports_granularity #### Fixed - Fix erroneous removal of `const` keyword on const trait impl ([rust-lang/rustfmt#4084](rust-lang/rustfmt#4084)) - Fix incorrect span usage wit const generics in supertraits ([rust-lang/rustfmt#4204](rust-lang/rustfmt#4204)) - Use correct span for const generic params ([rust-lang/rustfmt#4263](rust-lang/rustfmt#4263)) - Correct span on const generics to include type bounds ([rust-lang/rustfmt#4310](rust-lang/rustfmt#4310)) - Idempotence issue on blocks containing only empty statements ([rust-lang/rustfmt#4627](rust-lang/rustfmt#4627) and [rust-lang#3868](rust-lang/rustfmt#3868)) - Fix issue with semicolon placement on required functions that have a trailing comment that ends in a line-style comment before the semicolon ([rust-lang/rustfmt#4646](rust-lang/rustfmt#4646)) - Avoid shared interned cfg_if symbol since rustfmt can re-initialize the rustc_ast globals on multiple inputs ([rust-lang/rustfmt#4656](rust-lang/rustfmt#4656)) - Don't insert trailing comma on (base-less) rest in struct literals within macros ([rust-lang/rustfmt#4675](rust-lang/rustfmt#4675))
Backported in #4674 |
This option splits all imports into their own
use
statement.