Search code examples
javaintellij-ideaintellij-plugin

How can I add another EditorActionHandler to a single IdeAction without losing functionality?


I have a plugin where I would like to add additional functionality when the user triggers the BACKSPACE and DELETE key, without losing the initial functionality (in this case, character deletion).

I am trying to override the EditorActionHandler for the specified ideActions.<ACTION_EDITOR_VALUE>:

public class MyPlugin implements BaseComponent {

@Override
public void initializeComponent() {

    final EditorActionManager editorActionManager = EditorActionManager.getInstance();
    EditorActionHandler originalBackspaceHandler = editorActionManager.getActionHandler(IdeActions.ACTION_EDITOR_BACKSPACE);

    EditorActionHandler eaHandler = new EditorActionHandler() {
        @Override
        protected void doExecute(@NotNull Editor editor, @Nullable Caret caret, DataContext dataContext) {
            super.doExecute(editor, caret, dataContext);

            originalBackspaceHandler.execute(editor,caret,dataContext);

            PsiElement e = dataContext.getData(CommonDataKeys.PSI_ELEMENT);
            if(e != null) {
                System.out.println("psi Element" + e.toString());
            }
        }

    };        
  editorActionManager.setActionHandler(IdeActions.ACTION_EDITOR_DELETE,  eaHandler);   

} //initalizeComponent()   
} //MyPlugin

Is this the preferred way of overriding editorAction handlers while maintaining current functionality? I would like to target other IdeActions.<VALUES> and reuse the same EditorActionHandler.


Solution

  • There is no common way to override action handlers. Your approach is one of the possibilities.

    E.g. Here is a code of smart step into handlers for "right arrow", "left arrow", etc. A similar approach is used.