Top 20 Features of Code Completion in IntelliJ IDEA
In this article I would like to highlight the top features of code completion in IntelliJ IDEA, which make coding for Java incredibly productive. Everyone agrees that completion is a number one feature for any IDE. It predicts what you are trying to type and completes it for you, even if you don’t know exactly the name of particular class, member or any other resource.
The secret, why code completion in IntelliJ IDEA is so profound, is that IntelliJ IDEA really understands your code, as well as the context you stay in. Here I will make a short overview of the most important features, which make IntelliJ IDEA unique among other Java IDEs.
The first one and probably the most impressive feature I would like to highlight is instant completion. Unlike other IDEs, IntelliJ IDEA provides it everywhere, not only when you are trying to access a member of a class. Just type a first letter of a word, and IntelliJ IDEA will instantly start suggesting the most relevant options, which would be appropriate for current place. This means you don’t need to call completion explicitly.
To get the idea, how efficient Instant completion is, watch demo.
Another feature, which also breaks down common code completion approach, is so called Smart completion. You might already know, that there are more than one type of completion exist in IntelliJ IDEA. These are Basic completion ⌘Space and Smart completion ⌘⇧Space.
When you call Smart completion, it filters suggestion list and shows only classes, variables, fields and methods of the type, expected in the context. This improves performance very much and keeps you out of possible mistakes. After you try it, promise, you will miss it all the time.
Static fields and methods
Just as I said, IntelliJ IDEA always tries to be a step ahead of you. If you don’t remember class name with static member, which you need, just start typing the name of the member and call completion twice. It will provide correct completion, and even offer to import the member statically, if you call Intention action ⌥⏎.
Once again, if you use Smart completion, it will offer only members of expected type.
Next feature, which makes you even more productive, is Chain completion. If you need to type a chain like getModule().getProject(), just call Smart completion twice. In that case IntelliJ IDEA will scan variables, fields and methods, visible from the context, and offer you a chain, which satisfies expected type.
As you might know, recently there was a plugin for Eclipse Juno, called Code Recommenders. This is quite interesting plugin, which also claims to provide a kind of chain completion. However, it works in a different way. While Code Recommenders suggests only chains of Eclipse SDK APIs classes, IntelliJ IDEA provides chains of any classes from your code.
Data flow analysis
The reason, is why IntelliJ IDEA is called the most intelligent Java IDE, is that it has many aspects of any feature, that reveal depending on the results of analysis of your code. Data flow analysis is an example. IntelliJ IDEA analyses data flow to guess the type of a variable in runtime and provide a completion, based on this type, adding class casts automatically.
Many API provide constants of primitive types instead of enum types. IntelliJ IDEA is aware of such constants in the project and suggests them in completion, what is very helpful, if you are dealing with legacy APIs.
You might noticed, how it is deadly boring to manually type a super constructor having many arguments. IntelliJ IDEA understands you and provides completion for this case too.
Another brilliant feature of IntelliJ IDEA is Complete statement ⌘⇧⏎, which helps you complete the rest part of a statement. It automatically adds missing parentheses, brackets, braces and apply necessary formatting.
You might consider this a minor feature, but you will be surprised, how much time it can save, when you get used to it.
To see how it works, try to type “if” and click ⌘⇧⏎. You can also use it when you select item from suggestion list.
I believe, that pretty number of IDEs, provide info on expected parameters, when you call a method or a constructor. However the way, how they provide it, does matter.
IntelliJ IDEA shows parameter info for every overloaded method or constructor, and highlight the best match for the parameters already typed. This helps you choosing between overloaded options and compare your input with expectation.
If you don’t know, Parameter info may be also shown on demand, by clicking ⌘P.
One more feature, you might not know, is Quick definition ⌘⇧I. It raises a popup with definition for every item from suggestion list. In many cases definition info is more useful than even documentation, especially when you are not sure, which method is to use.
Exclude from completion
I’m sure, everyone at least once faced the situation, when some class, that you never use, is constantly suggested in completion (e.g. from com.sun or java.awt packages).
IntelliJ IDEA can help you exclude such class or even the whole package from completion. Just call Intention ⌥⏎ on an item and confirm exclusion. You can always turn it back in Settings.
Next feature I want to mention, is much more than only about code completion. I hope to cover it deeply in a separate article, but in the meanwhile, I will describe the aspects of this feature, which relate to code completion.
Before IntelliJ IDEA provides code completion for such expression, you must specify it’s language. You can do it by calling Intention action ⌥⏎ on a String literal. After that you will be able to use code completion, as it was not a String literal.
For instance, if you define a datasource in IntelliJ IDEA, you will get code completion for SQL expressions, based on actual tables scheme. The IDE analyzes all the project files to provide advanced completion for every supported language.
Completion with Tab
One more feature, that makes life easier, is Сompletion with Tab. If you select an item from suggestion list by clicking ⇥, it will overwrite identifier at the caret, instead of just insert. This is helpful if you edit a fragment of an identifier, such as name of a file.
Negating boolean results
Not everyone knows, that if you select boolean item from suggestion list with !, IntelliJ IDEA automatically adds negation operator to the result. This is an additional way to approve completion.
Another feature, which is quite impressive and efficient is Middle matching. It means, you can type an an identifier not even from the beginning. IntelliJ IDEA will still offer you correct completion. This is helpful and improves your performance significantly, especially if you remember only a part of the name.
Case sensitivity settings
As you might know, the common approach in code completion implies using camel humps to match your input with class names. Thus you need to enter at least one uppercase letter to give an IDE a clue, which class you want to have completed. IntelliJ IDEA advances this approach with case-insensitive completion. This means you don’t need to press ⇧ for each camel hump to get completion. Twice as less keys every day.
By default IntelliJ IDEA is sensitive to the first letter only. You can change it in Settings.
Completion for properties, file names, resources, etc.
One more feature, someone may not know is completion within String literals, recognized as property names, file names, resources, etc.
Class name completion in non-Java files
Additionally to Java, IntelliJ IDEA provides code completion for fully-qualified class names in other resource files, such as property files, XML, etc.
Completion in other places
One of the nicest things about completion in IntelliJ IDEA, you will hardly find in other IDEs, is that, it works not only in the editor, but also in Evaluate Expression, Watches, file choosers, search inputs and many other elements over the IDE.
Code completion in Evaluate expression and Watches tools is absolutely invaluable, when you evaluate state of your application while debug.
This may be useful, if you have hard time finding particular item in suggestion list.
I hope this article will you help better understand how code completion works in IntelliJ IDEA, just as benefits it gives for a developer.
Please also use Help → Productivity Guide to see how many completion types you use and how much time IntelliJ IDEA saves for you every day.
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)