rust-analyzer icon indicating copy to clipboard operation
rust-analyzer copied to clipboard

organize imports action

Open 0x8f701 opened this issue 5 years ago • 18 comments

It'll be good to have this feature so that we can remove unused imports

EDIT:

instructions

0x8f701 avatar Jun 29 '20 21:06 0x8f701

cc https://github.com/rust-analyzer/rust-analyzer/issues/3301

matklad avatar Jul 11 '20 10:07 matklad

Actually, there are several operations I expect for "organize import" feature:

  • Remove all redundant imports, and remove curly brackets if unneeded
  • Merge all imports as much as possible, or split all imports, one identifier for one line
  • Sort (and maybe insert appropriate blank lines between groups) the imports (the first of which can be already done by cargo fmt)

TonalidadeHidrica avatar Mar 31 '21 13:03 TonalidadeHidrica

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).

branpk avatar Mar 31 '21 14:03 branpk

I would follow the approach intellij uses, with imports grouped by standard library, external, and local, and alphabetized within each group.

dnut avatar Jun 18 '21 20:06 dnut

I like the suggestions here (merging and sorting), but removal of unused imports is by far the what I'm missing the most.

aloucks avatar Jan 16 '22 15:01 aloucks

Is there any ongoing work on this issue?

ocheret avatar Apr 02 '23 16:04 ocheret

There is already diagnostic info and quick fix for unused imports, what is stopping this from happening?

Ciel-MC avatar Apr 27 '23 14:04 Ciel-MC

A person willing to implement this (also not r-a itself does not yet diagnose unused imports, that's coming from the cargo checks)

Veykril avatar Apr 27 '23 15:04 Veykril

Clippy can remove unused imports:

cargo clippy --fix

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:

  • applies to the entire workspace
  • changes a bunch of things that rust-analyzer does not change
  • does not work if you have local changes that have not been committed. The clippy maintainers seem to think this command might break your code, so use it carefully.

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 cargo clippy --fix --allow-staged. Then I review the unstaged changes from clippy. If it looks bad, I can revert them in isolation.

It would be ideal if rust-analyzer could clean up unused imports, but at least we have an alternative for now.

dnut avatar Apr 27 '23 15:04 dnut

Clippy can remove unused imports:

cargo clippy --fix

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:

* applies to the entire workspace

* changes a bunch of things that rust-analyzer does not change

* does not work if you have local changes that have not been committed.

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 cargo clippy --fix --allow-staged. Then I review the unstaged changes from clippy. If it looks bad, I can revert them in isolation.

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.

Ciel-MC avatar Apr 27 '23 15:04 Ciel-MC

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:

  • this probably wants to be an assist, there's a doc comment about what it is here
  • auto import might be a good read to learn about related machinery
  • Similarly, usage search is probably something we'll use here.

I think this feature can work in one of two ways:

  • In approach one, we record which imports do we use during analysis. So, eg, Body would get a field with the used imports, which is populated during lowering. import optimization than looks at all file's bodies (and whatever else can use imports), and computes the set of used things.
  • Alternative approach would be to work from search. If you have use foo::Bar, and Bar is a struct, and the file textually does not include the string Bar, then we can conclude that Bar is unused. If the Bar is a trait, then we should also look for mentions of trait's items.

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.

matklad avatar Apr 28 '23 14:04 matklad

Ah, I realized that we already have a model of this workflow in the code, that's "remove unused param" assist:

https://github.com/rust-lang/rust-analyzer/blob/62c81d62932c458e23975191ecc124916be0b35e/crates/ide-assists/src/handlers/remove_unused_param.rs

So I think import optimization should have roughly the same skeleton, with just a tiny bit more meat on it :D

matklad avatar Apr 28 '23 14:04 matklad

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.

obsgolem avatar Apr 28 '23 16:04 obsgolem

MR up!

obsgolem avatar May 03 '23 03:05 obsgolem

when?

lem0nify avatar Jun 28 '23 00:06 lem0nify

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.

igorakkerman avatar Jan 14 '24 09:01 igorakkerman

CleanShot 2024-06-04 at 08 54 10

TIL you can half-way get this functionality. You just need to select the entire imports and then do + . and select remove unused imports

ospfranco avatar Jun 04 '24 06:06 ospfranco

any update on this? I'm so used to doing cmd + shift + o (which is the shortcut to the vscode "Organize Imports" command) to organize my imports in other languages like python, in rust it seems like this is already happening when you format the document but ideally I'd like to have an option to separate these actions.

ghost avatar Jun 19 '24 16:06 ghost

@nervenes Could you clarify what you mean by "this is already happening when you format the document". I'm pressing ctrl+shift+I and still have unused imports. Yes, selecting the imports and pressing ctrl+. brings up the option, but similar to @igorakkerman 's request I want to be able to do this without scrolling to the top each and every time.

Is there a config in vscode or Cargo.toml that you need to enable to get this to work? Many thanks.

nmay231 avatar Dec 23 '24 05:12 nmay231

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?

yuhr avatar Mar 30 '25 18:03 yuhr

Bump, what is holding this up from being implemented? What's still needs to be done?

liamwh avatar Jun 30 '25 13:06 liamwh

Yeah, what's the state of this? I wanted to try out the different values for the rust-analyzer.imports.granularity.group setting but I cannot find any way to actually apply it... what am I missing?!

ThiefMaster avatar Sep 25 '25 10:09 ThiefMaster

Yeah, what's the state of this? I wanted to try out the different values for the rust-analyzer.imports.granularity.group setting but I cannot find any way to actually apply it... what am I missing?!

That option works for auto imports, like when you are auto-completing Mutex and rust-analyzer automatically inserting use std::sync::Mutex. I think that option would be applied to this feature but we don't have this yet. For state of this, well, I guess there's no major blockers for this but no one has worked on actually implementing this so far?

ShoyuVanilla avatar Sep 25 '25 11:09 ShoyuVanilla

I'm using Zed (big recommendation) and it has a "Remove all unused imports" code action that works perfectly well.

chrs-b avatar Sep 25 '25 11:09 chrs-b