Search in sources :

Example 1 with TypeCondition

use of org.jetbrains.plugins.groovy.extensions.impl.TypeCondition in project intellij-community by JetBrains.

the class GroovyConstructorNamedArgumentProvider method processClass.

public static void processClass(@NotNull GrCall call, PsiClassType type, @Nullable String argumentName, final Map<String, NamedArgumentDescriptor> result) {
    if (argumentName == null) {
        final HashMap<String, Trinity<PsiType, PsiElement, PsiSubstitutor>> map = ContainerUtil.newHashMap();
        MyPsiScopeProcessor processor = new MyPsiScopeProcessor() {

            @Override
            protected void addNamedArgument(String propertyName, PsiType type, PsiElement element, PsiSubstitutor substitutor) {
                if (result.containsKey(propertyName))
                    return;
                Trinity<PsiType, PsiElement, PsiSubstitutor> pair = map.get(propertyName);
                if (pair != null) {
                    if (element instanceof PsiMethod && pair.second instanceof PsiField) {
                    // methods should override fields
                    } else {
                        return;
                    }
                }
                map.put(propertyName, Trinity.create(type, element, substitutor));
            }
        };
        processor.setResolveTargetKinds(ClassHint.RESOLVE_KINDS_METHOD_PROPERTY);
        ResolveUtil.processAllDeclarations(type, processor, ResolveState.initial(), call);
        for (Map.Entry<String, Trinity<PsiType, PsiElement, PsiSubstitutor>> entry : map.entrySet()) {
            result.put(entry.getKey(), new TypeCondition(entry.getValue().first, entry.getValue().getSecond(), entry.getValue().getThird(), NamedArgumentDescriptor.Priority.AS_LOCAL_VARIABLE));
        }
    } else {
        MyPsiScopeProcessor processor = new MyPsiScopeProcessor() {

            @Override
            protected void addNamedArgument(String propertyName, PsiType type, PsiElement element, PsiSubstitutor substitutor) {
                if (result.containsKey(propertyName))
                    return;
                result.put(propertyName, new TypeCondition(type, element, substitutor, NamedArgumentDescriptor.Priority.AS_LOCAL_VARIABLE));
            }
        };
        processor.setResolveTargetKinds(ClassHint.RESOLVE_KINDS_METHOD);
        processor.setNameHint(GroovyPropertyUtils.getSetterName(argumentName));
        ResolveUtil.processAllDeclarations(type, processor, ResolveState.initial(), call);
        processor.setResolveTargetKinds(ClassHint.RESOLVE_KINDS_PROPERTY);
        processor.setNameHint(argumentName);
        ResolveUtil.processAllDeclarations(type, processor, ResolveState.initial(), call);
    }
}
Also used : Trinity(com.intellij.openapi.util.Trinity) TypeCondition(org.jetbrains.plugins.groovy.extensions.impl.TypeCondition) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with TypeCondition

use of org.jetbrains.plugins.groovy.extensions.impl.TypeCondition in project intellij-community by JetBrains.

the class SwingBuilderNamedArgumentProvider method getNamedArguments.

@Override
public void getNamedArguments(@NotNull GrCall call, @NotNull GroovyResolveResult resolveResult, @Nullable String argumentName, boolean forCompletion, @NotNull Map<String, NamedArgumentDescriptor> result) {
    PsiElement resolve = resolveResult.getElement();
    PsiType returnType = resolve == null ? null : ((PsiMethod) resolve).getReturnType();
    PsiClass aClass = PsiTypesUtil.getPsiClass(returnType);
    if (aClass == null)
        return;
    Map<String, Pair<PsiType, PsiElement>> typeMap = null;
    if (!forCompletion) {
        typeMap = new HashMap<>();
    }
    PsiManager manager = aClass.getManager();
    for (PsiMethod method : aClass.getAllMethods()) {
        String methodName = method.getName();
        String propertyName = GroovyPropertyUtils.getPropertyNameBySetterName(methodName);
        if (propertyName != null) {
            if (argumentName != null && !argumentName.equals(propertyName))
                continue;
            PsiType methodReturnType = method.getReturnType();
            if (methodReturnType != null && !PsiType.VOID.equals(methodReturnType))
                continue;
            PsiParameter[] parameters = method.getParameterList().getParameters();
            if (parameters.length != 1)
                continue;
            if (forCompletion) {
                // optimization, don't calculate types.
                result.put(propertyName, NamedArgumentDescriptor.SIMPLE_ON_TOP);
            } else {
                PsiType newType = parameters[0].getType();
                Pair<PsiType, PsiElement> oldPair = typeMap.get(propertyName);
                if (oldPair == null) {
                    typeMap.put(propertyName, new Pair<>(newType, method));
                } else {
                    PsiType type = TypesUtil.getLeastUpperBound(oldPair.first, newType, manager);
                    if (type == null) {
                        type = PsiType.getJavaLangObject(manager, aClass.getResolveScope());
                    }
                    typeMap.put(propertyName, new Pair<>(newType, null));
                }
            }
        } else {
            PsiType closureType = null;
            if (methodName.startsWith("add")) {
                PsiParameter[] parameters = method.getParameterList().getParameters();
                if (parameters.length != 1)
                    continue;
                PsiClass paramClass = PsiTypesUtil.getPsiClass(parameters[0].getType());
                if (paramClass == null || !InheritanceUtil.isInheritor(paramClass, "java.util.EventListener"))
                    continue;
                for (PsiMethod psiMethod : paramClass.getMethods()) {
                    if (!psiMethod.hasModifierProperty(PsiModifier.ABSTRACT))
                        continue;
                    if (forCompletion) {
                        result.put(psiMethod.getName(), NamedArgumentDescriptor.SIMPLE_ON_TOP);
                    } else {
                        if (closureType == null) {
                            closureType = JavaPsiFacade.getElementFactory(manager.getProject()).createTypeByFQClassName(GroovyCommonClassNames.GROOVY_LANG_CLOSURE, call.getResolveScope());
                        }
                        result.put(psiMethod.getName(), new TypeCondition(closureType, method));
                    }
                }
            }
        }
    }
    if (!forCompletion) {
        for (Map.Entry<String, Pair<PsiType, PsiElement>> entry : typeMap.entrySet()) {
            result.put(entry.getKey(), new TypeCondition(entry.getValue().first, entry.getValue().second));
        }
    }
}
Also used : TypeCondition(org.jetbrains.plugins.groovy.extensions.impl.TypeCondition) HashMap(java.util.HashMap) Map(java.util.Map) Pair(com.intellij.openapi.util.Pair)

Aggregations

HashMap (java.util.HashMap)2 Map (java.util.Map)2 TypeCondition (org.jetbrains.plugins.groovy.extensions.impl.TypeCondition)2 Pair (com.intellij.openapi.util.Pair)1 Trinity (com.intellij.openapi.util.Trinity)1