Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent IntelliJ IDEA from deleting unused packages during Rename refactoring?

In Intellij IDEA, when I rename a method using Rename refactoring tool (which essentially replaces occurences of method's old name for its new one), IDEA also removes unused imports from files, where method name is replaced.

This is not what I want to do. And even if I want to remove this redundant imports, I would like to do it in the whole project in a separate action, so it could be a separate commit.

How can I disable this behavior?

The version I use is 14.1.3 Ultimate.

There is already similar question How to prevent IntelliJ IDEA from deleting unused packages?, but the option mentioned there (Optimize imports on the fly) has nothing to do with my problem. Actually I have it unchecked.

Update: It seems for me now that the only solution is to delete all unused imports before applying other refactoring tools.

like image 493
Mikhail Batcer Avatar asked Aug 18 '15 09:08

Mikhail Batcer


People also ask

How do I fix an unused import in IntelliJ?

Choose the project/module you want to remove unused import from in Project view. Then from Code menu choose Optimize imports and confirm with Run. The imports will be cleaned in whole project/module. Save this answer.

How do I refactor a package name in IntelliJ?

In the Project tool window, click Project and select Packages from the list. IntelliJ IDEA lists all the packages in your project. Right-click the package you want to rename and from the context menu, select Refactor | Rename ( Shift+F6 ).

How do I disable wildcard import in IntelliJ?

Disable wildcard importsIn the Settings/Preferences dialog ( Ctrl+Alt+S ), select Editor | Code Style | Java | Imports. Make sure that the Use single class import option is enabled.

How do I automatically delete unused imports?

To automatically remove unused imports, we will need to add the eslint-plugin-unused-imports plugin. Now, when you run ESLint, you should see error lines saying error '<imported-var>' is defined but never used unused-imports/no-unused-imports for the files where you have unused imports.


1 Answers

I've asked this from IntelliJ support, and the response referred me to this issue in their issue tracking.

Basically, it's been a bug/feature since 2005. There has been discussion back and forth about if the behaviour should be changed, but as it stands, there hasn't been any changes and there is nothing you as a user can do about it.

like image 99
eis Avatar answered Nov 05 '22 13:11

eis