IntelliJ Platform Plugin SDK Help

Code Intentions

This topic describes the conditional_operator_intention, a sample plugin that adds a new intention action to the IDE Intentions list. In addition, the sample plugin contains a JUnit-based test.

About Intention Actions

The IntelliJ Platform analyzes your code and helps handle situations that may result in errors. When a possible problem is suspected, the IDE suggests an appropriate intention action, denoted with special icons. For more information, refer to Intention Actions in the IntelliJ IDEA Web Help.

See Inspections topic in the IntelliJ Platform UI Guidelines on naming, writing description, and message texts for inspections/intentions.

You can view a list of all available intention actions as well as enable/disable them using the Intentions List in Settings/Preferences | Editor | Intentions.

Techniques Used

The conditional_operator_intention sample plugin illustrates the use of the following techniques:

Sample Plugin

When launched, this plugin adds the Convert ternary operator if statement item to the Conditional Operator node in the Intentions List:

Intentions list

Running the Plugin

See Code Samples on how to set up and run the plugin.

How does it work?

The plugin analyzes symbols under the cursor in your code opened in the editor. If the cursor is positioned on the ? conditional operator, IntelliJ IDEA proposes to replace this conditional (ternary) operator with the "if-then-else" statement:

Ternary operator

In this example, the code:

return (n>=0) ? n : -n;

will be replaced with the code:

if ((n>=0)) { return n; } else { return -n; }

Testing the Plugin

The sample plugin contains the ConditionalOperatorConverterTest Java class and the test data in the test/testData/ directory. To perform the plugin test, run the ConditionalOperatorConverterTest.testIntention() method.

For detailed information about testing and all related procedures, refer to Testing in the IntelliJ IDEA Web Help.

Last modified: 03 June 2021