Monday, August 2, 2010

Things I Love About IntelliJ IDEA: Smart Auto-Completion

Have you ever noticed that when you try to autocomplete something in Eclipse, it shows you half of the code in your classpath? It'll show you classes - both in your package and not - methods and fields - regardless of whether their use would create compilation errors - and templates. Autocompletion in IntelliJ IDEA is made much more useful by dividing the 'feature' of Autocompletion into a set of more specific features. One the one hand, you need to learn a couple of extra key combinations. The advantage, though, is that more specific autocompletion means less options, which means quicker selection. In fact, in many cases, there will only be one valid option, and when this happens IDEA will just complete without popping anything up. (I think Eclipse might do this too, but its "autocomplete as anything" philosophy makes it more rare that there is only one option.)

Here's the different completion options in IDEA and when you should use them:

Ctrl-Shift-Space: Smart (Type-Aware) Completion
This is probably my favourite feature of IntelliJ IDEA. Whenever you are auto-completing at a location that has an expected type - for instance, on the RHS of an assignment or inside the parentheses of a method invocation - type-aware completion will greatly reduce the number of options that appear because it only displays expressions (variables, fields or methods) that match the expected type. This is the key combo that most often feels like magic to me. Sometimes I can just hit Ctrl-Shift-Space four or five times in a row and watch the code that was in my head appear on the screen. And the JetBrains guys are continuing to make this do even more magic: See 'Chained Java Code Completion' and 'Super Completion' at

Ctrl-Alt-Space: Out-of-Scope Class Name Completion
It's actually fairly rare that you'll want to import a new class or interface into a Java source file. Most of the time you auto-complete a class name, it's already in scope, either because it's in the same package, you've already imported it, or its in java.lang. That makes it a bit of overkill to show a list of every class in your classpath every time you try to autocomplete. So Ctrl-Space doesn't do that. But you do obviously need to bring in out of scope classes every now and then, so if you want to complete a class that you haven't already used, hit Ctrl-Alt-Space to see all matching class names. The rest of the time, just use Ctrl-Space to see the classes that you're already using. IntelliJ (like Eclipse) also supports initialised completion. So if you want to use the IllegalArgumentException class, type 'IAE' and hit Ctrl-Alt-Space to see a list of all classes whose first three capitalised words being with 'I', 'A' and 'E'.

Tab: Template Completion
If you know the short name for your template, e.g. "iter", just type it, then tab, voila. If you need to browse for a template, or you only know the first letter or two of the template (e.g. "it"), type what you know and hit Ctrl-J (on Windows and Linux, Command-J on Mac) to see a list of options.

Ctrl-Space: Everything Else
This is the one you use for everything else:
* class names that are already in scope
* methods or fields that don't match the expected type
* completing names when there is no expected type (e.g. invoking a void method)
* completing the declaration of names for variables, fields or parameters

It might seem daunting at first to have four autocomplete options rather than one. However, by giving the IDE a small hint about what kind of thing you're trying to complete, and with the IDE being a bit smarter in assessing what kinds of things might possibly compile if they were completed, the list of options that you are shown is shorter and less confusing than chucking everything in one jolly big list.

Below are screenshots of the different options you will get if you press Ctrl-Space in both Eclipse and IntelliJ IDEA for the same piece of code. Aside from the fact that Eclipse suggested every class beginning with 'B', note that IDEA doesn't suggest blogEntry, boolean or byte, because it knows these wouldn't compile. The really good news is that, if you hit Ctrl-Shift-Space in IDEA here instead of Ctrl-Space, it will just insert blogRoot; complete with semi-colon. Actually, Ctrl-Shift-Space would insert this code even without typing the 'b'.

1 comment:

  1. One of the things I also love about IDEA is that it often understands what you meant even if it's not what you said. I.e. it can give you meaningful suggestions even when your code isn't syntactically correct.

    Just now I incorrectly created a class and had it extend instead of implement an interface. Control-i (implement methods) still worked fine and picked up the methods from the interface.