Skip to content
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

feat: Add "One" import granularity #16372

Merged
merged 4 commits into from Jan 18, 2024

Conversation

davidsemakula
Copy link
Contributor

Adds a new import granularity option "One" that merges all imports into a single use statement as long as they have the same visibility and attributes.

This is similar to rustfmt's imports_granularity = "One".

Fixes: #11361

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jan 16, 2024
@bors
Copy link
Collaborator

bors commented Jan 17, 2024

☔ The latest upstream changes (presumably #16389) made this pull request unmergeable. Please resolve the merge conflicts.

Comment on lines 43 to 45
let mut edits = tree.clone().try_merge_from(&mut neighbor, &ctx.config.insert_use);

if edits.is_none() && ctx.config.insert_use.granularity == ImportGranularity::One {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
let mut edits = tree.clone().try_merge_from(&mut neighbor, &ctx.config.insert_use);
if edits.is_none() && ctx.config.insert_use.granularity == ImportGranularity::One {
if ctx.config.insert_use.granularity != ImportGranularity::One {
tree.clone().try_merge_from(&mut neighbor, &ctx.config.insert_use)
else {

This more reasonable to me, since then we don't do work twice.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Making this change would break this test (and similar ones for ImportGranularity::One - the test case just above it is the motivation for having this behavior).

But breaking this behavior (at least for ImportGranularity::One only for now) may not be a bad thing because the assist is supposed to "Merges two imports with a common prefix". So may be it's better to provide the functionality equivalent to the test case behavior with a new assist (in a separate PR - something like "normalize import")?

If the above make sense, then I can simplify this implementation even further, while maintaining existing behavior for other import granularities. LMK 🙂

Copy link
Member

Choose a reason for hiding this comment

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

I see, then that's probably fine. Could you add a https://docs.rs/cov-mark/latest/cov_mark/ here for one of the corresponding tests? That makes this behavior a bit more clear

@Veykril Veykril added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jan 18, 2024
@davidsemakula
Copy link
Contributor Author

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jan 18, 2024
@Veykril
Copy link
Member

Veykril commented Jan 18, 2024

Thanks!
@bors r+

@bors
Copy link
Collaborator

bors commented Jan 18, 2024

📌 Commit 67c1c2b has been approved by Veykril

It is now in the queue for this repository.

@bors
Copy link
Collaborator

bors commented Jan 18, 2024

⌛ Testing commit 67c1c2b with merge 3f4c6da...

@bors
Copy link
Collaborator

bors commented Jan 18, 2024

☀️ Test successful - checks-actions
Approved by: Veykril
Pushing 3f4c6da to master...

@bors bors merged commit 3f4c6da into rust-lang:master Jan 18, 2024
10 checks passed
@davidsemakula davidsemakula deleted the import-granularity-one branch January 18, 2024 15:53
bors added a commit that referenced this pull request Jan 30, 2024
feat: "Normalize import" assist and utilities for normalizing use trees

- Add import/use tree normalization utilities
- Add "normalize import" assist
- Update "merge imports" assist to always apply to the covering use item except for nested use tree selections
- Update "merge imports" assist to avoid adding unnecessary braces when merging nested use tree selections

See [this discussion](#16372 (comment)) for the motivation for the new "normalize import" assist and changes to the "merge imports" assist.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feat: "One" import granularity
4 participants