IDEA-56126 Groovy: "Convert parameter to map entry" intention applied to closure doesn't change its arguments in explicit call

This commit is contained in:
Maxim Medvedev
2010-07-20 12:26:03 +04:00
parent 6815d02c65
commit 0d689b8f58
4 changed files with 65 additions and 46 deletions
@@ -185,42 +185,53 @@ public class ConvertParameterToMapEntryIntention extends Intention {
//final List<GrCall> 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<PsiElement>[] argInfos =
GrClosureSignatureUtil.mapParametersToArguments(signature, argumentList, closureArguments, owner.getResolveScope());
if (argInfos == null) continue;
final GrClosureSignatureUtil.ArgInfo<PsiElement> 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<PsiElement, String>() {
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<PsiElement>[] argInfos =
GrClosureSignatureUtil.mapParametersToArguments(signature, argumentList, closureArguments, owner.getResolveScope());
if (argInfos == null) continue;
final GrClosureSignatureUtil.ArgInfo<PsiElement> 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<PsiElement, String>() {
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<Boolean> result = new Ref<Boolean>(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<PsiReference> query = ReferencesSearch.search(namedElem, projectScope);
@@ -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();
@@ -0,0 +1,3 @@
def clos = { int <caret>i -> println(i) }
clos(1)
clos.call(1)
+3
View File
@@ -0,0 +1,3 @@
def clos = { Map attrs -> println(attrs.i) }
clos(i: 1)
clos.call(i: 1)