void (@NotNull AnActionEvent e) { final Presentation presentation = e.getPresentation(); Project project = e.getProject(); if (project == null) { presentation.setEnabled(false); return; } Editor hostEditor = e.getData(CommonDataKeys.EDITOR); if (hostEditor == null) { presentation.setEnabled(false); return; } if (hostEditor.isDisposed()) { LOG.error("Disposed editor in " + e.getDataContext() + " for " + this); presentation.setEnabled(false); return; } final Ref<Boolean> enabled = new Ref<>(Boolean.FALSE); iterateOverCarets(project, hostEditor, new MultiCaretCodeInsightActionHandler() { @Override public void invoke(@NotNull Project project, @NotNull Editor editor, @NotNull Caret caret, @NotNull PsiFile file) { if (isValidFor(project, editor, caret, file)) { enabled.set(Boolean.TRUE); } } }); presentation.setEnabled(enabled.get()); }