public static @NotNull PsiElement @NotNull [] filterAncestors(@NotNull PsiElement @NotNull [] elements) {
    List<PsiElement> filteredElements = new ArrayList<>();
    ContainerUtil.addAll(filteredElements, elements);

    int previousSize;
    do {
      previousSize = filteredElements.size();
      outer:
      for (int i = 0; i < filteredElements.size(); i++) {
        PsiElement element = filteredElements.get(i);
        for (int j = 0; j < filteredElements.size(); j++) {
          PsiElement element2 = filteredElements.get(j);
          if (i == j) continue;
          if (isAncestor(element, element2, false)) {
            filteredElements.remove(element2);
            break outer;
          }
        }
      }
    }
    while (filteredElements.size() != previousSize);
    return PsiUtilCore.toPsiElementArray(filteredElements);
  }
public static @NotNull PsiElement @NotNull [] filterAncestors(@NotNull PsiElement @NotNull [] elements)