I have this action that executes built in Editor Action - it adds a line above current line, respecting indentation:
class TestAction : AnAction() {
override fun actionPerformed(e: AnActionEvent) {
val actionManager = ActionManager.getInstance()
val newLineAction = actionManager.getAction("EditorStartNewLineBefore")
val dataContext = e.dataContext
val actionEvent = AnActionEvent.createFromAnAction(newLineAction, null, e.place, dataContext)
newLineAction.actionPerformed(actionEvent)
}
}
Then I attempt the same, trying several combinations from function that is executed outside AnAction context - I tried to rebuild dataContext - getting the same context as I had in AnAction attempt, but here indentation is not respected, line is created above, but indentation is not respected, it always ends up at column 1.
val action = ActionManager.getInstance().getAction("EditorStartNewLineBefore")
val dataContext = DataManager.getInstance().getDataContext(editor.contentComponent)
action.actionPerformed(AnActionEvent.createFromAnAction(action, null, "", dataContext))
Interestingly action “EditorStartNewLine” works as intended and respects indentation.
I suspect there is something missing from state that is present in AnAction context but don’t know what…