PsiElement (@NotNull PsiElement scope, @NotNull PsiElement maxScope) { PsiElement result = maxScope; if (result instanceof PsiLoopStatement) { return result; } while (!result.equals(scope)) { final PsiElement element = getChildWhichContainsElement(result, scope); if (element instanceof PsiForStatement forStatement) { if (forStatement.getInitialization() instanceof PsiEmptyStatement) { return element; } } if (element == null || element instanceof PsiLoopStatement || element instanceof PsiClass || element instanceof PsiLambdaExpression) { while (result != null && !(result instanceof PsiCodeBlock)) { result = result.getParent(); } return result; } else { result = element; } } return scope; }