mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
introduce variable: distinguish lambda parameters but allow param free expressions (IDEA-163785)
This commit is contained in:
+21
-17
@@ -750,29 +750,33 @@ public abstract class IntroduceVariableBase extends IntroduceHandlerBase {
|
||||
}
|
||||
|
||||
private static ExpressionOccurrenceManager createOccurrenceManager(PsiExpression expr, PsiElement tempContainer) {
|
||||
boolean skipForStatement = true;
|
||||
final PsiForStatement forStatement = PsiTreeUtil.getParentOfType(expr, PsiForStatement.class);
|
||||
if (forStatement != null) {
|
||||
Set<PsiVariable> vars = new HashSet<>();
|
||||
SyntaxTraverser.psiTraverser().withRoot(expr)
|
||||
.filter(element -> element instanceof PsiReferenceExpression)
|
||||
.forEach(element -> {
|
||||
final PsiElement resolve = ((PsiReferenceExpression)element).resolve();
|
||||
if (resolve instanceof PsiVariable) {
|
||||
vars.add((PsiVariable)resolve);
|
||||
}
|
||||
});
|
||||
skipForStatement = vars.stream().noneMatch(variable -> PsiTreeUtil.isAncestor(forStatement.getInitialization(), variable, true));
|
||||
}
|
||||
Set<PsiVariable> vars = new HashSet<>();
|
||||
SyntaxTraverser.psiTraverser().withRoot(expr)
|
||||
.filter(element -> element instanceof PsiReferenceExpression)
|
||||
.forEach(element -> {
|
||||
final PsiElement resolve = ((PsiReferenceExpression)element).resolve();
|
||||
if (resolve instanceof PsiVariable) {
|
||||
vars.add((PsiVariable)resolve);
|
||||
}
|
||||
});
|
||||
|
||||
PsiElement containerParent = tempContainer;
|
||||
PsiElement lastScope = tempContainer;
|
||||
while (true) {
|
||||
if (containerParent instanceof PsiFile) break;
|
||||
if (containerParent instanceof PsiMethod) break;
|
||||
// allow to find occurrences outside lambda as we allow this for loops, ifs, etc
|
||||
// if (containerParent instanceof PsiLambdaExpression) break;
|
||||
if (!skipForStatement && containerParent instanceof PsiForStatement) break;
|
||||
if (containerParent instanceof PsiLambdaExpression) {
|
||||
PsiParameter[] parameters = ((PsiLambdaExpression)containerParent).getParameterList().getParameters();
|
||||
if (Arrays.stream(parameters).anyMatch(parameter -> vars.contains(parameter))) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (containerParent instanceof PsiForStatement) {
|
||||
PsiForStatement forStatement = (PsiForStatement)containerParent;
|
||||
if (vars.stream().anyMatch(variable -> PsiTreeUtil.isAncestor(forStatement.getInitialization(), variable, true))) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
containerParent = containerParent.getParent();
|
||||
if (containerParent instanceof PsiCodeBlock) {
|
||||
lastScope = containerParent;
|
||||
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
import java.util.function.UnaryOperator;
|
||||
|
||||
class Main {
|
||||
public static void main(String[] args) {
|
||||
UnaryOperator<String> f1 = s -> {
|
||||
System.out.println("foo");
|
||||
String temp = s.trim();
|
||||
return temp;
|
||||
};
|
||||
UnaryOperator<String> f2 = s -> {
|
||||
System.out.println("foo");
|
||||
return s.trim();
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
import java.util.function.UnaryOperator;
|
||||
|
||||
class Main {
|
||||
public static void main(String[] args) {
|
||||
UnaryOperator<String> f1 = s -> {
|
||||
System.out.println("foo");
|
||||
return <selection>s.trim()</selection>;
|
||||
};
|
||||
UnaryOperator<String> f2 = s -> {
|
||||
System.out.println("foo");
|
||||
return s.trim();
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -211,6 +211,10 @@ public class IntroduceVariableTest extends LightCodeInsightTestCase {
|
||||
doTest(new MockIntroduceVariableHandler("temp", true, false, false, "int"));
|
||||
}
|
||||
|
||||
public void testDistinguishLambdaParams() {
|
||||
doTest(new MockIntroduceVariableHandler("temp", true, false, false, CommonClassNames.JAVA_LANG_STRING));
|
||||
}
|
||||
|
||||
public void testDuplicateGenericExpressions() {
|
||||
doTest(new MockIntroduceVariableHandler("temp", true, false, false, "Foo2<? extends java.lang.Runnable>"));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user