-
Notifications
You must be signed in to change notification settings - Fork 888
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
Implement One
option for imports_granularity
#4680
Implement One
option for imports_granularity
#4680
Conversation
Thank you for the PR @magurotuna! As you may have noticed the PR queue is a bit backed up at the moment as I've been preoccupied with some maintenance work and trying to get rustfmt back and available on nightly again. Should have bandwidth to get through some of these PRs in the near future though! |
@calebcartwright Alright, no rush :) |
src/formatting/imports.rs
Outdated
@@ -560,6 +574,7 @@ impl UseTree { | |||
SharedPrefix::Module => { | |||
self.path[..self.path.len() - 1] == other.path[..other.path.len() - 1] | |||
} | |||
SharedPrefix::One => unreachable!(), |
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.
Did you try making this branch return true
? It seems like that could mean you don't need temp_merge_by
.
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.
Yes, I tried that, but I found that ended up requiring additional special logic to address SharedPrefix::One
cases.
For example, consider the following input:
use b;
use a::ac::{aca, acb};
use a::{aa::*, ab};
If we changed SharedPrefix::One => unreachable!(),
to SharedPrefix::One => true,
with the rest code reverted to master
, the above code would be formatted into:
use {
a::{
ab,
ac::acb,
{aa::*, ac::aca},
},
b,
};
Here, ac::acb
and ac::aca
should be put together, but actually not.
After bumping into this issue, I thought it would be a good and simple solution to leverage the same logic as the SharedPrefix::Crate
and then to merge the result.
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 @msmorgan for reviewing and @magurotuna for explaining the test case. I'm not sure about the implementation to deal with those types of cases though. I suspect the unreachable
arm and temp/actual vars will be puzzling to both casual and future browsers of the code.
I will take a closer look at the proposed changes later on, but I think it will be preferable if we can come up with an approach that avoids those
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.
Okay, I will consider more as well.
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.
@calebcartwright
I attempted to fix the code to avoid unreachable!
arm. I think this implementation is kind of naive but seems to work fine. To ensure it works, I added more test cases.
Could you take a look again?
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 this implementation is kind of naive but seems to work fine
As in, you think your original approach was better than this, or you think there's a better approach than this updated implementation?
To ensure it works, I added more test cases
Brilliant thank you! If you don't already have these, I'd advising having a few cases that exercise some self
and comment scenarios
Could you take a look again?
Yes indeed. I've not had a ton of bandwidth to allocate to PR reviews lately though so may be a while yet.
Also, don't necessarily feel like you have to shoehorn the One strategy into the existing flow that's leveraged for other variants, especially if doing so is more cumbersome than helpful.
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.
As in, you think your original approach was better than this, or you think there's a better approach than this updated implementation?
Actually, on second thought, now I think the current approach is reasonably good. Although there are a couple of ad-hoc if-expressions, which seemed a little bit ugly to me, it apparently works just fine. Implementing imports_granularity essentially involves complexity so I feel like working fine is more important than anything else.
If you don't already have these, I'd advising having a few cases that exercise some self and comment scenarios
Those definitely should be added! I've added more tests that cover them.
e1dd5fa
to
c165107
Compare
Thanks again for the updates @magurotuna! I've done a quick pass through the updated implementation and agree that this could work. Would you do me a favor though and rebase on the latest from |
3b92ef9
to
04c5a99
Compare
@calebcartwright |
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 again! One minor request left inline below, but otherwise should be good to go for the initial rollout.
Not strictly related to this PR but every time I review changes to the imports handling code I am reminded of how great it would be to one day be able to rid of all these clones 😞
src/formatting/imports.rs
Outdated
let similarity = tree | ||
.path | ||
.iter() | ||
.zip(&use_tree.path) | ||
.take_while(|(a, b)| a.equal_except_alias(b)) | ||
.count(); |
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 should be able to skip the derivation of this value when merge_by is not the SharedPrefix::One variant, correct? If so, then would be good to variant gate this and default to 0 for the others. However, if not then let's add some test cases with the Module and/or Crate variants that demonstrates the necessity.
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 that's right! Only SharedPrefix::One
requires similarity
. I fixed the code so computing similarity
will be done in the case of SharedPrefix::One
only.
I am reminded of how great it would be to one day be able to rid of all these clones 😞
True... :-|
967cff2
to
22c5926
Compare
This option merges all imports into a single `use` statement as long as they have the same visibility.
Brilliant thank you! |
I was looking for a config option that merges all imports. |
Hm no, it's not working:
When will it be available? :) I have
|
@Boscop looks like it was merged into 2.0 experimental branch, not into current master 😕 |
Backport was done in #4973 |
Believe this is avail as of |
This implements a new option
One
forimports_granularity
, which merges all imports into a singleuse
statement as long as they have the same visibility.Resolves #4669