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;
}
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);
}
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);
}
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;
}
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;
}
Aggregations