Search in sources :

Example 1 with JspxImportStatement

use of com.intellij.psi.impl.source.jsp.jspJava.JspxImportStatement in project intellij-community by JetBrains.

the class ImportHelper method addNamesToImport.

private static void addNamesToImport(@NotNull Set<Pair<String, Boolean>> names, @NotNull List<PsiElement> comments, @NotNull PsiElement scope, @NotNull String thisPackageName, PsiFile context) {
    if (scope instanceof PsiImportList)
        return;
    final LinkedList<PsiElement> stack = new LinkedList<>();
    stack.add(scope);
    while (!stack.isEmpty()) {
        final PsiElement child = stack.removeFirst();
        if (child instanceof PsiImportList) {
            for (PsiElement element : child.getChildren()) {
                if (element == null) {
                    continue;
                }
                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(stack, child.getChildren());
        for (final PsiReference reference : child.getReferences()) {
            if (!(reference instanceof PsiJavaReference))
                continue;
            final PsiJavaReference javaReference = (PsiJavaReference) reference;
            if (javaReference instanceof JavaClassReference && ((JavaClassReference) javaReference).getContextReference() != null)
                continue;
            PsiJavaCodeReferenceElement referenceElement = null;
            if (reference instanceof PsiJavaCodeReferenceElement) {
                referenceElement = (PsiJavaCodeReferenceElement) child;
                if (referenceElement.getQualifier() != null) {
                    continue;
                }
                if (reference instanceof PsiJavaCodeReferenceElementImpl && ((PsiJavaCodeReferenceElementImpl) reference).getKind(((PsiJavaCodeReferenceElementImpl) reference).getContainingFile()) == PsiJavaCodeReferenceElementImpl.CLASS_IN_QUALIFIED_NEW_KIND) {
                    continue;
                }
            }
            final JavaResolveResult resolveResult = javaReference.advancedResolve(true);
            PsiElement refElement = resolveResult.getElement();
            if (refElement == null && referenceElement != null) {
                // might be uncomplete code
                refElement = ResolveClassUtil.resolveClass(referenceElement, referenceElement.getContainingFile());
            }
            if (refElement == null)
                continue;
            PsiElement currentFileResolveScope = resolveResult.getCurrentFileResolveScope();
            if (!(currentFileResolveScope instanceof PsiImportStatementBase))
                continue;
            if (context != null && (!currentFileResolveScope.isValid() || currentFileResolveScope instanceof JspxImportStatement && context != ((JspxImportStatement) currentFileResolveScope).getDeclarationFile())) {
                continue;
            }
            if (referenceElement != null) {
                if (currentFileResolveScope instanceof PsiImportStaticStatement) {
                    PsiImportStaticStatement importStaticStatement = (PsiImportStaticStatement) currentFileResolveScope;
                    String name = importStaticStatement.getImportReference().getCanonicalText();
                    if (importStaticStatement.isOnDemand()) {
                        String refName = referenceElement.getReferenceName();
                        if (refName != null)
                            name = name + "." + refName;
                    }
                    names.add(Pair.create(name, Boolean.TRUE));
                    continue;
                }
            }
            if (refElement instanceof PsiClass) {
                String qName = ((PsiClass) refElement).getQualifiedName();
                if (hasPackage(qName, thisPackageName))
                    continue;
                names.add(Pair.create(qName, Boolean.FALSE));
            }
        }
    }
}
Also used : JspxImportStatement(com.intellij.psi.impl.source.jsp.jspJava.JspxImportStatement) JavaClassReference(com.intellij.psi.impl.source.resolve.reference.impl.providers.JavaClassReference) PsiJavaCodeReferenceElementImpl(com.intellij.psi.impl.source.PsiJavaCodeReferenceElementImpl) IElementType(com.intellij.psi.tree.IElementType) ASTNode(com.intellij.lang.ASTNode)

Aggregations

ASTNode (com.intellij.lang.ASTNode)1 PsiJavaCodeReferenceElementImpl (com.intellij.psi.impl.source.PsiJavaCodeReferenceElementImpl)1 JspxImportStatement (com.intellij.psi.impl.source.jsp.jspJava.JspxImportStatement)1 JavaClassReference (com.intellij.psi.impl.source.resolve.reference.impl.providers.JavaClassReference)1 IElementType (com.intellij.psi.tree.IElementType)1