How to delegate backspace and Enter button in TypedActionHandler?

I want to run execute() after backspace or enter key pressed. Currently this works only when a character key is pressed. 

public class MyTypedHandler implements TypedActionHandler {
private TypedActionHandler myOriginalHandler;

public MyTypedHandler(TypedActionHandler originalHandler){
myOriginalHandler = originalHandler;
}

@Override
public void execute(@NotNull Editor editor, char c, @NotNull DataContext dataContext){
myOriginalHandler.execute(editor, c, dataContext);
Document document = editor.getDocument();
Project project = editor.getProject();
ToolWindow toolWindow = ToolWindowManager.getInstance(project).getToolWindow("SCA_Tool");

Runnable runnable = new Runnable() {
@Override
public void run() {

}
};
WriteCommandAction.runWriteCommandAction(project, runnable);
}
}

 

11 comments
Comment actions Permalink

For Enter and Backspace there are separate extension points - see EnterHandlerDelegate and BackspaceHandlerDelegate.

0
Comment actions Permalink

I tried adding two extension points but it didn't worked. Same as this case when I tried to delegate to original typing behaviour https://intellij-support.jetbrains.com/hc/en-us/community/posts/360000597339-Why-I-can-t-see-what-I-type- . But I resolved typing behaviour using myOriginalHandler.

 

0
Comment actions Permalink

What is the specific problem you have with those new extension points?

0
Comment actions Permalink

I don't get any errors. The plugin runs and works but execute() doesn't run when enter key or backspace is pressed. Works fine for any character.

I added extension point for enter key delegation as follows.

<enterHandlerDelegate implementation="com.intellij.codeInsight.editorActions.enter.EnterHandlerDelegateAdapter"/>
0
Comment actions Permalink

There's no 'execute' method in EnterHandlerDelegate. It has its own methods you'll need to overwrite. Looks like you've added an empty implementation.

0
Comment actions Permalink

For that do I have to make a new file called EnterHandlerDelegate.java? I only need to make TypedActionHandler run its execute() for enter and backspace keys as well. Currently it works for any character key.

0
Comment actions Permalink

Yes, you'll need your own implementation of EnterHandlerDelegate. Typed handlers are not invoked for Enter and Backspace.

0
Comment actions Permalink

It worked. Thanks. But when I select one or more characters and press backspace it doesn't work.

0
Comment actions Permalink

If you want to handle such situations as well, you'll need a different approach - register EditorActionHandler for backspace action (actionId=EditorBackSpace). You can check how com.intellij.codeInsight.editorActions.BackspaceHandler is registered in IDEA CE codebase for an example. It's similar to TypedActionHandler, original handler is passed to it, and it should delegate requests to it, if default backspace behaviour should be preserved.

0
Comment actions Permalink

Aroshamudalige1 The solution Dmitry proposed worked for me. Here's my code:

plugin.xml

        <editorActionHandler action="EditorBackSpace" order="first"
                             implementationClass="org.jetbrains.plugins.template.BackspaceActionHandler"/>


BackspaceActionHandler.kt

package org.jetbrains.plugins.template

import com.intellij.openapi.actionSystem.DataContext
import com.intellij.openapi.editor.Caret
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.editor.actionSystem.EditorActionHandler

class BackspaceActionHandler(private val originalHandler: EditorActionHandler): EditorActionHandler() {

    override fun doExecute(editor: Editor, caret: Caret?, dataContext: DataContext?) {
        println("RUNNING")
        originalHandler.execute(editor, caret, dataContext)
    }

}

 

0

Please sign in to leave a comment.