Search in sources :

Example 1 with CandidateInfo

use of com.intellij.psi.infos.CandidateInfo in project intellij-community by JetBrains.

the class GroovyOverrideImplementExploreUtil method getMapToOverrideImplement.

@NotNull
public static Map<MethodSignature, CandidateInfo> getMapToOverrideImplement(GrTypeDefinition aClass, boolean toImplement, boolean skipImplemented) {
    Map<MethodSignature, PsiMethod> abstracts = ContainerUtil.newLinkedHashMap();
    Map<MethodSignature, PsiMethod> finals = ContainerUtil.newLinkedHashMap();
    Map<MethodSignature, PsiMethod> concretes = ContainerUtil.newLinkedHashMap();
    PsiUtilCore.ensureValid(aClass);
    Collection<HierarchicalMethodSignature> allMethodSignatures = aClass.getVisibleSignatures();
    PsiResolveHelper resolveHelper = JavaPsiFacade.getInstance(aClass.getProject()).getResolveHelper();
    for (HierarchicalMethodSignature signature : allMethodSignatures) {
        PsiMethod method = signature.getMethod();
        if (method instanceof GrTraitMethod) {
            for (HierarchicalMethodSignature superSignature : signature.getSuperSignatures()) {
                processMethod(aClass, skipImplemented, abstracts, finals, concretes, resolveHelper, superSignature, superSignature.getMethod());
            }
        } else {
            processMethod(aClass, skipImplemented, abstracts, finals, concretes, resolveHelper, signature, method);
        }
    }
    final Map<MethodSignature, CandidateInfo> result = new TreeMap<>(new OverrideImplementExploreUtil.MethodSignatureComparator());
    if (toImplement || aClass.isInterface()) {
        collectMethodsToImplement(aClass, abstracts, finals, concretes, result);
    } else {
        for (Map.Entry<MethodSignature, PsiMethod> entry : concretes.entrySet()) {
            MethodSignature signature = entry.getKey();
            PsiMethod concrete = entry.getValue();
            if (finals.get(signature) == null) {
                PsiMethod abstractOne = abstracts.get(signature);
                if (abstractOne == null || !abstractOne.getContainingClass().isInheritor(concrete.getContainingClass(), true) || CommonClassNames.JAVA_LANG_OBJECT.equals(concrete.getContainingClass().getQualifiedName())) {
                    PsiSubstitutor subst = OverrideImplementExploreUtil.correctSubstitutor(concrete, signature.getSubstitutor());
                    CandidateInfo info = new CandidateInfo(concrete, subst);
                    result.put(signature, info);
                }
            }
        }
    }
    return result;
}
Also used : MethodSignature(com.intellij.psi.util.MethodSignature) CandidateInfo(com.intellij.psi.infos.CandidateInfo) TreeMap(java.util.TreeMap) OverrideImplementExploreUtil(com.intellij.codeInsight.generation.OverrideImplementExploreUtil) GrTraitMethod(org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GrTraitMethod) TreeMap(java.util.TreeMap) Map(java.util.Map) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with CandidateInfo

use of com.intellij.psi.infos.CandidateInfo in project intellij-community by JetBrains.

the class ReplaceAbstractClassInstanceByMapIntention method processIntention.

@Override
protected void processIntention(@NotNull PsiElement psiElement, @NotNull Project project, Editor editor) throws IncorrectOperationException {
    PsiDocumentManager.getInstance(project).commitAllDocuments();
    GrCodeReferenceElement ref = (GrCodeReferenceElement) psiElement;
    final GrAnonymousClassDefinition anonymous = (GrAnonymousClassDefinition) ref.getParent();
    final GrNewExpression newExpr = (GrNewExpression) anonymous.getParent();
    final PsiElement resolved = ref.resolve();
    // && ((PsiClass)resolved).isInterface();
    assert resolved instanceof PsiClass;
    GrTypeDefinitionBody body = anonymous.getBody();
    assert body != null;
    List<Pair<PsiMethod, GrOpenBlock>> methods = new ArrayList<>();
    for (GrMethod method : body.getMethods()) {
        methods.add(new Pair<>(method, method.getBlock()));
    }
    final PsiClass iface = (PsiClass) resolved;
    final Collection<CandidateInfo> collection = OverrideImplementExploreUtil.getMethodsToOverrideImplement(anonymous, true);
    for (CandidateInfo info : collection) {
        methods.add(new Pair<>((PsiMethod) info.getElement(), null));
    }
    StringBuilder buffer = new StringBuilder();
    if (methods.size() == 1) {
        final Pair<PsiMethod, GrOpenBlock> pair = methods.get(0);
        appendClosureTextByMethod(pair.getFirst(), buffer, pair.getSecond(), newExpr);
        if (!GroovyConfigUtils.getInstance().isVersionAtLeast(psiElement, GroovyConfigUtils.GROOVY2_2)) {
            buffer.append(" as ").append(iface.getQualifiedName());
        }
    } else {
        buffer.append("[");
        buffer.append("\n");
        for (Pair<PsiMethod, GrOpenBlock> pair : methods) {
            final PsiMethod method = pair.getFirst();
            final GrOpenBlock block = pair.getSecond();
            buffer.append(method.getName()).append(": ");
            appendClosureTextByMethod(method, buffer, block, newExpr);
            buffer.append(",\n");
        }
        if (!methods.isEmpty()) {
            buffer.delete(buffer.length() - 2, buffer.length());
            buffer.append('\n');
        }
        buffer.append("]");
        buffer.append(" as ").append(iface.getQualifiedName());
    }
    createAndAdjustNewExpression(project, newExpr, buffer);
}
Also used : CandidateInfo(com.intellij.psi.infos.CandidateInfo) GrAnonymousClassDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrAnonymousClassDefinition) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrNewExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrNewExpression) GrTypeDefinitionBody(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinitionBody) GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement) GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) Pair(com.intellij.openapi.util.Pair)

