PsiElement (PsiVariable variable, PsiElement context) { PsiElement declarationScope = variable instanceof PsiParameter ? ((PsiParameter)variable).getDeclarationScope() : PsiUtil.getVariableCodeBlock(variable, null); if (declarationScope == null) return null; PsiElement statement = context; nextInnerClass: do { statement = CommonJavaRefactoringUtil.getParentStatement(statement, false); if (statement == null || statement.getParent() == null) { return null; } PsiElement element = statement; while (element != declarationScope && !(element instanceof PsiFile)) { if (element instanceof PsiClass || element instanceof PsiLambdaExpression || element instanceof PsiSwitchLabelStatementBase) { statement = statement.getParent(); continue nextInnerClass; } element = element.getParent(); } return statement; } while (true); }