import org.jetbrains.annotations.Nullable;
import org.jetbrains.plugins.groovy.dsl.GdslMembersHolderConsumer;
import org.jetbrains.plugins.groovy.dsl.holders.DelegatedMembersHolder;
-import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression;
+import org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList;
+import org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock;
+import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrApplicationStatement;
+import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCall;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression;
-import org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock;
+import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression;
/**
* @author ilyas
* Returns enclosing method call of a given context's place
*/
@Nullable
- public GrMethodCallExpression enclosingCall(String name, GdslMembersHolderConsumer consumer) {
+ public GrCall enclosingCall(String name, GdslMembersHolderConsumer consumer) {
final PsiElement place = consumer.getPlace();
if (place == null) return null;
- GrMethodCallExpression call = PsiTreeUtil.getParentOfType(place, GrMethodCallExpression.class, true);
+ GrCall call = PsiTreeUtil.getParentOfType(place, GrCall.class, true);
if (call == null) return null;
while (call != null && !name.equals(getInvokedMethodName(call))) {
- call = PsiTreeUtil.getParentOfType(call, GrMethodCallExpression.class, true);
+ call = PsiTreeUtil.getParentOfType(call, GrCall.class, true);
}
if (call == null) return null;
- for (GrExpression arg : call.getClosureArguments()) {
- if (arg instanceof GrClosableBlock && PsiTreeUtil.findCommonParent(place, arg) == arg) {
- return call;
+
+ final GrArgumentList argumentList = call.getArgumentList();
+ if (argumentList != null) {
+ for (GrExpression arg : argumentList.getExpressionArguments()) {
+ if (arg instanceof GrClosableBlock && PsiTreeUtil.findCommonParent(place, arg) == arg) {
+ return call;
+ }
}
}
- for (GrExpression arg : call.getExpressionArguments()) {
- if (arg instanceof GrClosableBlock && PsiTreeUtil.findCommonParent(place, arg) == arg) {
- return call;
+
+ if (call instanceof GrMethodCallExpression) {
+ for (GrExpression arg : ((GrMethodCallExpression)call).getClosureArguments()) {
+ if (arg instanceof GrClosableBlock && PsiTreeUtil.findCommonParent(place, arg) == arg) {
+ return call;
+ }
}
}
return null;
return PsiTreeUtil.getParentOfType(place, PsiClass.class, true);
}
- private static String getInvokedMethodName(GrMethodCallExpression call) {
- final GrExpression expr = call.getInvokedExpression();
+ @Nullable
+ private static String getInvokedMethodName(GrCall call) {
+ final GrExpression expr = call instanceof GrApplicationStatement ? ((GrApplicationStatement)call).getFunExpression() :
+ call instanceof GrMethodCallExpression ? ((GrMethodCallExpression)call).getInvokedExpression() : null;
if (expr instanceof GrReferenceExpression) {
- GrReferenceExpression ref = (GrReferenceExpression)expr;
- return ref.getName();
+ return ((GrReferenceExpression)expr).getName();
}
return null;
}
import org.jetbrains.annotations.Nullable;
import org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap;
import org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotationArrayInitializer;
+import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrApplicationStatement;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression;
@Nullable
public static PsiElement bind(PsiElement element) {
- PsiElement elem = element instanceof GrMethodCallExpression ? ((GrMethodCallExpression)element).getInvokedExpression() : element;
+ PsiElement elem = element instanceof GrMethodCallExpression ? ((GrMethodCallExpression)element).getInvokedExpression() :
+ element instanceof GrApplicationStatement ? ((GrApplicationStatement)element).getFunExpression() :
+ element;
final PsiReference ref = elem.getReference();
- if (ref == null) {
- return null;
- }
- else {
- return ref.resolve();
- }
+ return ref == null ? null : ref.resolve();
}
@Nullable
package org.jetbrains.plugins.groovy.dsl.psi;
-import com.intellij.psi.*;
+import com.intellij.psi.PsiClass;
+import com.intellij.psi.PsiType;
import org.jetbrains.annotations.Nullable;
+import org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList;
+import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCall;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression;
-import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression;
-import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
+import java.util.Collections;
/**
* @author ilyas
*/
+@SuppressWarnings({"UnusedDeclaration"})
public class PsiExpressionCategory implements PsiEnhancerCategory{
@Nullable
}
/**
- * Returns arguments of a call expression
* @param call
- * @return
+ * @return arguments
*/
- public static Collection<GrExpression> getArguments(GrExpression call) {
- if (call instanceof GrMethodCallExpression) {
- return Arrays.asList(((GrMethodCallExpression)call).getExpressionArguments());
+ public static Collection<GrExpression> getArguments(GrCall call) {
+ final GrArgumentList argumentList = call.getArgumentList();
+ if (argumentList != null) {
+ return Arrays.asList(argumentList.getExpressionArguments());
}
- return new ArrayList<GrExpression>();
+ return Collections.emptyList();
}
}
import org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase
import org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList
import org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock
+import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCall
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression
-import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression
import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition
import org.jetbrains.plugins.groovy.lang.psi.patterns.GroovyPatterns
if (parent instanceof GrArgumentList) {
parent = parent.parent
}
- return parent instanceof GrMethodCallExpression
+ return parent instanceof GrCall
}
}
})
""")
}
+
+ public void testDelegateToArgument2() throws Throwable {
+ doCustomTest("""
+ def ctx = context(scope: closureScope(isArgument: true))
+
+ contributor(ctx, {
+ def call = enclosingCall("boo")
+ if (call) {
+ delegatesTo(call.arguments[0]?.classType)
+ }
+ })
+""")
+ }
public void testClassContext() throws Throwable {
doCustomTest( """
--- /dev/null
+class MyDelegate {
+ def saySomething(String str) {
+ println str
+ }
+}
+
+class Runner {
+ def boo(c, Closure cl) { }
+}
+
+def runner = new Runner()
+runner.boo new MyDelegate(), {
+ say<caret>
+}
\ No newline at end of file
--- /dev/null
+class MyDelegate {
+ def saySomething(String str) {
+ println str
+ }
+}
+
+class Runner {
+ def boo(c, Closure cl) { }
+}
+
+def runner = new Runner()
+runner.boo new MyDelegate(), {
+ saySomething <caret>
+}
\ No newline at end of file