From 0d689b8f58406736b15e2acf4ecc24731c5ef592 Mon Sep 17 00:00:00 2001 From: Maxim Medvedev Date: Tue, 20 Jul 2010 12:26:03 +0400 Subject: [PATCH] IDEA-56126 Groovy: "Convert parameter to map entry" intention applied to closure doesn't change its arguments in explicit call --- .../ConvertParameterToMapEntryIntention.java | 83 ++++++++++--------- .../ParameterToMapEntryTest.java | 22 +++-- .../testdata/paramToMap/callMethod/A.groovy | 3 + .../testdata/paramToMap/callMethod/A.test | 3 + 4 files changed, 65 insertions(+), 46 deletions(-) create mode 100644 plugins/groovy/testdata/paramToMap/callMethod/A.groovy create mode 100644 plugins/groovy/testdata/paramToMap/callMethod/A.test diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/intentions/style/parameterToEntry/ConvertParameterToMapEntryIntention.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/intentions/style/parameterToEntry/ConvertParameterToMapEntryIntention.java index 3ce5f8148c7d..494bdb90eb55 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/intentions/style/parameterToEntry/ConvertParameterToMapEntryIntention.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/intentions/style/parameterToEntry/ConvertParameterToMapEntryIntention.java @@ -185,42 +185,53 @@ public class ConvertParameterToMapEntryIntention extends Intention { //final List calls = getCallOccurrences(occurrences); try { for (PsiElement occurrence : occurrences) { - if (occurrence instanceof GrReferenceExpression && occurrence.getParent() instanceof GrCall) { - final GrReferenceExpression refExpr = (GrReferenceExpression)occurrence; - final GrClosureSignature signature = generateSignature(owner, refExpr); - if (signature == null) continue; - final GrCall call = (GrCall)refExpr.getParent(); - final GrArgumentList argumentList = call.getArgumentList(); - final GrClosableBlock[] closureArguments = - call instanceof GrCallExpression ? ((GrCallExpression)call).getClosureArguments() : GrClosableBlock.EMPTY_ARRAY; - - final GrClosureSignatureUtil.ArgInfo[] argInfos = - GrClosureSignatureUtil.mapParametersToArguments(signature, argumentList, closureArguments, owner.getResolveScope()); - if (argInfos == null) continue; - final GrClosureSignatureUtil.ArgInfo argInfo = argInfos[index]; - - final GrNamedArgument namedArg; - if (argInfo.isMultiArg) { - if (argInfo.args.size() == 0) continue; - String arg = "[" + StringUtil.join(ContainerUtil.map(argInfo.args, new Function() { - public String fun(PsiElement element) { - return element.getText(); - } - }), ", ") + "]"; - for (PsiElement psiElement : argInfo.args) { - psiElement.delete(); + GrReferenceExpression refExpr = null; + if (occurrence instanceof GrReferenceExpression) { + final PsiElement parent = occurrence.getParent(); + if (parent instanceof GrCall) { + refExpr = (GrReferenceExpression)occurrence; + } + else if (parent instanceof GrReferenceExpression) { + final PsiElement resolved = ((GrReferenceExpression)parent).resolve(); + if (resolved instanceof PsiMethod && "call".equals(((PsiMethod)resolved).getName())) { + refExpr = (GrReferenceExpression)parent; } - namedArg = factory.createNamedArgument(paramName, factory.createExpressionFromText(arg)); } - else { - if (argInfo.args.size() == 0) continue; - final PsiElement argument = argInfo.args.iterator().next(); - assert argument instanceof GrExpression; - namedArg = factory.createNamedArgument(paramName, (GrExpression)argument); - argument.delete(); - } - ((GrCall)refExpr.getParent()).addNamedArgument(namedArg); } + if (refExpr == null) continue; + final GrClosureSignature signature = generateSignature(owner, refExpr); + if (signature == null) continue; + final GrCall call = (GrCall)refExpr.getParent(); + final GrArgumentList argumentList = call.getArgumentList(); + final GrClosableBlock[] closureArguments = + call instanceof GrCallExpression ? ((GrCallExpression)call).getClosureArguments() : GrClosableBlock.EMPTY_ARRAY; + + final GrClosureSignatureUtil.ArgInfo[] argInfos = + GrClosureSignatureUtil.mapParametersToArguments(signature, argumentList, closureArguments, owner.getResolveScope()); + if (argInfos == null) continue; + final GrClosureSignatureUtil.ArgInfo argInfo = argInfos[index]; + + final GrNamedArgument namedArg; + if (argInfo.isMultiArg) { + if (argInfo.args.size() == 0) continue; + String arg = "[" + StringUtil.join(ContainerUtil.map(argInfo.args, new Function() { + public String fun(PsiElement element) { + return element.getText(); + } + }), ", ") + "]"; + for (PsiElement psiElement : argInfo.args) { + psiElement.delete(); + } + namedArg = factory.createNamedArgument(paramName, factory.createExpressionFromText(arg)); + } + else { + if (argInfo.args.size() == 0) continue; + final PsiElement argument = argInfo.args.iterator().next(); + assert argument instanceof GrExpression; + namedArg = factory.createNamedArgument(paramName, (GrExpression)argument); + argument.delete(); + } + ((GrCall)refExpr.getParent()).addNamedArgument(namedArg); } } catch (IncorrectOperationException e) { @@ -369,10 +380,8 @@ public class ConvertParameterToMapEntryIntention extends Intention { final PsiElement namedElem = getReferencedElement(owner); if (namedElem == null) return true; final Ref result = new Ref(true); - final Task task = new Task.Modal(project, GroovyIntentionsBundle.message("find.method.ro.closure.usages.0", - owner instanceof GrClosableBlock - ? CLOSURE_CAPTION - : METHOD_CAPTION), true) { + final Task task = new Task.Modal(project, GroovyIntentionsBundle + .message("find.method.ro.closure.usages.0", owner instanceof GrClosableBlock ? CLOSURE_CAPTION : METHOD_CAPTION), true) { public void run(@NotNull final ProgressIndicator indicator) { final GlobalSearchScope projectScope = GlobalSearchScope.projectScope(getProject()); final Query query = ReferencesSearch.search(namedElem, projectScope); diff --git a/plugins/groovy/test/org/jetbrains/plugins/groovy/intentions/style/parameterToEntry/ParameterToMapEntryTest.java b/plugins/groovy/test/org/jetbrains/plugins/groovy/intentions/style/parameterToEntry/ParameterToMapEntryTest.java index f8692a9da4c8..8fa073f22a6f 100644 --- a/plugins/groovy/test/org/jetbrains/plugins/groovy/intentions/style/parameterToEntry/ParameterToMapEntryTest.java +++ b/plugins/groovy/test/org/jetbrains/plugins/groovy/intentions/style/parameterToEntry/ParameterToMapEntryTest.java @@ -56,39 +56,43 @@ public class ParameterToMapEntryTest extends GroovyFormatterTestCase { } */ - public void testParam1() throws Throwable { + public void testParam1() { doTestImpl("A.groovy"); } - public void testFormatter() throws Throwable { + public void testFormatter() { doTestImpl("A.groovy"); } - public void testClosureAtEnd() throws Throwable { + public void testClosureAtEnd() { doTestImpl("A.groovy"); } - public void testClosure1() throws Throwable { + public void testClosure1() { doTestImpl("A.groovy"); } - public void testNewMap() throws Throwable { + public void testNewMap() { doTestImpl("A.groovy"); } - public void testTestError() throws Throwable { + public void testTestError() { doTestImpl("A.groovy"); } - public void testSecondClosure() throws Throwable { + public void testSecondClosure() { doTestImpl("A.groovy"); } - public void testVarArgs() throws Throwable { + public void testVarArgs() { doTestImpl("A.groovy"); } - private void doTestImpl(String filePath) throws Throwable { + public void testCallMethod() { + doTestImpl("A.groovy"); + } + + private void doTestImpl(String filePath) { myFixture.configureByFile(filePath); int offset = myFixture.getEditor().getCaretModel().getOffset(); final PsiFile file = myFixture.getFile(); diff --git a/plugins/groovy/testdata/paramToMap/callMethod/A.groovy b/plugins/groovy/testdata/paramToMap/callMethod/A.groovy new file mode 100644 index 000000000000..23d1c8b94211 --- /dev/null +++ b/plugins/groovy/testdata/paramToMap/callMethod/A.groovy @@ -0,0 +1,3 @@ +def clos = { int i -> println(i) } +clos(1) +clos.call(1) \ No newline at end of file diff --git a/plugins/groovy/testdata/paramToMap/callMethod/A.test b/plugins/groovy/testdata/paramToMap/callMethod/A.test new file mode 100644 index 000000000000..0b7f54921e34 --- /dev/null +++ b/plugins/groovy/testdata/paramToMap/callMethod/A.test @@ -0,0 +1,3 @@ +def clos = { Map attrs -> println(attrs.i) } +clos(i: 1) +clos.call(i: 1)