BlockData (@NotNull PsiElement startElement) { PsiStatement statement = PsiTreeUtil.getNonStrictParentOfType(startElement, PsiStatement.class); if (statement instanceof PsiBlockStatement) { statement = PsiTreeUtil.getNonStrictParentOfType(statement.getParent(), PsiStatement.class); } final PsiElement body; if (statement instanceof PsiLoopStatement loopStatement) { body = loopStatement.getBody(); } else if (statement instanceof PsiIfStatement ifStatement) { body = myKeywordText.equals("else") ? ifStatement.getElseBranch() : ifStatement.getThenBranch(); } else { return null; } if (!(body instanceof PsiBlockStatement block)) return null; return new BlockData(statement, block); }