Search code examples
javaintellij-ideaintellij-plugin

How to verify that IntentionAction was called on an interface file? IntelliJ Platform SDK


I am new in the IntelliJ Platform SDK and a plugin development.

I want to implement IntentionAction that available only if press Alt+Enter in an editor when a cursor set on an interface declaration. In other words, expected behavior as in cases when the "Implement interface" intention action appears.

enter image description here

Place in an editor:

public interface SomeInterfaceNameDeclaration {

Solution

  • In your IntentionAction.isAvailable() method, you receive an Editor and a PsiFile. You can use file.findElementAt(editor.getCaretModel().getOffset()) to find a PSI element at caret, and then PsiTreeUtil.getParentOfType() to find an element of the type you need. See the documentation for more information.

    Also, you can refer to the source code of the "Implement interface" action as an example.