Disable 'Optimize Imports' partially

Answered

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;

public class MainClass {
  public static String foo(String s) {
    Deque<Character> st = new ArrayDeque<>();
    List<Integer> loop = new ArrayList<>();
    List<Integer> loop2 = new LinkedList<>(); <== Add
    ...
  }
}

-------------------------------------------------------------------------------

import java.util.*;

public class MainClass {
  public static String foo(String s) {
    Deque<Character> st = new ArrayDeque<>();
    List<Integer> loop = new ArrayList<>();
    List<Integer> loop2 = new LinkedList<>();
    ...
  }
}

 

IntelliJ optimizes import statements of same package to '*'. Is there any way to only disable this?

I think auto import/clear is great but this option works in situations I don't intend.

0
9 comments

Hello,

Do you mean that you need to keep the first state with four import statements? You could increase "Class count to use import with *" value in "File | Settings | Editor | Code Style | Java - Imports".

0
Avatar
Permanently deleted user

Thank you. Now it works as I intented.

But I have one more question. I set Insert imports on paste to All but IntelliJ always ask me if I want to insert imports when I paste the code.

How to change this configuration correctly?

0

I couldn't reproduce the issue on my machine. Do you face it with every project? Could you please share idea.log ("Help | Show Log in...") after restarting IDE?

0
Avatar
Permanently deleted user

I don't know if it's relevant, the issue has resolved after checking Add unambiguous imports on the fly option.

0

It works fine without this option on my machine. Could you please provide sample project example? Maybe it depends on project configuration?

0
Avatar
Permanently deleted user

https://drive.google.com/open?id=1ALiCphp_O2I-2xXjwrjORYJ_jYnhHCYG 

Can you check any issues in my idea.log file?

0

No errors in log. So it should be fine. You could also try it with 2018.3.3 RC: https://confluence.jetbrains.com/display/IDEADEV/IDEA+2018.3+EAP

0

Found from stackoverflow (worked for me):

"Use single class import" does not prevent wildcard imports. The solution is to go to Preferences ( + , on macOS / Ctrl + Alt + S on Windows and Linux) > Editor > Code Style > Java > Imports tab set Class count to use import with '*' and Names count to use static import with '*' to a higher value. Any value over 99 seems to work fine.

0

Hello, baum. Could you please specify which IDE version you use? I tried to reproduce the case in IntelliJIDEA 2023.1.3, but the IDE follows the Class count to use import with * option. 

0

Please sign in to leave a comment.