List<PsiElement> (PsiElement anchor, PsiExpression expr) { List<PsiElement> containers = new ArrayList<>(); containers.add(anchor); PsiLambdaExpression container = PsiTreeUtil.getParentOfType(anchor, PsiLambdaExpression.class, true, PsiCodeBlock.class); if (container == null) return containers; Set<PsiElement> dependencies = PsiTreeUtil.collectElementsOfType(expr, PsiReferenceExpression.class) .stream() .map(ref -> ref.resolve()) .filter(PsiParameter.class::isInstance) .collect(Collectors.toSet()); while (container != null) { if (ContainerUtil.intersects(dependencies, Arrays.asList(container.getParameterList().getParameters()))) { break; } containers.add(container); container = PsiTreeUtil.getParentOfType(container, PsiLambdaExpression.class, true, PsiCodeBlock.class); } return containers; }