List<PsiElement> (@NotNull Collection<PsiElement> elements, @NotNull ScopeOwner owner) { final List<PsiElement> result = new ArrayList<>(); for (PsiElement element : elements) { final PsiReference reference = element.getReference(); if (reference != null) { final PsiElement resolved = reference.resolve(); if (resolved != null && ScopeUtil.getScopeOwner(resolved) == owner && !(owner instanceof PsiFile)) { result.add(element); } } } return result; }