Tips & Tricks

Something more about completion

Along with basic code completion already described on this blog, IntelliJ IDEA provides so-called “smart completion” that can be considered as a separate feature. Sometimes users call it “super” or even “secret” completion. “Super” is because this feature is far more intelligent than simple completion. “Secret” comes from the fact that this completion is invoked not by the usual Ctrl + Space shortcut, but by Ctrl + Shift + Space.
Why have two different completions? Well, how often have you found yourself saying “This thing thinks that it’s smarter than me!” and try to make the product do what you want it to do? IntelliJ IDEA doesn’t want to be smarter than you are. But its intelligence is always at your disposal when you need it.
So, what is so special about smart completion? First of all, it filters the list of suggestions according to the expected expression type.
For example, it can suggest values at right-hand side of assignments, in the return statements, in method calls, and anywhere you need to.

In obvious cases where you don’t need to see a list of choices, smart completion will simply add the necessary value. For example, use Ctrl + Shift + Space after new when you create an instance of a class.

IntelliJ IDEA adds the constructor, brackets, and the semicolon. It also positions the caret right within the brackets. And all you need to do is press Ctrl + Shift + Space again to see the list of possible parameter values.

In case of abstract classes and interfaces, you can choose one of the inheritors.

Another example when you don’t need a list of choices is type casting. For example, you have typed something like str = (, where str is some variable of String type. To cast the right-hand side of assignment to String, just press Ctrl + Shift + Space. The result, of course, will be str = (String).
But, probably, the most impressive use case for the Smart completion is when you create an anonymous implementation of some interface. For example, you had the following code:

When you press Ctrl + Shift + Space and select the MouseListener interface, you get the following result:

Just think about the time the smart completion saves.

Note   This tip&trick was originally posted at www.javalobby.org.

Technorati tags: IntelliJ, IDE, Java
image description

Discover more