Search in sources :

Example 1 with JavaCompletionProcessor

use of com.intellij.codeInsight.completion.scope.JavaCompletionProcessor in project intellij-community by JetBrains.

the class JavaDocCompletionContributor method completeJavadocReference.

@NotNull
private List<LookupElement> completeJavadocReference(PsiElement position, PsiJavaReference ref) {
    JavaCompletionProcessor processor = new JavaCompletionProcessor(position, TrueFilter.INSTANCE, JavaCompletionProcessor.Options.CHECK_NOTHING, Conditions.alwaysTrue());
    ref.processVariants(processor);
    return ContainerUtil.map(processor.getResults(), (completionResult) -> {
        LookupElement item = createReferenceLookupItem(completionResult.getElement());
        item.putUserData(JavaCompletionUtil.FORCE_SHOW_SIGNATURE_ATTR, Boolean.TRUE);
        return item;
    });
}
Also used : JavaCompletionProcessor(com.intellij.codeInsight.completion.scope.JavaCompletionProcessor) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with JavaCompletionProcessor

use of com.intellij.codeInsight.completion.scope.JavaCompletionProcessor in project intellij-community by JetBrains.

the class SuperCalls method suggestQualifyingSuperCalls.

static Set<LookupElement> suggestQualifyingSuperCalls(PsiElement element, PsiJavaReference javaReference, ElementFilter elementFilter, JavaCompletionProcessor.Options options, Condition<String> nameCondition) {
    Set<LookupElement> set = ContainerUtil.newLinkedHashSet();
    for (final String className : getContainingClassNames(element)) {
        PsiReferenceExpression fakeSuper = JavaCompletionUtil.createReference(className + ".super.rulez", element);
        PsiElement leaf = ObjectUtils.assertNotNull(fakeSuper.getReferenceNameElement());
        JavaCompletionProcessor superProcessor = new JavaCompletionProcessor(leaf, elementFilter, options, nameCondition);
        fakeSuper.processVariants(superProcessor);
        for (CompletionElement completionElement : superProcessor.getResults()) {
            for (LookupElement item : JavaCompletionUtil.createLookupElements(completionElement, javaReference)) {
                set.add(withQualifiedSuper(className, item));
            }
        }
    }
    return set;
}
Also used : LookupElement(com.intellij.codeInsight.lookup.LookupElement) JavaCompletionProcessor(com.intellij.codeInsight.completion.scope.JavaCompletionProcessor) CompletionElement(com.intellij.codeInsight.completion.scope.CompletionElement)

Example 3 with JavaCompletionProcessor

use of com.intellij.codeInsight.completion.scope.JavaCompletionProcessor 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 4 with JavaCompletionProcessor

use of com.intellij.codeInsight.completion.scope.JavaCompletionProcessor in project intellij-community by JetBrains.

the class JavaClassReference method processVariants.

@Override
public void processVariants(@NotNull final PsiScopeProcessor processor) {
    if (processor instanceof JavaCompletionProcessor) {
        final Map<CustomizableReferenceProvider.CustomizationKey, Object> options = getOptions();
        if (options != null && (JavaClassReferenceProvider.EXTEND_CLASS_NAMES.getValue(options) != null || JavaClassReferenceProvider.NOT_INTERFACE.getBooleanValue(options) || JavaClassReferenceProvider.CONCRETE.getBooleanValue(options)) || JavaClassReferenceProvider.CLASS_KIND.getValue(options) != null) {
            ((JavaCompletionProcessor) processor).setCompletionElements(getVariants());
            return;
        }
    }
    PsiScopeProcessor processorToUse = processor;
    if (myInStaticImport) {
        // allows to complete members
        processor.handleEvent(JavaScopeProcessorEvent.CHANGE_LEVEL, null);
    } else {
        if (isDefinitelyStatic()) {
            processor.handleEvent(JavaScopeProcessorEvent.START_STATIC, null);
        }
        processorToUse = new PsiScopeProcessor() {

            @Override
            public boolean execute(@NotNull PsiElement element, @NotNull ResolveState state) {
                return !(element instanceof PsiClass || element instanceof PsiPackage) || processor.execute(element, state);
            }

            @Override
            public <V> V getHint(@NotNull Key<V> hintKey) {
                return processor.getHint(hintKey);
            }

            @Override
            public void handleEvent(@NotNull Event event, Object associated) {
                processor.handleEvent(event, associated);
            }
        };
    }
    super.processVariants(processorToUse);
}
Also used : PsiScopeProcessor(com.intellij.psi.scope.PsiScopeProcessor) JavaScopeProcessorEvent(com.intellij.psi.scope.JavaScopeProcessorEvent) JavaCompletionProcessor(com.intellij.codeInsight.completion.scope.JavaCompletionProcessor)

Example 5 with JavaCompletionProcessor

