Search in sources :

Example 21 with GrMethodCall

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall 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 22 with GrMethodCall

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

the class Dependency method getGroovyElements.

@Override
@NotNull
public List<PsiElement> getGroovyElements(@NotNull GroovyPsiElementFactory factory) {
    String extraGroovyCode;
    switch(type) {
        case EXTERNAL:
            if (extraClosure != null) {
                extraGroovyCode = "(" + escapeAndQuote(data) + ")";
            } else {
                extraGroovyCode = " " + escapeAndQuote(data);
            }
            break;
        case MODULE:
            if (data instanceof Map) {
                extraGroovyCode = " project(" + GradleGroovyFile.convertMapToGroovySource((Map<String, Object>) data) + ")";
            } else {
                extraGroovyCode = " project(" + escapeAndQuote(data) + ")";
            }
            if (extraClosure != null) {
                // If there's a closure with exclusion rules, then we need extra parentheses:
                // compile project(':foo') { ... } is not valid Groovy syntax
                // compile(project(':foo')) { ... } is correct
                extraGroovyCode = "(" + extraGroovyCode.substring(1) + ")";
            }
            break;
        case FILES:
            extraGroovyCode = " files(" + escapeAndQuote(data) + ")";
            break;
        case FILETREE:
            extraGroovyCode = " fileTree(" + GradleGroovyFile.convertMapToGroovySource((Map<String, Object>) data) + ")";
            break;
        default:
            extraGroovyCode = "";
            break;
    }
    GrStatement statement = factory.createStatementFromText(scope.getGroovyMethodCall() + extraGroovyCode);
    if (statement instanceof GrMethodCall && extraClosure != null) {
        statement.add(factory.createClosureFromText(extraClosure));
    }
    return ImmutableList.of((PsiElement) statement);
}
Also used : GrMethodCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall) BuildFileKey.escapeLiteralString(com.android.tools.idea.gradle.parser.BuildFileKey.escapeLiteralString) GrString(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrString) Map(java.util.Map) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement) NotNull(org.jetbrains.annotations.NotNull)

Example 23 with GrMethodCall

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

the class GradleBuildFile method getClosure.

/**
   * Given a path to a method, returns the first argument of that method that is a closure, or null.
   */
@Nullable
public GrStatementOwner getClosure(String path) {
    checkInitialized();
    GrMethodCall method = getMethodCallByPath(myGroovyFile, path);
    if (method == null) {
        return null;
    }
    return getMethodClosureArgument(method);
}
Also used : GrMethodCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall) Nullable(org.jetbrains.annotations.Nullable)

Example 24 with GrMethodCall

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

the class GradleBuildFile method removeValue.

/**
   * If the given key has a value at the given root, removes it and returns true. Returns false if there is no value for that key.
   */
public boolean removeValue(@Nullable GrStatementOwner root, @NotNull BuildFileKey key) {
    checkInitialized();
    commitDocumentChanges();
    if (root == null) {
        root = myGroovyFile;
    }
    GrMethodCall method = getMethodCallByPath(root, key.getPath());
    if (method != null) {
        GrStatementOwner parent = (GrStatementOwner) method.getParent();
        parent.removeElements(new PsiElement[] { method });
        reformatClosure(parent);
        return true;
    }
    return false;
}
Also used : GrMethodCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall) GrStatementOwner(org.jetbrains.plugins.groovy.lang.psi.api.util.GrStatementOwner)

Example 25 with GrMethodCall

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

the class GradleGroovyFile method getMethodCallByPath.

/**
   * Finds a method identified by the given path. It descends into nested closure arguments of method calls to find the leaf method.
   * For example, if you have this code in Groovy:
   *
   * method_a {
   *   method_b {
   *     method_c 'literal'
   *   }
   * }
   *
   * you can find method_c using the path "method_a/method_b/method_c"
   *
   * It returns the first eligible result. If you have code like this:
   *
   * method_a {
   *   method_b 'literal1'
   *   method_b 'literal2'
   * }
   *
   * a search for "method_a/method_b" will only return the first invocation of method_b.
   *
   * It continues searching until it has exhausted all possibilities of finding the path. If you have code like this:
   *
   * method_a {
   *   method_b 'literal1'
   * }
   *
   * method_a {
   *   method_c 'literal2'
   * }
   *
   * a search for "method_a/method_c" will succeed: it will not give up just because it doesn't find it in the first method_a block.
   *
   * @param root the block to use as the root of the path
   * @param path the slash-delimited chain of methods with closure arguments to navigate to find the final leaf.
   * @return the resultant method, or null if it could not be found.
   */
@Nullable
protected static GrMethodCall getMethodCallByPath(@NotNull GrStatementOwner root, @NotNull String path) {
    if (path.isEmpty() || path.endsWith("/")) {
        return null;
    }
    int slash = path.indexOf('/');
    String pathElement = slash == -1 ? path : path.substring(0, slash);
    for (GrMethodCall gmc : getMethodCalls(root, pathElement)) {
        if (slash == -1) {
            return gmc;
        }
        if (gmc == null) {
            return null;
        }
        GrClosableBlock[] blocks = gmc.getClosureArguments();
        if (blocks.length != 1) {
            return null;
        }
        GrMethodCall subresult = getMethodCallByPath(blocks[0], path.substring(slash + 1));
        if (subresult != null) {
            return subresult;
        }
    }
    return null;
}
Also used : GrMethodCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) BuildFileKey.escapeLiteralString(com.android.tools.idea.gradle.parser.BuildFileKey.escapeLiteralString) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

GrMethodCall (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall)51 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)23 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)22 Nullable (org.jetbrains.annotations.Nullable)18 PsiElement (com.intellij.psi.PsiElement)17 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)13 GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)9 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)8 NotNull (org.jetbrains.annotations.NotNull)7 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)4 LookupElement (com.intellij.codeInsight.lookup.LookupElement)3 Project (com.intellij.openapi.project.Project)3 VirtualFile (com.intellij.openapi.vfs.VirtualFile)3 PsiFile (com.intellij.psi.PsiFile)3 PsiMethod (com.intellij.psi.PsiMethod)3 PsiType (com.intellij.psi.PsiType)3 ArrayList (java.util.ArrayList)3 GroovyMapContentProvider (org.jetbrains.plugins.groovy.extensions.GroovyMapContentProvider)3 GroovyFile (org.jetbrains.plugins.groovy.lang.psi.GroovyFile)3 GrAssignmentExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression)3