PsiElement (@NotNull PsiFile psiFile, int selectionStart, int selectionEnd) { final var elements = MarkdownActionUtil.getElementsUnderCaretOrSelection(psiFile, selectionStart, selectionEnd); PsiElement first = elements.getFirst(); PsiElement second = elements.getSecond(); if (MarkdownPsiUtil.WhiteSpaces.isNewLine(first)) { first = PsiTreeUtil.nextVisibleLeaf(first); } if (MarkdownPsiUtil.WhiteSpaces.isNewLine(second)) { second = PsiTreeUtil.prevVisibleLeaf(second); } if (first == null || second == null || first.getTextOffset() > second.getTextOffset()) { return null; } PsiElement parent = MarkdownActionUtil .getCommonParentOfTypes(first, second, TokenSet.orSet(INLINE_HOLDING_ELEMENT_TYPES, INLINE_HOLDING_ELEMENT_PARENTS_TYPES)); if (parent == null || PsiUtilCore.getElementType(parent) != PARAGRAPH) { return parent; } Document document = PsiDocumentManager.getInstance(psiFile.getProject()).getDocument(psiFile); assert document != null; int startOffset = parent.getTextRange().getStartOffset(); int endOffset = parent.getTextRange().getEndOffset(); if (startOffset < 0 || endOffset > document.getTextLength()) { return null; } if (document.getLineNumber(startOffset) == document.getLineNumber(endOffset)) { return parent; } return null; }