Search code examples
javaintellij-ideaiderefactoring

Optimize imports as part of refactoring in IntelliJ IDEA


I'm planning to move a widely used class from the non-standard package mycompany.etc to the more standard com.mycompany.etc. IDEA finds and updates references nicely, but when doing so, it updates import statements in-place without moving them according to the import order. The result is that they end up wildly out of place between all the remaining non-standard imports starting with mycompany.

Is there a way to instruct IDEA to move the updated import statement, or better yet, automatically perform an 'optimize imports' on each file where imports were updated during refactoring?


Solution

  • Given the fact that 8+ years later, as of 2024.2.4, a fully in-IDE solution still doesn't seem to exist, it's perhaps worth mentioning that I ended up writing a git hook that fixes the import order only in files that were changed or added in a particular commit.