Need to run "organize imports" twice if you are missing imports
If you have a class with unused imports as well as missing imports, the first time you run "organize imports" (ctrl shift o) it will only add missing imports. You need to run it a second time to remove any unused imports. For example:
java.util.ArrayList is imported and used, java.util.Random is imported but not used, and java.util.EmptyStackException is not imported but is needed. The first time you run ctrl shift o it will add in an import for EmptyStackException, but it will not remove java.util.Random. If you run it again, then it will remove java.util.Random
I assume this is because the class does not compile until EmptyStackException is imported, so I'm not sure how feasible this is to do.
import java.util.ArrayList
import java.util.Random
class Test {
val a = new ArrayList[String]
val b = new EmptyStackException
}
java.util.ArrayList is imported and used, java.util.Random is imported but not used, and java.util.EmptyStackException is not imported but is needed. The first time you run ctrl shift o it will add in an import for EmptyStackException, but it will not remove java.util.Random. If you run it again, then it will remove java.util.Random
I assume this is because the class does not compile until EmptyStackException is imported, so I'm not sure how feasible this is to do.
Leave a comment
Your assumption is correct. If the compilation unit doesn't compile, then it's not possible to say if an import is unused. However, it looks like we could simply spare you from having to run "organize imports" the second time (if the compilation unit compiles fine), and do it for you programmatically.
on 2015-03-13 22:15 *
By Simon Schäfer
Version changed from 3.0.0-RC2-210 to 4.0.0
Milestone changed from Enhancements to -none-
on 2016-02-05 14:52 *
By wpopielarski769697
is it still life issue?
looks that we would need:
1. add missing imports
2. compile unit
3a IF succeeded THEN Organize Imports
3b ELSE skip it
looks that we would need:
1. add missing imports
2. compile unit
3a IF succeeded THEN Organize Imports
3b ELSE skip it
on 2016-02-07 21:05 *
By Simon Schäfer
Your list looks good to me.
on 2016-02-29 14:55 *
By Simon Schäfer
Did you work on an implementation?
on 2016-02-29 15:03 *
By wpopielarski769697
not now, I just looked at this and noticed that there are two separate actions actually. one for just adding missing imports and the second to organize them. needs to be somehow combined together but only in case when there is no compilation errors.
Move ticket to next milestone.