void (@NotNull AnActionEvent event) { final var editor = MarkdownActionUtil.findRequiredMarkdownEditor(event); final var psiFile = event.getRequiredData(CommonDataKeys.PSI_FILE); final var commandName = getTemplatePresentation().getText(); WriteCommandAction.runWriteCommandAction(psiFile.getProject(), commandName, null, () -> { if (!psiFile.isValid()) { return; } for (Caret caret : ContainerUtil.reverse(editor.getCaretModel().getAllCarets())) { PsiElement parent = Objects.requireNonNull(findParent(psiFile, caret.getSelectionStart(), caret.getSelectionEnd())); MarkdownHeader header = PsiTreeUtil.getParentOfType(parent, MarkdownHeader.class, false); if (header != null && header.isValid()) { header.replace(createNewLevelHeader(header)); } else { parent.replace(createHeaderForText(parent)); } } }, psiFile); }