Example 3 with CandidateInfo

use of com.intellij.psi.infos.CandidateInfo in project intellij-community by JetBrains.

the class GrMethodOverrideCompletionProvider method addSuperMethods.

private static void addSuperMethods(final GrTypeDefinition psiClass, CompletionResultSet completionResultSet, boolean toImplement) {
    final Collection<CandidateInfo> candidates = GroovyOverrideImplementExploreUtil.getMethodsToOverrideImplement(psiClass, toImplement);
    for (CandidateInfo candidateInfo : candidates) {
        final PsiMethod method = (PsiMethod) candidateInfo.getElement();
        if (method.isConstructor())
            continue;
        RowIcon icon = new RowIcon(2);
        icon.setIcon(method.getIcon(0), 0);
        icon.setIcon(toImplement ? AllIcons.Gutter.ImplementingMethod : AllIcons.Gutter.OverridingMethod, 1);
        PsiSubstitutor substitutor = candidateInfo.getSubstitutor();
        String parameters = PsiFormatUtil.formatMethod(method, substitutor, PsiFormatUtilBase.SHOW_PARAMETERS, PsiFormatUtilBase.SHOW_NAME);
        String visibility = VisibilityUtil.getVisibilityModifier(method.getModifierList());
        String modifiers = (visibility == PsiModifier.PACKAGE_LOCAL ? "" : visibility + " ");
        PsiType type = substitutor.substitute(method.getReturnType());
        String parentClassName = psiClass == null ? "" : psiClass.getName();
        String signature = modifiers + (type == null ? "" : type.getPresentableText() + " ") + method.getName();
        LookupElementBuilder lookupElement = LookupElementBuilder.create(method, signature).appendTailText(parameters, false).appendTailText("{...}", true).withTypeText(parentClassName).withIcon(icon).withInsertHandler(new GroovyMethodOverrideHandler(psiClass));
        completionResultSet.addElement(lookupElement);
    }
}
Also used : CandidateInfo(com.intellij.psi.infos.CandidateInfo) LookupElementBuilder(com.intellij.codeInsight.lookup.LookupElementBuilder) RowIcon(com.intellij.ui.RowIcon) GroovyMethodOverrideHandler(org.jetbrains.plugins.groovy.lang.completion.handlers.GroovyMethodOverrideHandler)

Example 4 with CandidateInfo

use of com.intellij.psi.infos.CandidateInfo in project intellij-community by JetBrains.

the class JavaGenerateMemberCompletionContributor method addSuperSignatureElements.

private static void addSuperSignatureElements(final PsiClass parent, boolean implemented, CompletionResultSet result, Set<MethodSignature> addedSignatures) {
    for (CandidateInfo candidate : OverrideImplementExploreUtil.getMethodsToOverrideImplement(parent, implemented)) {
        PsiMethod baseMethod = (PsiMethod) candidate.getElement();
        PsiClass baseClass = baseMethod.getContainingClass();
        PsiSubstitutor substitutor = candidate.getSubstitutor();
        if (!baseMethod.isConstructor() && baseClass != null && addedSignatures.add(baseMethod.getSignature(substitutor))) {
            result.addElement(createOverridingLookupElement(implemented, baseMethod, baseClass, substitutor));
        }
    }
}
Also used : CandidateInfo(com.intellij.psi.infos.CandidateInfo)

Example 5 with CandidateInfo

use of com.intellij.psi.infos.CandidateInfo 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)

Aggregations

CandidateInfo (com.intellij.psi.infos.CandidateInfo)60 MethodCandidateInfo (com.intellij.psi.infos.MethodCandidateInfo)19 NotNull (org.jetbrains.annotations.NotNull)13 Nullable (org.jetbrains.annotations.Nullable)6 Pair (com.intellij.openapi.util.Pair)4 ArrayList (java.util.ArrayList)4 HashSet (java.util.HashSet)4 GrModifierList (org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList)4 Project (com.intellij.openapi.project.Project)3 TextRange (com.intellij.openapi.util.TextRange)3 PsiClass (com.intellij.psi.PsiClass)3 PsiElement (com.intellij.psi.PsiElement)3 IncorrectOperationException (com.intellij.util.IncorrectOperationException)3 GrReferenceList (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrReferenceList)3 PsiMethodMember (com.intellij.codeInsight.generation.PsiMethodMember)2 Result (com.intellij.openapi.application.Result)2 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)2 LanguageLevel (com.intellij.pom.java.LanguageLevel)2 ClassCandidateInfo (com.intellij.psi.infos.ClassCandidateInfo)2 PsiScopeProcessor (com.intellij.psi.scope.PsiScopeProcessor)2