| Intention Actions |
|
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. ![]() ![]()
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. ![]() 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. ![]() ![]()
Generally, intention actions can be divided into several groups:
To see the complete list of Intention Actions, please, refer to the Intention Action List section.
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: ![]() ![]() ![]()
This is a special case of the 'Create from Usage' Intention Action. The unresolved method call should contain words get, set or is ![]() ![]() ![]() ![]()
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: ![]() ![]()
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: ![]()
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 ![]() 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 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;
You can configure the Import Assistant feature to behave as an Intention Action. For more information, see Managing Imports: Import Assistant. |