Code Completion

We’ve been updating our videos lately. Some are topics that haven’t been covered before, and some, like this one, have been covered more than once. In this video, we wanted to show the different types of code completion available in IntelliJ IDEA, and when you might want to use them.  There will be things in this video you already know, but you might also find a surprise or two.

This blog post is adapted from the transcript of the video.

If you want to try out the code from the video, you can download the project from github.

We get code completion as soon as we start typing in IntelliJ IDEA, we don’t always need to use a keyboard shortcut to bring up the suggestions. Suggestions are also context sensitive, so they should make sense for the current place we’re typing.

Open up a new Java class file (like this one) and start typing a method declaration. As we type private void we see IntelliJ IDEA shows a basic completion drop-down when we’re typing each word. After we type a method name, when we type an opening bracket ( IntelliJ IDEA inserts the closing bracket ). The same happens when we type { and press Enter to go into the method body.

When we start typing inside the method, for example to create a new variable, the completion drop-down is also shown. If we start typing new Sq, for example, the drop-down gives us the option to choose Square.

We can press Alt+Enter at the end of this and have IntelliJ IDEA create a valid variable declaration for us. We even get useful defaults suggested for variable names.

Even as we’re typing our variable declaration, IntelliJ IDEA will suggest types and names for us. Type the following and see what happens:

When we’re writing a String value, it will automatically insert a closing quote as soon as we type the opening quote. To move outside of the String value, we can press tab and type a semi-colon to correctly finish off our line.

There’s another trick we can use to automatically finish off lines, we can press ⇧⌘⏎ on MacOS, or Shift+Ctrl+Enter on Windows, to complete the current statement. We’ll see this in more detail later.

Let’s create a slightly more complex example. Let’s create a List of Strings, again notice the brackets < are closed for us. We can force the Basic Completion list to appear by pressing Ctrl+Space, use this to show a list of suggested variable names given the type.

Press Ctrl+Space after the > to force a list of suggested variable names.

Now let’s try to use a variable.

IntelliJ IDEA shows a suggests a variable for us, and if we select it and type a dot we get the Basic Completion list of methods we could call. Methods directly on the class itself are in bold, and methods on super-classes are displayed in a lighter font. This list is usually ordered with the most frequently used methods at the top, if that information is available to the IDE.

basic-completion

Let’s change the return type of this method to a Shape, to demonstrate some other features of completion. Let’s create another variable that’s a Circle. Square and Circle both implement the Shape interface.

Now let’s say we want to return something from this method.

If we go to the bottom of the method and type return and use Basic Completion, we have the variables we declared in this method at the top of the list, and quite a long list of options.

Basic Completion for return

If we use Smart Completion (Ctrl+Shift+Space) instead, we’re only given two options, square and circle, which are the only two variables with the correct type for the return type of this method.

Smart Completion for return

Let’s change the return type of this method to Square which is even more restrictive. Now when we use Smart Completion, IntelliJ IDEA simply automatically selects the only variable that is the correct type.

We can use Smart Completion in a lot of different places, and it will always suggest something of the correct type. If we use it on the right hand side of the list assignment, it suggests the Collections factory methods that create Lists that were introduced in Java 9.

Let’s change this to a List of Squares. If we choose the Square item from the Basic Completion drop-down using the Enter key, Square will be inserted where the caret is:

This is not what we want, we want to replace the String type. So instead we need to use Tab to select the item we want, and this will replace the text that’s after the caret. We can use Tab to replace the variable name with the new suggestion as well.

We saw earlier how the Complete Statement feature can be used to put a semi colon on the end of an unfinished line. We can use this feature for other things too. If we type for and then use complete statement (⇧⌘/ Ctrl+Shift+Enter), IntelliJ IDEA will insert the round brackets and curly braces needed for a for loop, and place the caret inside the brackets.

We can then use IntelliJ IDEA’s suggestions to fill in the for loop values Square and square1, and Smart Completion will choose the only Iterable available in this context. Pressing ⇧⌘, or Ctrl+Shift+Enter, again will put our caret into the body of the for loop.

We can use Complete Current Statement to create a valid if statement as well.

Let’s type square1 into the brackets of the if statement and press .. If we select a method suggestion from the completion drop-down using Ctrl+. the method call will be added to our code followed by a dot/period/full-stop (“.”) – we can use this if we know we’re going to chain method calls. Let’s try this to create the following code:

What we want is to do something if the ID is not blank. Instead of having to move the caret all the way to the start of the expression to type an exclamation mark to negate this statement, we can simple type .not after isBlank() and press Enter, to use postfix completion to negate this expression.

There’s a whole range of postfix completion templates. For example, inside the if statement we could type square1.getId() (using code completion to help us of course) and then add .sout and press Enter to wrap the expression in a System.out.println.

To see all the postfix options on a symbol, type a dot and then press the up arrow to browse the completion drop-down. We can see all the postfix completion templates, and even add new ones in IntelliJ IDEA’s preferences.

Now we’ve seen and used all the main code completion techniques. Let’s finish up by taking a quick look at some other areas of code completion that might be useful.

Hippie completion analyzes text from all parts of any open file. We can use Alt+/ to have IntelliJ IDEA suggest any matching value from the file. This may be most useful when we’re creating or reusing text values.

Code completion isn’t just for Java code. Code completion is available for all supported languages. While we might expect code completion inside files for each language type, IntelliJ IDEA can use language injection to provide code completion even inside String values in, for example, a Java class. We can tell IntelliJ IDEA that a particular String contains a particular language, and we’ll get code suggestions and completion inside this String value.

We also get help when writing HTML or XML files, such as suggestions and completion.

You may be surprised at which parts of IntelliJ IDEA support completion. Often input fields on dialogs also support it. Try invoking Basic and Smart completion in places you don’t normally use it to see if there are some hidden gems.

See also:

This entry was posted in Tips & Tricks and tagged , , . Bookmark the permalink.

2 Responses to Code Completion

  1. c says:

    wish smart completion worked on Linux :-(

    IntelliJ IDEA 2020.1.1 (Ultimate Edition)
    Build #IU-201.7223.91, built on April 29, 2020
    Licensed to w press
    Subscription is active until October 6, 2020
    Runtime version: 11.0.6+8-b765.40 amd64
    VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
    Linux 4.8.0-59-generic
    GC: ParNew, ConcurrentMarkSweep
    Memory: 725M
    Cores: 8
    Registry: debugger.watches.in.variables=false
    Non-Bundled Plugins: Lombook Plugin, org.intellij.scala, com.intellij.plugins.html.instantEditing, org.nik.presentation-assistant, quokka.js

Leave a Reply

Your email address will not be published. Required fields are marked *