private static void addNamesToImport(@NotNull Set<? super Import> imports,
                                       @NotNull List<? super PsiElement> comments,
                                       @NotNull PsiElement scope,
                                       @NotNull String thisPackageName,
                                       @Nullable PsiFile context){
    if (scope instanceof PsiImportList) return;

    Queue<PsiElement> queue = new ArrayDeque<>();
    queue.add(scope);
    while (!queue.isEmpty()) {
      PsiElement child = queue.remove();
      if (child instanceof PsiImportList) {
        for(PsiElement element = child.getFirstChild(); element != null; element = element.getNextSibling()) {
          ASTNode node = element.getNode();
          if (node == null) {
            continue;
          }
          IElementType elementType = node.getElementType();
          if (!ElementType.IMPORT_STATEMENT_BASE_BIT_SET.contains(elementType) &&
              !JavaJspElementType.WHITE_SPACE_BIT_SET.contains(elementType)) {
            comments.add(element);
          }
        }
        continue;
      }
      if (child instanceof PsiLiteralExpression) continue;
      ContainerUtil.addAll(queue, child.getChildren());

      for (PsiReference reference : child.getReferences()) {
        PsiJavaCodeReferenceElement referenceElement = null;
        if (reference instanceof PsiJavaReference javaReference) {
          if (javaReference instanceof JavaClassReference classReference && classReference.getContextReference() != null) continue;
          if (reference instanceof PsiJavaCodeReferenceElement) {
            referenceElement = (PsiJavaCodeReferenceElement)child;
            if (referenceElement.getQualifier() != null) {
              continue;
            }
            if (reference instanceof PsiJavaCodeReferenceElementImpl refImpl
                && refImpl.getKindEnum(refImpl.getContainingFile()) == PsiJavaCodeReferenceElementImpl.Kind.CLASS_IN_QUALIFIED_NEW_KIND) {
              continue;
            }
          }
        }

        JavaResolveResult resolveResult = HighlightVisitorImpl.resolveJavaReference(reference);
        if (resolveResult == null) continue;
        PsiElement refElement = resolveResult.getElement();

        PsiElement currentFileResolveScope = resolveResult.getCurrentFileResolveScope();
        if (!(currentFileResolveScope instanceof PsiImportStatementBase) && refElement != null) continue;
        if (context != null &&
            refElement != null &&
            (!currentFileResolveScope.isValid() ||
             currentFileResolveScope instanceof JspxImportStatement jspxImportStatement &&
             context != jspxImportStatement.getDeclarationFile())) {
          continue;
        }

        if (refElement == null && referenceElement != null) {
          refElement = ResolveClassUtil.resolveClass(referenceElement, referenceElement.getContainingFile()); // might be incomplete code
        }
        if (refElement == null) continue;

        if (referenceElement != null && currentFileResolveScope instanceof PsiImportStaticStatement importStaticStatement) {
          String name = importStaticStatement.getImportReference().getCanonicalText();
          if (importStaticStatement.isOnDemand()) {
            String refName = referenceElement.getReferenceName();
            if (refName != null) name = name + "." + refName;
          }
          imports.add(new Import(name, true));
          continue;
        }

        if (refElement instanceof PsiClass psiClass) {
          String qName = psiClass.getQualifiedName();
          if (qName == null || hasPackage(qName, thisPackageName)) continue;
          imports.add(new Import(qName, false));
        }
      }
    }
  }
private static void addNamesToImport(@NotNull Set<? super Import> imports,
                                       @NotNull List<? super PsiElement> comments,
                                       @NotNull PsiElement scope,
                                       @NotNull String thisPackageName,
                                       @Nullable PsiFile context)