PsiElement (PsiExpression expression) { PsiExpression outermost = expression; while (outermost.getParent() instanceof PsiExpression) { outermost = (PsiExpression)outermost.getParent(); } if (outermost.getParent() instanceof PsiForStatement) { final PsiForStatement forStatement = (PsiForStatement)outermost.getParent(); if (forStatement.getCondition() == outermost) { return forStatement; } else { return null; } } if (outermost.getParent() instanceof PsiExpressionStatement && outermost.getParent().getParent() instanceof PsiForStatement) { final PsiForStatement forStatement = (PsiForStatement)outermost.getParent().getParent(); if (forStatement.getUpdate() == outermost.getParent()) { return forStatement; } else { return null; } } if (outermost.getParent() instanceof PsiWhileStatement) { return outermost.getParent(); } if (outermost.getParent() instanceof PsiDoWhileStatement) { return outermost.getParent(); } return null; }