-
Notifications
You must be signed in to change notification settings - Fork 1.8k
organize imports action #5131
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Comments
cc #3301 |
Actually, there are several operations I expect for "organize import" feature:
|
It would also be nice if local imports could be canonicalized, so that it consistently uses relative or absolute paths (or has a rule for determining which to use). |
I would follow the approach intellij uses, with imports grouped by standard library, external, and local, and alphabetized within each group. |
I like the suggestions here (merging and sorting), but removal of unused imports is by far the what I'm missing the most. |
Is there any ongoing work on this issue? |
There is already diagnostic info and quick fix for unused imports, what is stopping this from happening? |
A person willing to implement this (also not r-a itself does not yet diagnose unused imports, that's coming from the cargo checks) |
Clippy can remove unused imports:
There should be a way to bind this to a keyboard shortcut. Some ideas: https://stackoverflow.com/questions/52786022/shortcut-for-running-terminal-command-in-vs-code Caveats:
To deal with the last issue, my typical approach is to stage all my changes, so they are isolated from any additional changes. Then I run It would be ideal if rust-analyzer could clean up unused imports, but at least we have an alternative for now. |
I am aware of cargo/clippy fix, but I don't want it to potentially nuke anything, but I couldn't figure out how to only fix a certain thing(unused imports), binding commands and stuff is the easiest part because... (neo)vim, lol. |
Yeah, it feels like this should be doable. This is going to be a pretty large wad of code to add, but it seems to me that, at this point, we probably a reasonably precise analysis to make this worthwhile. Some general pointers:
I think this feature can work in one of two ways:
Abstractly, I like the second approach much more, as it allows us to be significantly more lazy. For all unused imports, we can probably say that they are unused following the parse, and for used stuff it's enough to have just one confirmed used to rule out a false negative. What makes me hesitant to try the second approach are macros. To textually find all uses, we have to expand the macros... But this is something our search infra has to deal with anyway. So, yeah, my prior here is that building the feature on top of search is the way to go. |
Ah, I realized that we already have a model of this workflow in the code, that's "remove unused param" assist: So I think import optimization should have roughly the same skeleton, with just a tiny bit more meat on it :D |
I want to give this one a shot. If it turns out to be beyond me or I don't have the time I will give an update. |
MR up! |
This comment was marked as spam.
This comment was marked as spam.
Added remove unused imports assist This resolves the most important part of #5131. I needed to make a couple of cosmetic changes to the search infrastructure to do this. A few open questions: * Should imports that don't resolve to anything be considered unused? I figured probably not, but it would be a trivial change to make if we want it. * Is there a cleaner way to make the edits to the use list? * Is there a cleaner way to get the list of uses that intersect the current selection? * Is the performance acceptable? When testing this on itself, it takes a good couple seconds to perform the assist. * Is there a way to hide the rustc diagnostics that overlap with this functionality?
I understand that the action is not fully completed yet. But is there a way to remove (all) unused imports at this point already (in VS Code) from any cursor position in the code and without scrolling? The least disruptive solution I found so far is to open the problems view and to apply the assist on each problem. |
any update on this? I'm so used to doing |
@nervenes Could you clarify what you mean by "this is already happening when you format the document". I'm pressing Is there a config in vscode or |
How can I make “Remove all the unused imports” happen when running “Format Document”, so that I can also run it on save? Is there any option to enable it on format? |
Uh oh!
There was an error while loading. Please reload this page.
It'll be good to have this feature so that we can remove unused imports
EDIT:
instructions
The text was updated successfully, but these errors were encountered: