How to avoid reordering when optimize imports
Asked Answered
S

2

9

Is there a way to prevents Intellij Idea to rearrange imports when it runs the optimize imports? I want to avoid this since I work on a large codebase where most of others programmers are using Eclipse. When Idea do the optimization (to remove unused imports, for example) it always put these imports to the bottom:

import javax.annotation.Resource;
import javax.ejb.EJB;
import javax.ejb.EJBContext;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.interceptor.Interceptors;
import java.util.ArrayList;
import java.util.List;

And when some eclipse users do the same, it are placed to the top. I want to avoid this, since it may annoy others programmers, since I'm the only one that uses Idea IDE in project.

I've googled about it and found old messages, like: https://youtrack.jetbrains.com/issue/IDEABKL-6456

I would like to know if there is any good way to do this in the newer Idea versions.

The version is: IntelliJ IDEA 2018.1 (Community)

  • EDIT - Some Infos:

The "Optimize imports on the fly" is already unchecked.

Smitty answered 16/4, 2018 at 13:45 Comment(4)
plugins.jetbrains.com/plugin/6546-eclipse-code-formatterIsomerize
I already use eclipse formatter, but cannot configure it to avoid messing with imports.Smitty
That is a critically important information! Report a bug, the plugin should handle it just fine - if you configure it right.Isomerize
It works now after I check "Manual configuration" in Eclipse Code Formatter config.Smitty
D
7

Go to preferences (settings) -> editor -> code style -> java -> imports tab

At the bottom you find the import layout. This will determine the structure if your imports. You should remove everything and just leave import all other imports.

Here is how it should look: enter image description here

Note: don't forget to uncheck Layout static imports separately

Note 2: I'm using 2018.1.2 EAP (Ultimate)

Deoxyribose answered 16/4, 2018 at 14:49 Comment(4)
unfortunately, this doesn't work for my. I think it is because I use eclipse formatter to format code.Smitty
Ahh too bad. Perhaps you don't really need the eclipse formatter? Have you tried to import the eclipse xml profile into your style settings? (best to save it as a separate profile). I'm not familiar with the plugin so doing a blind shot here. If you need help, let me know.Deoxyribose
I need it because Intellij doesn't format exactly like eclipse even if I import xml settings.Smitty
This answer seems plain wrong. These settings are for how things are grouped. "Optimize imports" still insists on re-ordering alphabetically within any group, just with these settings I now have one big group. On 2020.2 without Eclipse Formatter. Ridiculous not to have a simple way to remove unused while preserving order.Pissarro
B
0

For someone who has is still facing this issue even after disabling "optimize imports", It might be possible that you have persisted your settings at your project level.

Solution which worked for me - try deleting ".idea" folder (which has all your current project related settings) and do a "invalidate cache and restart" from "File" menu.

After your IDE is restarted, you will see the issue resolved.

Bouchard answered 3/12, 2021 at 12:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.