How to add an action button to the upper right corner of the EditorTextField?

How to add an action button to the upper right corner of the EditorTextField?

Hello,
I see no way how to do that properly. That particular widget is — to quote the docs — a hack.

The alternative, contributing to the Inspection Widget, is an internal API in the com.intellij.openapi.editor.ex.EditorMarkupModel#addInspectionWidgetAction.

In the future, you can use the UI Inspector to investigate the source of this “Gear” UI element. Use Ctrl-Alt and click (Control+Option and Click on macOS) on that element. The UI inspector will show up. It will lead you to the DecompilerInEditorListener in the Community sources. There, you can discover additional comments and documentation.

This method is marked for internal use. Is it safe for me to use it?

The hard to find EP com.intellij.iw.actionProvider should be able to do it and not use any internal API

Interface: intellij-community/platform/platform-api/src/com/intellij/openapi/editor/markup/InspectionWidgetActionProvider.kt at 56606a9ddbcf9c78723553f95683e01055aaae26 · JetBrains/intellij-community · GitHub

1 Like

@ApiStatus.Internal are not to be used by 3rd party plugins. IntelliJ Plugin Verifier and JetBrains Marketplace will report such usages.

However, @abrooksv has a great point. The suggested API is not @ApiStatus.Internal. And on top, it is indeed quite hidden, the only implementation is in the Reader Mode.

To contribute to the Inspection Widget, declare the com.intellij.iw.actionProvider:

<extensions defaultExtensionNs="com.intellij">
    <iw.actionProvider 
            implementation="com.portablesimula.MyInspectionWidgetActionProvider" />
</extensions>

Then, an implementation of com.intellij.openapi.editor.markup.InspectionWidgetActionProvider that provides AnAction

import com.intellij.openapi.actionSystem.ActionManager
import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.editor.markup.InspectionWidgetActionProvider

class MyInspectionWidgetActionProvider : InspectionWidgetActionProvider {
    override fun createAction(editor: Editor): AnAction? {
        return ActionManager.getInstance().getAction("com.example.MyAction")
    }
}

Additional notes:

  • null means that no action will be shown. Useful for cases when the editor does not support a particular action.
  • ActionGroup elements will be flattened. For example, the built-in ReaderModeActionProvider provides a two-element action group: an action and a separator.