introduce variable inside lambda: do not step out of lambda block

This commit is contained in:
anna
2013-11-29 21:12:28 +01:00
parent 2fdb93a278
commit 2b49ac9eb4
5 changed files with 52 additions and 2 deletions
@@ -737,6 +737,7 @@ public abstract class IntroduceVariableBase extends IntroduceHandlerBase {
while (true) {
if (containerParent instanceof PsiFile) break;
if (containerParent instanceof PsiMethod) break;
if (containerParent instanceof PsiLambdaExpression) break;
if (!skipForStatement && containerParent instanceof PsiForStatement) break;
containerParent = containerParent.getParent();
if (containerParent instanceof PsiCodeBlock) {
@@ -29,8 +29,11 @@ public class JavaPsiEquivalenceUtil {
if (o1 instanceof PsiParameter && o2 instanceof PsiParameter) {
final PsiElement scope1 = ((PsiParameter)o1).getDeclarationScope();
final PsiElement scope2 = ((PsiParameter)o2).getDeclarationScope();
if (scope1 instanceof PsiMethod && scope2 instanceof PsiMethod && !scope1.getTextRange().intersects(scope2.getTextRange())) {
return ((PsiParameter)o1).getName().compareTo(((PsiParameter)o2).getName());
if (scope1 instanceof PsiMethod && scope2 instanceof PsiMethod ||
scope1 instanceof PsiLambdaExpression && scope2 instanceof PsiLambdaExpression) {
if (!scope1.getTextRange().intersects(scope2.getTextRange())) {
return ((PsiParameter)o1).getName().compareTo(((PsiParameter)o2).getName());
}
}
}
return 1;
@@ -0,0 +1,22 @@
import java.util.ArrayList;
import java.util.Collection;
public class ExtractVariableSample {
interface I {
void foo(String s);
}
public static void main(String[] args) {
Collection<String> strings = new ArrayList<>();
I i = (s) -> {
int j = s.hashCode();
System.out.println(j); };
for (String s : strings) {
System.out.println(s.hashCode());
}
}
}
@@ -0,0 +1,20 @@
import java.util.ArrayList;
import java.util.Collection;
public class ExtractVariableSample {
interface I {
void foo(String s);
}
public static void main(String[] args) {
Collection<String> strings = new ArrayList<>();
I i = (s) -> { System.out.println(<selection>s.hashCode()</selection>); };
for (String s : strings) {
System.out.println(s.hashCode());
}
}
}
@@ -90,6 +90,10 @@ public class IntroduceVariableTest extends LightCodeInsightTestCase {
doTest(new MockIntroduceVariableHandler("j", true, false, false, "int"));
}
public void testLambda() throws Exception {
doTest(new MockIntroduceVariableHandler("j", true, false, false, "int"));
}
public void testParenthized() throws Exception {
doTest(new MockIntroduceVariableHandler("temp", true, false, false, "int"));
}