IDEA 10.5 Auto-Imports Wrong Class

I use the or.apache.commons.lang.StringUtils class a lot for String validation. However, if I type "StringUtils" quickly, IDEA auto-selects the org.apache.shared_tomahawk.util.StringUtils, which happens to be the top one in a list of about 6 StringUtils classes visible to the class I'm working on.

I don't recall this happening with earlier versions of IDEA.

Correcting this is a pain as there's no "undo" for the completion (as far as I know). I have to scroll up to the imports section and delete the im port. To avoid this, I've found myself commenting out the line (ctrl-/), optimizing imports for the file (ctrl-alt-o), then uncommenting the line to pick the imported class.

A) Is there anything I can do to cause IDEA not to picking the top entry in the list of possible imports?  I searched the settings for anthing import related, but didn't see a setting to do this.
B) Once the import has been added, is there a way to undo it?

Thanks.

jack

0
8 comments
Avatar
Permanently deleted user

Hi,

if you do not need to import the other class you can exclude packages/classes from the Autoimport.
Goto


Settings > Editor > Auto Import





There is a list where you can add packages and classes that should not be inluded during import.
However, this setting will exclude the matching classes from autoimport and code completion, so for classes that you do want to use in rare cases, this might not be the perfect solution.

By the way: have you used the search input field in the settings dialog (above the tree on the left hand side) or have you manually searched the settings ?
I always find the search field very convenient and searching for "import exclude" gives me just one setting which is the one from above ;)

-Christian
0

Is the class you want to import located in your project? Is it called exactly how you type it, case-sensitive? If you don't press Space after typing StringUtils, you'll see a completion autopopup. In which order are the classes presented there?

BTW undoing should work fine. Perhaps it'll also undo space insertion, but the main thing is that the import will be removed as well.

0

There are 6 classes named StringUtils that are visible from the class I'm editing. The issue is, as I'm typing, I'm not given the chance to pick the class that's imported - IDEA's doing it for me. Unfortunately, the class it's picking is the wrong one.

It's probably easier to demonstrate than explain, so I've attached an 11 second AVI illustrating the problem.

jack



Attachment(s):
Idea10_5ImportIssue.avi
0

It actually lets you choose the correct class from same-named alternatives, but before you press dot. Dot just acts as accepting completion variant (and inserting all the necessary imports), and it's quite handy in most cases.

0

Peter,

I agree that IDEA presenting me a choice of imports before I press the dot is a benefit. However, I hope we can all agree that it can never be considered a benefit for IDEA to automatically pick the wrong choice.

In the simple example I showed in the AVI, it's pretty obvious that StringUtils has never been used. So pausing "StringUtils" is typed, but before the period is typed may appear to make sense. However, consider someone typing any foreign class name inside one of a dozen methods inside inside a production class of 100s of lines.

The developer cannot, and should not have to, remember that the class being used has already been imported. Requiring a developer to slow down and pause before typing every period following the class name just to let IDEA prompt for an import selection simply cannot be considered a feature. IDEA has never been an IDE that has asked me to slow down while developing.

I'd much prefer nothing be done when typing a class name for a class that has not been imported. Let me type my full statement at full speed, "StringUtils.isNotBlank(foo)" then flag the class name as needing to be imported. If I do pause (~300-500ms) before typing a period, give me a list of imports to pick from. If I t pick one, great. Otherwise, never, never, pick one for me.

I'd say this is true even if there's only one import possible. Maybe I plan to add a JAR to the build path after I code this method? Only after the JAR is added is the desired class name available. What good is it going to do me for IDEA to automatically add the wrong import just so my code now compiles, but doesn't work as I planned?  Even if only one import was possible at the time a class name was typed, it's better to have me manually review a compile problem with a class name than automatically making the wrong choice and tricking me into assuming all is well.

In short, when considering any automatic operation, an IDE should first "do no harm." Automatically adding the wrong class does harm that a developer must remember to manually undo. This behavior can never be considered a feature.

I've marked my question as answered. I've shown exactly how this happens and the bad result that occurs. I hope I'm not alone in considering this a problem.

jack

PS. to this story. This behavior really bit me this week. As described in this thread, IDEA automatically added  an incorrect import (a Tomcat-related StringUtils). In a hurry, I added  the correct import statement, but didn't optimize imports.  My code  compiled. My tests ran. I committed the code. It ran on other  development boxes. However, when the build was being done for deployment to the non-Tomcat production server, the deployment the build failed  because the production-server build doesn't have the development-build Tomcat JARs. It was my mistake not to optimize imports before committing. It was IDEAs mistake to add an import I never wanted. I really don't want to add additional ceremony to my coding to clean up IDEAs mistakes.

0

You can set the delay after which autopopup appears, in Settings | Editor | Code Completion. By default it's 0, so it appears as soon as it's ready and it may be quite unexpectedly right before you type dot. But if you increase the delay to, say, 200 ms, this may solve your problem.

0
Avatar
Permanently deleted user

Setting preselect first suggestion to never works better in my experience.

0

Mark & Peter,

I've included them both to hopefully prevent the incorrect import from being automatically selected.

Thank you both for your suggestions!

jack

0

Please sign in to leave a comment.