Search in sources :

Example 1 with ClosureDescriptor

use of org.jetbrains.plugins.groovy.lang.completion.closureParameters.ClosureDescriptor in project intellij-community by JetBrains.

the class GdslClosureCompleter method getParameterInfos.

@Override
protected List<ClosureParameterInfo> getParameterInfos(InsertionContext context, PsiMethod method, PsiSubstitutor substitutor, PsiElement place) {
    final ArrayList<ClosureDescriptor> descriptors = new ArrayList<>();
    GrReferenceExpression ref = (GrReferenceExpression) place;
    PsiType qtype = PsiImplUtil.getQualifierType(ref);
    if (qtype == null)
        return null;
    GrExpression qualifier = ref.getQualifier();
    if (qualifier != null) {
        PsiType type = qualifier.getType();
        if (type == null)
            return null;
        processExecutors(qtype, ref, descriptors);
    } else {
        PsiElementFactory factory = JavaPsiFacade.getElementFactory(context.getProject());
        for (PsiElement parent = ref.getParent(); parent != null; parent = parent.getParent()) {
            if (parent instanceof GrClosableBlock) {
                processExecutors(TypesUtil.createTypeByFQClassName(GroovyCommonClassNames.GROOVY_LANG_CLOSURE, ref), ref, descriptors);
            } else if (parent instanceof GrTypeDefinition) {
                processExecutors(factory.createType(((GrTypeDefinition) parent), PsiType.EMPTY_ARRAY), ref, descriptors);
            }
        }
    }
    for (ClosureDescriptor descriptor : descriptors) {
        if (descriptor.isMethodApplicable(method, ref)) {
            return descriptor.getParameters();
        }
    }
    return null;
}
Also used : GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) ArrayList(java.util.ArrayList) ClosureDescriptor(org.jetbrains.plugins.groovy.lang.completion.closureParameters.ClosureDescriptor) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Example 2 with ClosureDescriptor

use of org.jetbrains.plugins.groovy.lang.completion.closureParameters.ClosureDescriptor in project intellij-community by JetBrains.

the class NonCodeMembersHolder method createClosureDescriptor.

@Nullable
private static PsiElement createClosureDescriptor(Map prop, PsiElement place, PsiManager manager) {
    final ClosureDescriptor closure = new ClosureDescriptor(manager);
    final Object method = prop.get("method");
    if (!(method instanceof Map))
        return null;
    closure.setMethod(((Map) method));
    //    closure.setReturnType(convertToPsiType(String.valueOf(prop.get("type")), place));
    final Object closureParams = prop.get("params");
    if (closureParams instanceof Map) {
        boolean first = true;
        for (Object paramName : ((Map) closureParams).keySet()) {
            Object value = ((Map) closureParams).get(paramName);
            boolean isNamed = first && value instanceof List;
            first = false;
            String typeName = isNamed ? CommonClassNames.JAVA_UTIL_MAP : String.valueOf(value);
            closure.addParameter(typeName, String.valueOf(paramName));
        }
    }
    Object doc = prop.get("doc");
    if (doc instanceof String) {
        closure.putUserData(DOCUMENTATION, (String) doc);
    }
    Object docUrl = prop.get("docUrl");
    if (docUrl instanceof String) {
        closure.putUserData(DOCUMENTATION_URL, (String) docUrl);
    }
    return closure;
}
Also used : ClosureDescriptor(org.jetbrains.plugins.groovy.lang.completion.closureParameters.ClosureDescriptor) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

ArrayList (java.util.ArrayList)2 ClosureDescriptor (org.jetbrains.plugins.groovy.lang.completion.closureParameters.ClosureDescriptor)2 List (java.util.List)1 Map (java.util.Map)1 Nullable (org.jetbrains.annotations.Nullable)1 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)1 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)1 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)1 GrTypeDefinition (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition)1