Search in sources :

Example 1 with JavaClassReference

use of com.intellij.psi.impl.source.resolve.reference.impl.providers.JavaClassReference in project intellij-community by JetBrains.

the class CanonicalPsiTypeConverterImpl method getReferences.

public PsiReference[] getReferences(@Nullable PsiType type, String typeText, int startOffsetInText, @NotNull final PsiElement element) {
    final ElementManipulator<PsiElement> manipulator = ElementManipulators.getManipulator(element);
    assert manipulator != null;
    String trimmed = typeText.trim();
    int offset = manipulator.getRangeInElement(element).getStartOffset() + startOffsetInText + typeText.indexOf(trimmed);
    if (trimmed.startsWith(ARRAY_PREFIX)) {
        offset += ARRAY_PREFIX.length();
        if (trimmed.endsWith(";")) {
            trimmed = trimmed.substring(ARRAY_PREFIX.length(), trimmed.length() - 1);
        } else {
            trimmed = trimmed.substring(ARRAY_PREFIX.length());
        }
    }
    if (type != null) {
        type = type.getDeepComponentType();
    }
    final boolean isPrimitiveType = type instanceof PsiPrimitiveType;
    return new JavaClassReferenceSet(trimmed, element, offset, false, CLASS_REFERENCE_PROVIDER) {

        @Override
        @NotNull
        protected JavaClassReference createReference(int refIndex, @NotNull String subRefText, @NotNull TextRange textRange, boolean staticImport) {
            return new JavaClassReference(this, textRange, refIndex, subRefText, staticImport) {

                @Override
                public boolean isSoft() {
                    return true;
                }

                @Override
                @NotNull
                public JavaResolveResult advancedResolve(final boolean incompleteCode) {
                    if (isPrimitiveType) {
                        return new CandidateInfo(element, PsiSubstitutor.EMPTY, false, false, element);
                    }
                    return super.advancedResolve(incompleteCode);
                }

                @Override
                public void processVariants(@NotNull final PsiScopeProcessor processor) {
                    if (processor instanceof JavaCompletionProcessor) {
                        ((JavaCompletionProcessor) processor).setCompletionElements(getVariants());
                    } else {
                        super.processVariants(processor);
                    }
                }

                @Override
                @NotNull
                public Object[] getVariants() {
                    final Object[] variants = super.getVariants();
                    if (myIndex == 0) {
                        return ArrayUtil.mergeArrays(variants, PRIMITIVES, ArrayUtil.OBJECT_ARRAY_FACTORY);
                    }
                    return variants;
                }
            };
        }
    }.getAllReferences();
}
Also used : JavaClassReferenceSet(com.intellij.psi.impl.source.resolve.reference.impl.providers.JavaClassReferenceSet) JavaClassReference(com.intellij.psi.impl.source.resolve.reference.impl.providers.JavaClassReference) CandidateInfo(com.intellij.psi.infos.CandidateInfo) TextRange(com.intellij.openapi.util.TextRange) PsiScopeProcessor(com.intellij.psi.scope.PsiScopeProcessor) NotNull(org.jetbrains.annotations.NotNull) JavaCompletionProcessor(com.intellij.codeInsight.completion.scope.JavaCompletionProcessor)

Example 2 with JavaClassReference

use of com.intellij.psi.impl.source.resolve.reference.impl.providers.JavaClassReference in project intellij-community by JetBrains.

the class ExtendsClassChecker method checkExtendsClassInReferences.

public static List<DomElementProblemDescriptor> checkExtendsClassInReferences(final GenericDomValue element, final DomElementAnnotationHolder holder) {
    if (!isPsiClassType(element)) {
        return Collections.emptyList();
    }
    final Object valueObject = element.getValue();
    if (!(valueObject instanceof PsiClass))
        return Collections.emptyList();
    final XmlElement valueElement = DomUtil.getValueElement(element);
    if (valueElement == null)
        return Collections.emptyList();
    final PsiReference[] references = ourProvider.getReferencesByElement(valueElement, new ProcessingContext());
    for (PsiReference reference : references) {
        if (reference instanceof JavaClassReference) {
            final PsiReferenceProvider psiReferenceProvider = ((JavaClassReference) reference).getProvider();
            final String[] value = psiReferenceProvider instanceof JavaClassReferenceProvider ? JavaClassReferenceProvider.EXTEND_CLASS_NAMES.getValue(((JavaClassReferenceProvider) psiReferenceProvider).getOptions()) : null;
            if (value != null && value.length != 0) {
                for (String className : value) {
                    final List<DomElementProblemDescriptor> problemDescriptors = checkExtendClass(element, ((PsiClass) valueObject), className, false, false, true, false, true, true, holder);
                    if (!problemDescriptors.isEmpty()) {
                        return problemDescriptors;
                    }
                }
            }
        }
    }
    return Collections.emptyList();
}
Also used : ProcessingContext(com.intellij.util.ProcessingContext) JavaClassReference(com.intellij.psi.impl.source.resolve.reference.impl.providers.JavaClassReference) JavaClassReferenceProvider(com.intellij.psi.impl.source.resolve.reference.impl.providers.JavaClassReferenceProvider) DomElementProblemDescriptor(com.intellij.util.xml.highlighting.DomElementProblemDescriptor) XmlElement(com.intellij.psi.xml.XmlElement)

