Search in sources :

Example 31 with GrArgumentList

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList in project intellij-community by JetBrains.

the class PsiUtil method getArgumentIndex.

public static int getArgumentIndex(@NotNull GrCall call, @NotNull PsiElement argument) {
    GrArgumentList argumentList = call.getArgumentList();
    if (argumentList == null)
        return -1;
    GrExpression[] expressionArguments = argumentList.getExpressionArguments();
    for (int i = 0; i < expressionArguments.length; i++) {
        if (argument.equals(expressionArguments[i])) {
            int res = i;
            if (argumentList.getNamedArguments().length > 0) {
                // first argument is map defined by named arguments
                res++;
            }
            return res;
        }
    }
    if (argument instanceof GrClosableBlock) {
        GrClosableBlock[] closureArgs = call.getClosureArguments();
        for (int i = 0; i < closureArgs.length; i++) {
            if (argument.equals(closureArgs[i])) {
                int res = i + expressionArguments.length;
                if (argumentList.getNamedArguments().length > 0) {
                    // first argument is map defined by named arguments
                    res++;
                }
                return res;
            }
        }
    }
    return -1;
}
Also used : GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)

Example 32 with GrArgumentList

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList in project intellij-community by JetBrains.

the class PluginXmlClosureMemberContributor method processMembers.

@Override
public boolean processMembers(GrClosableBlock closure, PsiScopeProcessor processor, GrReferenceExpression refExpr, ResolveState state) {
    PsiElement parent = closure.getParent();
    if (parent instanceof GrArgumentList)
        parent = parent.getParent();
    if (!(parent instanceof GrMethodCall))
        return true;
    PsiMethod psiMethod = ((GrMethodCall) parent).resolveMethod();
    if (psiMethod == null)
        return true;
    List<GroovyMethodInfo> infos = GroovyMethodInfo.getInfos(psiMethod);
    if (infos.isEmpty())
        return true;
    int index = PsiUtil.getArgumentIndex((GrMethodCall) parent, closure);
    if (index == -1)
        return true;
    for (GroovyMethodInfo info : infos) {
        GroovyMethodDescriptor.ClosureArgument[] closureArguments = info.getDescriptor().myClosureArguments;
        if (closureArguments != null) {
            for (GroovyMethodDescriptor.ClosureArgument argument : closureArguments) {
                if (argument.index == index && argument.methodContributor != null) {
                    ClosureMissingMethodContributor instance = SingletonInstancesCache.getInstance(argument.methodContributor, info.getPluginClassLoader());
                    if (!instance.processMembers(closure, processor, refExpr, state))
                        return false;
                }
            }
        }
    }
    return true;
}
Also used : GrMethodCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) PsiMethod(com.intellij.psi.PsiMethod) GroovyMethodDescriptor(org.jetbrains.plugins.groovy.extensions.GroovyMethodDescriptor) PsiElement(com.intellij.psi.PsiElement) GroovyMethodInfo(org.jetbrains.plugins.groovy.extensions.GroovyMethodInfo)

Example 33 with GrArgumentList

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList in project intellij-community by JetBrains.

the class GroovyLanguageInjectionSupport method doInject.

