This topic describes the ConditionalOperatorConverter sample plugin that adds a new intention action to the IDEA Intensions list. In addition, the sample plugin contains a
JUnit-based test. Basing on this information, you can develop your own plugins using similar techniques.
About Intention Actions
IntelliJ IDEA interface analyzes your code and helps you handle the situations that may result in errors. When a possible problem is suspected, IntelliJ IDEA suggests appropriate intention actions, denoted with the special icons. For more information, refer to Intention Actions in IntelliJ IDEA Web Help.
You can view a list of all available intention actions using the Intention List provided by IntelliJ IDEA interface.
To display Intention List
- On the toolbar, click .
The Settings dialog box opens.
- Under IDE Settings, click Intentions.
This displays the list of all intention actions currently available in IntelliJ IDEA. The intention actions are grouped according to the areas of their use.
To enable/disable an intention action, select/clear the check box to its left.
The ConditionalOperatorConverter sample plugin illustrates the use of the following techniques:
- How to analyze a PSI tree .
- How to find a Java token of interest in the PSI tree.
- How to invoke a quick fix action for a token element under cursor using the PsiElementBaseIntentionAction class.
- How to create a
JUnittest for this plugin using the IdeaTestFixtureFactory class.
The ConditionalOperatorConverter sample plugin is available in the <%IDEA project folder%>/community/samples/ConditionalOperatorConvertor> directory. When launched, this plugin adds the Convert ternary operator if statement item under Conditional Operator in the IDEA Intensions list:
Running the Plugin
To run the sample plugin
- Start IntelliJ IDEA and open the conditionalOperatorConvertor plugin project saved into the <%IDEA project folder%>/community/samples/conditionalOperatorConvertor directory.
- Ensure that the project settings are valid for your environment. If necessary, modify the project settings.
To view or modify the project settings, on the toolbar, click , and then complete the Project Structure dialog box that opens.
- Run the plugin by choosing the Run | Run on the main menu.
If necessary, change the Run/Debug Configurations .
How It Works?
The plugin analyzes symbols under the cursor in your code opened in the IDEA editor. If the cursor is positioned on the "?" operator, IntelliJ IDEA proposes to replace the "?" conditional (ternary) operator with the "if-then-else" statement.