PsiElement (@Nullable PsiElement element) { if (element instanceof PsiExpression) { PsiElement expression = PsiTreeUtil.findFirstParent(element, e -> !(e.getParent() instanceof PsiParenthesizedExpression)); while (expression != null) { final PsiElement parent = expression.getParent(); if (parent instanceof PsiExpressionStatement) { final PsiElement grandParent = parent.getParent(); if (grandParent instanceof PsiForStatement) { if (((PsiForStatement)grandParent).getInitialization() == parent) { return grandParent; } return null; } return parent; } if (parent instanceof PsiLocalVariable && ((PsiLocalVariable)parent).getInitializer() == expression) { PsiElement grandParent = parent.getParent(); if (grandParent instanceof PsiDeclarationStatement) return grandParent; if (grandParent instanceof PsiResourceList && grandParent.getParent() instanceof PsiTryStatement) { return grandParent.getParent(); } return null; } if (parent instanceof PsiIfStatement && ((PsiIfStatement)parent).getCondition() == expression || parent instanceof PsiSwitchBlock && ((PsiSwitchBlock)parent).getExpression() == expression || parent instanceof PsiWhileStatement && ((PsiWhileStatement)parent).getCondition() == expression || parent instanceof PsiForeachStatement && ((PsiForeachStatement)parent).getIteratedValue() == expression || parent instanceof PsiReturnStatement && ((PsiReturnStatement)parent).getReturnValue() == expression || parent instanceof PsiYieldStatement && ((PsiYieldStatement)parent).getExpression() == expression || parent instanceof PsiThrowStatement && ((PsiThrowStatement)parent).getException() == expression || parent instanceof PsiSynchronizedStatement && ((PsiSynchronizedStatement)parent).getLockExpression() == expression || parent instanceof PsiAssertStatement && ((PsiAssertStatement)parent).getAssertCondition() == expression) { return parent; } if (parent instanceof PsiExpression) { expression = parent; } else { break; } } } return null; }