TypedActionHandler with default typing behavior

I want to create a plugin that trigger typing event on editor without losing typing behavior.

The code I built is following

public class EditorIllustration extends AnAction {
    static {
        final EditorActionManager actionManager = EditorActionManager.getInstance();
        final TypedAction typedAction = actionManager.getTypedAction();
        typedAction.setupHandler(new MyTypedActionHandler());
    }
}

public class MyTypedActionHandler implements TypedActionHandler {
    @Override
    public void
execute(final Editor editor, final char c, DataContext dataContext) {
        final Document document = editor.getDocument();
        final Project project = editor.getProject();
        Runnable runnable = new Runnable() {
            @Override
            public void
run() {
                insertCharacter(editor, document, c);
                // trigger plugin action
            
}
        };
        WriteCommandAction.runWriteCommandAction(project, runnable);
    }

    private void insertCharacter(Editor editor, Document document, char c) {
        CaretModel caretModel = editor.getCaretModel();
        document.insertString(caretModel.getOffset(), String.valueOf(c));
        caretModel.moveToOffset(caretModel.getOffset() + 1);
    }
}


This does not work if the developer highlights words and types a character. (Highlighted words should be removed.)
I don't want to reinvent the default typing behavior.
Is there any other way to do this?

Thanks, in advance.

2 comments
Comment actions Permalink

Yes. You need to use the TypedHandlerDelegate interface and the <typedHandler> extension point. In your implementation, return Result.CONTINUE.

1
Comment actions Permalink

This is what I want!
What I did is the followings.

plugin.xml

<extensions defaultExtensionNs="com.intellij">

    <!-- Add your extensions here -->

    <typedHandler implementation="handler.MyTypedHandlerDelegate"/>


</extensions>

MyTypedHandlerDelegate.java

 
public class MyTypedHandlerDelegate extends TypedHandlerDelegate {
    @Override
    public
Result charTyped(char c, Project project, Editor editor, PsiFile file) {
        return Result.CONTINUE;
    }
}

Thanks!

1

Please sign in to leave a comment.