use of com.intellij.codeInsight.completion.scope.JavaCompletionProcessor in project intellij-community by JetBrains.

the class JavaCompletionUtil method processJavaQualifiedReference.

private static Set<LookupElement> processJavaQualifiedReference(PsiElement element, PsiJavaReference javaReference, ElementFilter elementFilter, JavaCompletionProcessor.Options options, final PrefixMatcher matcher, CompletionParameters parameters) {
    final Set<LookupElement> set = new LinkedHashSet<>();
    final Condition<String> nameCondition = matcher::prefixMatches;
    final JavaCompletionProcessor processor = new JavaCompletionProcessor(element, elementFilter, options, nameCondition);
    final PsiType plainQualifier = processor.getQualifierType();
    PsiType qualifierType = plainQualifier;
    PsiType runtimeQualifier = getQualifierCastType(javaReference, parameters);
    if (runtimeQualifier != null) {
        PsiType composite = qualifierType == null ? runtimeQualifier : PsiIntersectionType.createIntersection(qualifierType, runtimeQualifier);
        PsiElement ctx = createContextWithXxxVariable(element, composite);
        javaReference = createReference("xxx.xxx", ctx);
        qualifierType = runtimeQualifier;
        processor.setQualifierType(qualifierType);
    }
    javaReference.processVariants(processor);
    final PsiTypeLookupItem castItem = runtimeQualifier == null ? null : PsiTypeLookupItem.createLookupItem(runtimeQualifier, (PsiReferenceExpression) javaReference);
    final boolean pkgContext = inSomePackage(element);
    PsiClass qualifierClass = PsiUtil.resolveClassInClassTypeOnly(qualifierType);
    final boolean honorExcludes = qualifierClass == null || !isInExcludedPackage(qualifierClass, false);
    final Set<PsiMember> mentioned = new THashSet<>();
    for (CompletionElement completionElement : processor.getResults()) {
        for (LookupElement item : createLookupElements(completionElement, javaReference)) {
            item.putUserData(QUALIFIER_TYPE_ATTR, qualifierType);
            final Object o = item.getObject();
            if (o instanceof PsiClass && !isSourceLevelAccessible(element, (PsiClass) o, pkgContext)) {
                continue;
            }
            if (o instanceof PsiMember) {
                if (honorExcludes && isInExcludedPackage((PsiMember) o, true)) {
                    continue;
                }
                mentioned.add(CompletionUtil.getOriginalOrSelf((PsiMember) o));
            }
            set.add(highlightIfNeeded(qualifierType, castQualifier(item, castItem, plainQualifier, processor), o, element));
        }
    }
    if (javaReference instanceof PsiJavaCodeReferenceElement) {
        PsiElement refQualifier = ((PsiJavaCodeReferenceElement) javaReference).getQualifier();
        if (refQualifier == null) {
            final StaticMemberProcessor memberProcessor = new JavaStaticMemberProcessor(parameters);
            memberProcessor.processMembersOfRegisteredClasses(matcher, (member, psiClass) -> {
                if (!mentioned.contains(member) && processor.satisfies(member, ResolveState.initial())) {
                    set.add(memberProcessor.createLookupElement(member, psiClass, true));
                }
            });
        } else if (refQualifier instanceof PsiSuperExpression && ((PsiSuperExpression) refQualifier).getQualifier() == null) {
            set.addAll(SuperCalls.suggestQualifyingSuperCalls(element, javaReference, elementFilter, options, nameCondition));
        }
    }
    return set;
}
Also used : THashSet(gnu.trove.THashSet) CompletionElement(com.intellij.codeInsight.completion.scope.CompletionElement) JavaCompletionProcessor(com.intellij.codeInsight.completion.scope.JavaCompletionProcessor) FakePsiElement(com.intellij.psi.impl.FakePsiElement)

Aggregations

JavaCompletionProcessor (com.intellij.codeInsight.completion.scope.JavaCompletionProcessor)5 CompletionElement (com.intellij.codeInsight.completion.scope.CompletionElement)2 PsiScopeProcessor (com.intellij.psi.scope.PsiScopeProcessor)2 NotNull (org.jetbrains.annotations.NotNull)2 LookupElement (com.intellij.codeInsight.lookup.LookupElement)1 TextRange (com.intellij.openapi.util.TextRange)1 FakePsiElement (com.intellij.psi.impl.FakePsiElement)1 JavaClassReference (com.intellij.psi.impl.source.resolve.reference.impl.providers.JavaClassReference)1 JavaClassReferenceSet (com.intellij.psi.impl.source.resolve.reference.impl.providers.JavaClassReferenceSet)1 CandidateInfo (com.intellij.psi.infos.CandidateInfo)1 JavaScopeProcessorEvent (com.intellij.psi.scope.JavaScopeProcessorEvent)1 THashSet (gnu.trove.THashSet)1