From 2c24297f73f0d12241fc00dc32cb372d1f44548d Mon Sep 17 00:00:00 2001 From: "Maxim.Medvedev" Date: Tue, 19 Jun 2012 15:38:21 +0400 Subject: [PATCH] IDEA-87424 Smart completion in default constructor invocation --- .../GroovyExpectedTypesProvider.java | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/expectedTypes/GroovyExpectedTypesProvider.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/expectedTypes/GroovyExpectedTypesProvider.java index 56411b427de7..fc7041dc1a03 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/expectedTypes/GroovyExpectedTypesProvider.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/expectedTypes/GroovyExpectedTypesProvider.java @@ -30,6 +30,7 @@ import org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementVisitor; import org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult; import org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap; import org.jetbrains.plugins.groovy.lang.psi.api.statements.*; +import org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentLabel; import org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList; import org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument; import org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock; @@ -44,9 +45,11 @@ import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrM import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod; import org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement; import org.jetbrains.plugins.groovy.lang.psi.controlFlow.Instruction; +import org.jetbrains.plugins.groovy.lang.psi.impl.PsiImplUtil; import org.jetbrains.plugins.groovy.lang.psi.impl.signatures.GrClosureSignatureUtil; import org.jetbrains.plugins.groovy.lang.psi.impl.statements.expressions.TypesUtil; import org.jetbrains.plugins.groovy.lang.psi.util.GroovyCommonClassNames; +import org.jetbrains.plugins.groovy.lang.psi.util.GroovyPropertyUtils; import org.jetbrains.plugins.groovy.lang.psi.util.PsiUtil; import org.jetbrains.plugins.groovy.lang.resolve.ResolveUtil; @@ -156,6 +159,37 @@ public class GroovyExpectedTypesProvider { } } + @Override + public void visitNamedArgument(GrNamedArgument argument) { + PsiElement pparent = argument.getParent().getParent(); + if (pparent instanceof GrCall && resolvesToDefaultConstructor(((GrCall)pparent))) { + GrArgumentLabel label = argument.getLabel(); + if (label != null) { + PsiElement resolved = label.resolve(); + if (resolved instanceof PsiField) { + PsiType type = ((PsiField)resolved).getType(); + myResult = new TypeConstraint[]{new SubtypeConstraint(type, type)}; + } + else if (resolved instanceof PsiMethod && GroovyPropertyUtils.isSimplePropertySetter((PsiMethod)resolved)) { + PsiType type = ((PsiMethod)resolved).getParameterList().getParameters()[0].getType(); + myResult = new TypeConstraint[]{new SubtypeConstraint(type,type)}; + } + } + } + } + + private static boolean resolvesToDefaultConstructor(GrCall call) { + PsiMethod method = call.resolveMethod(); + if (method != null && method.isConstructor() && method.getParameterList().getParametersCount() == 0) return true; + + if (call instanceof GrConstructorCall) { + PsiElement resolved = PsiImplUtil.extractUniqueResult(((GrConstructorCall)call).multiResolveClass()).getElement(); + if (resolved instanceof PsiClass) return true; + } + + return false; + } + public void visitMethodCallExpression(GrMethodCallExpression methodCall) { final GrExpression invokedExpression = methodCall.getInvokedExpression(); if (myExpression.equals(invokedExpression)) {