private static boolean doInject(@NotNull String languageId, @NotNull PsiElement psiElement, @NotNull PsiLanguageInjectionHost host) {
    final PsiElement target = getTopLevelInjectionTarget(psiElement);
    final PsiElement parent = target.getParent();
    final Project project = psiElement.getProject();
    if (parent instanceof GrReturnStatement) {
        final GrControlFlowOwner owner = ControlFlowUtils.findControlFlowOwner(parent);
        if (owner instanceof GrOpenBlock && owner.getParent() instanceof GrMethod) {
            return JavaLanguageInjectionSupport.doInjectInJavaMethod(project, (PsiMethod) owner.getParent(), -1, host, languageId);
        }
    } else if (parent instanceof GrMethod) {
        return JavaLanguageInjectionSupport.doInjectInJavaMethod(project, (GrMethod) parent, -1, host, languageId);
    } else if (parent instanceof GrAnnotationNameValuePair) {
        final PsiReference ref = parent.getReference();
        if (ref != null) {
            final PsiElement resolved = ref.resolve();
            if (resolved instanceof PsiMethod) {
                return JavaLanguageInjectionSupport.doInjectInJavaMethod(project, (PsiMethod) resolved, -1, host, languageId);
            }
        }
    } else if (parent instanceof GrArgumentList && parent.getParent() instanceof GrMethodCall) {
        final PsiMethod method = ((GrMethodCall) parent.getParent()).resolveMethod();
        if (method != null) {
            final int index = GrInjectionUtil.findParameterIndex(target, ((GrMethodCall) parent.getParent()));
            if (index >= 0) {
                return JavaLanguageInjectionSupport.doInjectInJavaMethod(project, method, index, host, languageId);
            }
        }
    } else if (parent instanceof GrAssignmentExpression) {
        final GrExpression expr = ((GrAssignmentExpression) parent).getLValue();
        if (expr instanceof GrReferenceExpression) {
            final PsiElement element = ((GrReferenceExpression) expr).resolve();
            if (element != null) {
                return doInject(languageId, element, host);
            }
        }
    } else {
        if (parent instanceof PsiVariable) {
            Processor<PsiLanguageInjectionHost> fixer = getAnnotationFixer(project, languageId);
            if (JavaLanguageInjectionSupport.doAddLanguageAnnotation(project, (PsiModifierListOwner) parent, host, languageId, fixer))
                return true;
        } else if (target instanceof PsiVariable && !(target instanceof LightElement)) {
            Processor<PsiLanguageInjectionHost> fixer = getAnnotationFixer(project, languageId);
            if (JavaLanguageInjectionSupport.doAddLanguageAnnotation(project, (PsiModifierListOwner) target, host, languageId, fixer))
                return true;
        }
    }
    return false;
}
Also used : Processor(com.intellij.util.Processor) GrAnnotationNameValuePair(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotationNameValuePair) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrReturnStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement) LightElement(com.intellij.psi.impl.light.LightElement) Project(com.intellij.openapi.project.Project) GrControlFlowOwner(org.jetbrains.plugins.groovy.lang.psi.GrControlFlowOwner) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

Example 34 with GrArgumentList

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList in project android by JetBrains.

the class GradleDslParser method getExpressionElement.

@Nullable
private static GradleDslExpression getExpressionElement(@NotNull GradleDslElement parentElement, @NotNull GroovyPsiElement psiElement, @NotNull String propertyName, @NotNull GrExpression propertyExpression) {
    if (propertyExpression instanceof GrLiteral) {
        // ex: compileSdkVersion 23 or compileSdkVersion = "android-23"
        return new GradleDslLiteral(parentElement, psiElement, propertyName, (GrLiteral) propertyExpression);
    }
    if (propertyExpression instanceof GrReferenceExpression) {
        // ex: compileSdkVersion SDK_VERSION or sourceCompatibility = VERSION_1_5
        return new GradleDslReference(parentElement, psiElement, propertyName, (GrReferenceExpression) propertyExpression);
    }
    if (propertyExpression instanceof GrMethodCallExpression) {
        // ex: compile project("someProject")
        GrMethodCallExpression methodCall = (GrMethodCallExpression) propertyExpression;
        GrReferenceExpression callReferenceExpression = getChildOfType(methodCall, GrReferenceExpression.class);
        if (callReferenceExpression != null) {
            String referenceName = callReferenceExpression.getText();
            if (!isEmpty(referenceName)) {
                GrArgumentList argumentList = methodCall.getArgumentList();
                if (argumentList.getAllArguments().length > 0) {
                    return getMethodCall(parentElement, methodCall, referenceName, argumentList);
                } else {
                    return new GradleDslMethodCall(parentElement, methodCall, referenceName);
                }
            }
        }
    }
    if (propertyExpression instanceof GrNewExpression) {
        GrNewExpression newExpression = (GrNewExpression) propertyExpression;
        GrCodeReferenceElement referenceElement = newExpression.getReferenceElement();
        if (referenceElement != null) {
            String objectName = referenceElement.getText();
            if (!isEmpty(objectName)) {
                GrArgumentList argumentList = newExpression.getArgumentList();
                if (argumentList != null) {
                    if (argumentList.getAllArguments().length > 0) {
                        return getNewExpression(parentElement, newExpression, objectName, argumentList);
                    }
                }
            }
        }
    }
    return null;
}
Also used : GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement) GrMethodCallExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) GrLiteral(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral) Nullable(org.jetbrains.annotations.Nullable)

