lambda->anonym: preserve param names (IDEA-90718)

This commit is contained in:
Anna Kozlova
2012-08-28 18:21:06 +04:00
parent 9f79dfea45
commit cb9e2f3c3c
4 changed files with 68 additions and 0 deletions
@@ -51,6 +51,7 @@ public class ReplaceLambdaWithAnonymousIntention extends Intention {
protected void processIntention(Editor editor, @NotNull PsiElement element) throws IncorrectOperationException {
final PsiLambdaExpression lambdaExpression = PsiTreeUtil.getParentOfType(element, PsiLambdaExpression.class);
LOG.assertTrue(lambdaExpression != null);
final PsiParameter[] paramListCopy = ((PsiParameterList)lambdaExpression.getParameterList().copy()).getParameters();
PsiType functionalInterfaceType = lambdaExpression.getFunctionalInterfaceType();
LOG.assertTrue(functionalInterfaceType != null);
functionalInterfaceType = GenericsUtil.eliminateWildcards(functionalInterfaceType);
@@ -69,6 +70,14 @@ public class ReplaceLambdaWithAnonymousIntention extends Intention {
final List<PsiGenerationInfo<PsiMethod>> infos = OverrideImplementUtil.overrideOrImplement(anonymousClass, method);
if (infos != null && infos.size() == 1) {
final PsiMethod member = infos.get(0).getPsiMember();
final PsiParameter[] parameters = member.getParameterList().getParameters();
for (int i = 0; i < parameters.length; i++) {
final PsiParameter parameter = parameters[i];
final String lambdaParamName = paramListCopy[i].getName();
if (lambdaParamName != null) {
parameter.setName(lambdaParamName);
}
}
PsiCodeBlock codeBlock = member.getBody();
LOG.assertTrue(codeBlock != null);
codeBlock = (PsiCodeBlock)codeBlock.replace(psiElementFactory.createCodeBlockFromText(blockText, null));
@@ -0,0 +1,26 @@
/*
* Copyright 2000-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
class Test {
interface Some {
void test(int p1, int p2);
}
{
Some some = (i1,<caret> i2) -> {
System.out.println(i1 + i2);
};
}
}
@@ -0,0 +1,29 @@
/*
* Copyright 2000-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
class Test {
interface Some {
void test(int p1, int p2);
}
{
Some some = new Some() {
@Override
public void test(int i1, int i2) {
<selection>System.out.println(i1 + i2);</selection>
}
};
}
}
@@ -31,6 +31,10 @@ public class ReplaceLambdaWithAnonymousIntentionTest extends IPPTestCase {
doTest();
}
public void testRenameParams() {
doTest();
}
public void testInsertFinal() {
doTest();
}