Example 3 with JavaClassReference

use of com.intellij.psi.impl.source.resolve.reference.impl.providers.JavaClassReference in project intellij-community by JetBrains.

the class JavaClassReferenceCompletionContributor method fillCompletionVariants.

@Override
public void fillCompletionVariants(@NotNull CompletionParameters parameters, @NotNull CompletionResultSet result) {
    PsiElement position = parameters.getPosition();
    JavaClassReference reference = findJavaClassReference(position.getContainingFile(), parameters.getOffset());
    if (reference == null) {
        return;
    }
    String[] extendClassNames = reference.getExtendClassNames();
    PsiElement context = reference.getCompletionContext();
    if (extendClassNames != null && context instanceof PsiPackage) {
        if (parameters.getCompletionType() == CompletionType.SMART) {
            JavaClassReferenceSet set = reference.getJavaClassReferenceSet();
            int setStart = set.getRangeInElement().getStartOffset() + set.getElement().getTextRange().getStartOffset();
            String fullPrefix = parameters.getPosition().getContainingFile().getText().substring(setStart, parameters.getOffset());
            reference.processSubclassVariants((PsiPackage) context, extendClassNames, result.withPrefixMatcher(fullPrefix));
            return;
        }
        result.addLookupAdvertisement("Press " + getActionShortcut(IdeActions.ACTION_SMART_TYPE_COMPLETION) + " to see inheritors of " + StringUtil.join(extendClassNames, ", "));
    }
    if (parameters.getCompletionType() == CompletionType.SMART) {
        return;
    }
    if (parameters.isExtendedCompletion() || parameters.getCompletionType() == CompletionType.CLASS_NAME) {
        JavaClassNameCompletionContributor.addAllClasses(parameters, result);
    } else {
        LegacyCompletionContributor.completeReference(parameters, result);
    }
    result.stopHere();
}
Also used : JavaClassReferenceSet(com.intellij.psi.impl.source.resolve.reference.impl.providers.JavaClassReferenceSet) JavaClassReference(com.intellij.psi.impl.source.resolve.reference.impl.providers.JavaClassReference) PsiPackage(com.intellij.psi.PsiPackage) PsiElement(com.intellij.psi.PsiElement)

Example 4 with JavaClassReference

use of com.intellij.psi.impl.source.resolve.reference.impl.providers.JavaClassReference in project intellij-community by JetBrains.

the class JavaClassReferenceCompletionContributor method duringCompletion.

@Override
public void duringCompletion(@NotNull CompletionInitializationContext context) {
    JavaClassReference reference = findJavaClassReference(context.getFile(), context.getStartOffset());
    if (reference != null && reference.getExtendClassNames() != null) {
        JavaClassReferenceSet set = reference.getJavaClassReferenceSet();
        context.setReplacementOffset(set.getRangeInElement().getEndOffset() + set.getElement().getTextRange().getStartOffset());
    }
}
Also used : JavaClassReferenceSet(com.intellij.psi.impl.source.resolve.reference.impl.providers.JavaClassReferenceSet) JavaClassReference(com.intellij.psi.impl.source.resolve.reference.impl.providers.JavaClassReference)

Example 5 with JavaClassReference

use of com.intellij.psi.impl.source.resolve.reference.impl.providers.JavaClassReference 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

JavaClassReference (com.intellij.psi.impl.source.resolve.reference.impl.providers.JavaClassReference)6 JavaClassReferenceSet (com.intellij.psi.impl.source.resolve.reference.impl.providers.JavaClassReferenceSet)3 PsiElement (com.intellij.psi.PsiElement)2 PsiPackage (com.intellij.psi.PsiPackage)2 NotNull (org.jetbrains.annotations.NotNull)2 JavaCompletionProcessor (com.intellij.codeInsight.completion.scope.JavaCompletionProcessor)1 ASTNode (com.intellij.lang.ASTNode)1 TextRange (com.intellij.openapi.util.TextRange)1 PsiReference (com.intellij.psi.PsiReference)1 PsiJavaCodeReferenceElementImpl (com.intellij.psi.impl.source.PsiJavaCodeReferenceElementImpl)1 JspxImportStatement (com.intellij.psi.impl.source.jsp.jspJava.JspxImportStatement)1 JavaClassReferenceProvider (com.intellij.psi.impl.source.resolve.reference.impl.providers.JavaClassReferenceProvider)1 CandidateInfo (com.intellij.psi.infos.CandidateInfo)1 PsiScopeProcessor (com.intellij.psi.scope.PsiScopeProcessor)1 IElementType (com.intellij.psi.tree.IElementType)1 XmlElement (com.intellij.psi.xml.XmlElement)1 ProcessingContext (com.intellij.util.ProcessingContext)1 DomElementProblemDescriptor (com.intellij.util.xml.highlighting.DomElementProblemDescriptor)1