When it comes to implementing "quick fixes" there are two separate class hierarchies you can use, QuickFix
(and LocalQuickFix
) and IntentionAction
. There also seems to be ways of implementing both (as in LocalQuickFixAndIntentionActionOnPsiElement
).
I'd like to know the difference between those two base classes. When would I use one but not the other? Is one of those hierarchies obsolete and superseded by the other?
A QuickFix
usually belongs to an Inspection
, and should be implemented as an automated fix to the warning issued by the inspection.
Try, for example, using list.size() == 0
in Java. IntelliJ will suggest to replace this with list.isEmpty()
.
Note the highlighting (in IntelliJ).
The action that you can trigger with Alt + Enter is called the quick fix, and it will execute this replacement for you.
An intention is similar in the sense that it is an action specific to a piece of code that can be triggered by Alt + Enter. The main difference is that IntelliJ doesn't complain if you don't use the intention; there is no highlighting to indicate that something is or could be wrong here, because there probably isn't.
Intentions are meant to make the life of the user easier, but they don't get as in your face as inspections do.
To give an example, this could be used in LaTeX to replace any brace pair by their \left\right
equivalent; it's not something to trigger an inspection for because often the simple brace pair is preferred, but it's nice to be able to do this replacement with a simple shortcut.
You'll regularly encounter loops in intentions as well, and even intentions that go in the opposite direction as a quick fix, but you won't see loops in quick fixes (unless you've explicitly enabled both directions of an inspection).
To summarise: use a quick fix when implementing an automated fix to an inspection, and use an intention action otherwise.