PsiElement (@NotNull PsiElement position) { return JBIterable. generate(position, PsiElement::getParent). takeWhile(e -> !(e instanceof PsiMember || e instanceof PsiFile || e instanceof PsiLambdaExpression)). filter(e -> e instanceof PsiCodeBlock || e instanceof PsiExpression && e.getParent() instanceof PsiLambdaExpression). last(); }