public static @NotNull <T extends PsiElement> List<T> searchElementsInSelection(@Nullable PsiFile file,
                                                                         @NotNull TextRange selection,
                                                                         @NotNull Class<T> filter, boolean searchChildrenOfFound) {
    if (file == null || file instanceof PsiCompiledElement) {
      return Collections.emptyList();
    }
    final List<T> results = new ArrayList<>();

    final PsiElementVisitor visitor = new JavaRecursiveElementWalkingVisitor() {
      @Override
      public void visitElement(@NotNull PsiElement element) {
        if (!selection.intersects(element.getTextRange())) {
          return;
        }
        if (filter.isInstance(element)) {
          results.add(filter.cast(element));
          if (!searchChildrenOfFound) {
            return;
          }
        }
        super.visitElement(element);
      }
    };

    file.accept(visitor);
    return results;
  }
public static @NotNull <T extends PsiElement> List<T> searchElementsInSelection(@Nullable PsiFile file,
                                                                         @NotNull TextRange selection,
                                                                         @NotNull Class<T> filter, boolean searchChildrenOfFound)