Example 35 with GrArgumentList

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList in project android by JetBrains.

the class GradleDslParser method parse.

private static boolean parse(@NotNull GrMethodCallExpression expression, @NotNull GradlePropertiesDslElement dslElement) {
    GrReferenceExpression referenceExpression = findChildOfType(expression, GrReferenceExpression.class);
    if (referenceExpression == null) {
        return false;
    }
    String name = referenceExpression.getText();
    if (isEmpty(name)) {
        return false;
    }
    List<String> nameSegments = Splitter.on('.').splitToList(name);
    if (nameSegments.size() > 1) {
        dslElement = getBlockElement(nameSegments.subList(0, (nameSegments.size() - 1)), dslElement);
        name = nameSegments.get(nameSegments.size() - 1);
    }
    if (dslElement == null) {
        return false;
    }
    GrClosableBlock[] closureArguments = expression.getClosureArguments();
    GrArgumentList argumentList = expression.getArgumentList();
    if (argumentList.getAllArguments().length > 0) {
        // This element is a method call with arguments and an optional closure associated with it.
        // ex: compile("dependency") {}
        GradleDslExpression methodCall = getMethodCall(dslElement, expression, name, argumentList);
        if (closureArguments.length > 0) {
            methodCall.setParsedClosureElement(getClosureElement(methodCall, closureArguments[0], name));
        }
        dslElement.addParsedElement(name, methodCall);
        return true;
    }
    if (argumentList.getAllArguments().length == 0 && closureArguments.length == 0) {
        // This element is a pure method call, i.e a method call with no arguments and no closure arguments.
        // ex: jcenter()
        dslElement.addParsedElement(name, new GradleDslMethodCall(dslElement, expression, name));
        return true;
    }
    // Now this element is pure block element, i.e a method call with no argument but just a closure argument. So, here just process the
    // closure and treat it as a block element.
    // ex: android {}
    GrClosableBlock closableBlock = closureArguments[0];
    // The block elements this closure needs to be applied.
    List<GradlePropertiesDslElement> blockElements = Lists.newArrayList();
    if (dslElement instanceof GradleDslFile && name.equals("allprojects")) {
        // The "allprojects" closure needs to be applied to this project and all it's sub projects.
        blockElements.add(dslElement);
        // After applying the allprojects closure to this project, process it as subprojects section to also pass the same properties to
        // subprojects.
        name = "subprojects";
    }
    GradlePropertiesDslElement blockElement = getBlockElement(ImmutableList.of(name), dslElement);
    if (blockElement != null) {
        blockElement.setPsiElement(closableBlock);
        blockElements.add(blockElement);
    }
    if (blockElements.isEmpty()) {
        return false;
    }
    for (GradlePropertiesDslElement element : blockElements) {
        parse(closableBlock, element);
    }
    return true;
}
Also used : GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)

Aggregations

GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)80 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)26 PsiElement (com.intellij.psi.PsiElement)21 Nullable (org.jetbrains.annotations.Nullable)20 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)18 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)15 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)14 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)14 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)14 GrMethodCallExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression)14 GrNamedArgument (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument)12 GrListOrMap (org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap)9 GrCall (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCall)8 GrMethodCall (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall)8 NotNull (org.jetbrains.annotations.NotNull)7 GrCommandArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCommandArgumentList)6 GrLiteral (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral)6 GrGdkMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrGdkMethod)6 GrCodeReferenceElement (org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement)6 ASTNode (com.intellij.lang.ASTNode)5