Intention Actions
Overview

Everyone makes mistakes while writing code. Most IDEs simply ignore this fact and leave you on your own. You have to compile to find out you did something wrong, and then you're on your own to figure out what you did, where you did it, and how to fix it when you finally find what you did.

IntelliJ IDEA, on the other hand, assumes that mistakes will happen when you're coding complex solutions under deadline pressure, so some intelligence is built in to assist you in dealing with them, trying to guess what you were going to make or what you INTENDED to do. Almost like a pair programming partner, IntelliJ IDEA observes the code you write and tries to flag problems as they occur. What's more, it suggests one or more ways of solving the problem. If you agree with a proposed solution, IntelliJ IDEA fixes the problem after a quick keystroke or click. If the proposed fixes are not what you need, you at least know about the problem and can correct it yourself. In other words, when IntelliJ IDEA considers that there is something wrong with your code, it suggests an ACTION to fix it. This feature is called Intention Actions. Not only, does is help you find and fix mistakes, but it can also speed coding by enabling you to start using new things before they have been declared. Let's see an example of how this can help you.

Suppose that while writing code, you find you need a new field. Traditionally, you would scroll up the class declaration, declare the new field, then scroll back down and begin using it. You might do those steps in the opposite order, or you might just use a new symbol and forget to declare it, not realizing your mistake until you compile. In any case, it's inefficient. With IDEA, you can just start using the new field without declaring it first. The name will be highlighted as an error because it is not yet declared. Now move the caret into the name. IntelliJ IDEA will show a light bulb sign by the current line. That's your cue that the Intention Action feature has spotted the problem and has a suggestion for you.
Now press Alt + Enter (or click the light bulb) and IDEA pops up a list of suggested actions it can take with this reference to fix the code.
In this case, IntelliJ IDEA suggests several possible actions: create either a field, a local variable, or a parameter, or rename the reference. If a suggested solution is suitable (in this case, creating a field), press Enter (or click the item in the popup). IntelliJ IDEA will then create the new field declaration at an appropriate line (which you don't need to scroll to!), and also try to guess its type. If none of the suggestions reflect what you really want, press Escape to remove the popup. You can then proceed to fix the code however you choose.


Intention actions also appear as appropriate in the GUI Designer for form components and/or properties.



Disabling Intention Actions
Disabling Intention Actions

If you needn't an intention action to appear in a project for a particular expression or statement each time the caret is on it you can switch it off. Every suggested intention action has a bulb sign to the left. Just click it. The bulb sign will be grayed and this action will not appear again. However, if there are several actions possible you have to switch off all of them.
And if you wish to see what intention actions are available for this expression or statement you can still use the Alt + Enter shortcut. To switch on the disabled intention action click the grayed bulb.

There is another way to manage the inspection-related intention actions - they can be suppressed. In other words, you can "switch off" inspection check for either particular class or a class member, or edit settings for the selected inspection.

After the intention action is opened and there is a small arrow at the right, press it or press Enter.
In the appearing popup select the desired action - to suppress inspections either for a statement/method/class or edit the inspection properties. The inspection properties can be edited in the 'Inspection name' Settings dialog.
The dialog elements are similar to the Inspection & Highlighting Profiles option group of the Errors dialog.

Types of Intention Actions

Generally, intention actions can be divided into several groups:
  1. "Create from usage" intentions
    This type of intention action creates new code items: classes, interfaces, methods, etc. They are smart enough to analyze your code and provide actions suitable for the particular case. For example, Create constant field is suggested if the reference is uppercase, or Create class appears when a name is typed after the new keyword, or when an identifier starts with a capitalized letter, etc.

    The main concept behind this type is that you can begin using new things without declaring them first. You are not taken away from your current task for mundane minutiae like creating declarations, new files, etc. which IDEA handles while you keep focused.

  2. Quick fixes
    This type of intention action responds to common coding mistakes: a misspelled variable name, using an improper access modifier, or an expression of the wrong type. IDEA catches these kinds of problems as you type, and provides a quick way to fix them using Intentions Actions with appropriate suggestions for the error.

  3. Micro-refactorings
    These intention actions appear for code that is correct, but which may possible be made more readable by such things as:
       splitting declaration and assignment
       inverting if condition
       splitting into 2 if statements
       etc.

  4. EJB-specific intentions
    This type of intention action occurs only within EJB classes. They catch and provide fixes for a number of error and warning conditions. For details, see Intention Action List: Intention Actions for EJBs.

  5. Miscellaneous intentions
    The group includes intentions that cannot be placed in any of the groups above. Currently, there are two of them: Fetch external resource and Ignore external resource.
The light bulb sign differs for various intentions.

Intention action sign A yellow bulb indicates that IDEA just proposes you to alter your code. It covers a range of situations, from warning correction to suggestions for code improvement (like micro-refactorings).
Quick-fix sign A red bulb with an exclamation mark indicates that IntelliJ IDEA suggests you a way to fix an error. It is related to such intentions as Create from usage intentions and Quick fixes.


To see the complete list of Intention Actions, please, refer to the Intention Action List section.

'Create from Usage' Intention Actions

This is a common name for a group of intention actions including 'Create class from usage', 'Create field from usage', etc. These intentions work in a quite similar way when you have unresolved references.

For instance, you have the following unresolved reference:
Press Alt + Enter and select Create field 'ref_1'.
IDEA creates a field and suggests an appropriate type in the drop-down list.
You can select one of the types suggested by IntelliJ IDEA or enter another one.

'Create Getter/Setter' Intention Action

This is a special case of the 'Create from Usage' Intention Action.

The unresolved method call should contain words get, set or is
Press Alt + Enter. IntelliJ IDEA suggests to create a getter (or setter when prefix set is present) for the corresponding reference. Select Create Getter.
IDEA creates a getter method with a suggested return type, which is automatically selected. You can either type a new value or select a different type from a list by pressing Ctrl + Space.
After specifying the return type for the method (press Enter), a list pops up enabling you to choose a name for the returned variable chosen from the fields of an applicable type. You can select one of the types suggested by IntelliJ IDEA or enter another one. Besides, you can base the accessor on the existing field or define a new one.
'Rename reference' Intention Action

The Rename reference intention action works like intentions of the Create from Usage type.

Sometimes you can mistype a name. A quick way to fix it is with the Rename reference intention action:
When you choose Rename reference form the actions list, IDEA makes a suggestion from among the references with similar spelling, and drops down a list of valid field and local variable names.
You can keep the default suggestion in (selected and outlined in red) by pressing Enter, or you can type a different name (a good choice if the name is short), or you can select one of the names suggested by IntelliJ IDEA in the drop-down list (a good idea for long names). By typing the first few letters you can narrow down the list. After you press Enter, all unresolved references with the same name are renamed to the new value.

'Add Javadoc Tag' Intention Action

The Add Javadoc Tag intention action works like intentions of the Create from Usage type.

If you write a new custom Javadoc tag, an Intention Action is suggested that enables you to add it to the list of custom Javadoc tags:

You can manage custom Javadoc tags in File | Settings | IDE Settings: Errors: Unknown Javadoc Tags: Options.


Interface Intention Action on Parameter Mismatch

This intention action is related to the micro-refactorings. It inserts interface implementation into a class in case there is a method call where some parameters have a type of this interface.

Let's take following two classes and an interface.



The bar() method of ClassCaller calls foo() from ClassCallee. However, the caller class lacks implementation of the myInterface interface. Press Alt + Enter.



Select 'Make 'ClassCaller' implement Sample.I'.



The interface implementation is added to the class declaration. Also the interface methods will be suggested for implementation with the Select Methods to Implement dialog.

Static Import Intention Actions

Static import enables you to use static classes and class members without a qualifier. There two following static import intentions, "on demand" and "single static import". On-demand static import is called if the caret is on the class name in the editor.



Then all members of the imported class will be used without a qualifier. The import statement will look as follows:

import static java.lang.Math.*;

Single static import is called if the caret is on the class name in the editor.



Only members with the given name in the imported class will be used without a qualifier. The import statement will look as follows:

import static java.lang.Math.abs;


To use these intentions, the Language level for Java SDK 1.5 should be enabled.



Import Assistant

You can configure the Import Assistant feature to behave as an Intention Action. For more information, see Managing Imports: Import Assistant.