Java: Don't fold the whole extracted expression (IDEA-195927)

This commit is contained in:
Pavel Dolgov
2018-07-23 15:40:29 +03:00
parent 3bb763253b
commit c0f7044cd0
7 changed files with 60 additions and 10 deletions
@@ -30,6 +30,7 @@ import com.intellij.psi.util.PsiUtil;
import com.intellij.refactoring.util.RefactoringChangeUtil;
import com.intellij.refactoring.util.VariableData;
import com.intellij.refactoring.util.duplicates.DuplicatesFinder;
import com.intellij.util.ObjectUtils;
import com.intellij.util.text.UniqueNameGenerator;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -215,6 +216,9 @@ public class ParametersFolder {
private List<PsiExpression> getMentionedExpressions(PsiVariable var, LocalSearchScope scope, final List<? extends PsiVariable> inputVariables) {
if (myMentionedInExpressions.containsKey(var)) return myMentionedInExpressions.get(var);
final PsiElement[] scopeElements = scope.getScope();
final PsiExpression scopeExpression =
PsiUtil.skipParenthesizedExprDown(scopeElements.length == 1 ? ObjectUtils.tryCast(scopeElements[0], PsiExpression.class) : null);
List<PsiExpression> expressions = null;
for (PsiReference reference : ReferencesSearch.search(var, scope)) {
PsiElement expression = reference.getElement();
@@ -224,7 +228,7 @@ public class ParametersFolder {
if (isAccessedForWriting((PsiExpression)expression)) {
return null;
}
if (isAncestor(expression, scopeElements)) {
if (expression == scopeExpression || isAncestor(expression, scopeElements)) {
break;
}
if (dependsOnLocals(expression, inputVariables)) {
@@ -0,0 +1,19 @@
class C {
int x;
int b(boolean[] b, C[] c, int n) {
int i = n;
while (i >= 0 && (<selection>b[i] || c[n].x == c[i].x</selection>)) {
i--;
}
return i;
}
int a(boolean[] b, C[] c, int n) {
int i = n;
while (i < c.length && (b[i] || c[n].x == c[i].x)) {
i++;
}
return i;
}
}
@@ -0,0 +1,23 @@
class C {
int x;
int b(boolean[] b, C[] c, int n) {
int i = n;
while (i >= 0 && newMethod(b[i], c[n].x == c[i].x)) {
i--;
}
return i;
}
private boolean newMethod(boolean b2, boolean b1) {
return b2 || b1;
}
int a(boolean[] b, C[] c, int n) {
int i = n;
while (i < c.length && (newMethod(b[i], c[n].x == c[i].x))) {
i++;
}
return i;
}
}
@@ -2,12 +2,12 @@ import java.util.List;
class C {
void foo(int[] a, List<Integer> b) {
int i = 1;
int n = newMethod(bar(a[i], b.get(i)));
int n = newMethod(a[i], b.get(i));
System.out.println(n);
}
private int newMethod(int bar) {
return bar;
private int newMethod(int a, Integer b) {
return bar(a, b);
}
int bar(int a, int b) {
@@ -1,9 +1,9 @@
public class Test {
String foo(String[] s, int i) {
return newMethod(s[i]);
return newMethod(s, i);
}
private String newMethod(String s) {
return s;
private String newMethod(String[] s, int i) {
return s[i];
}
}
@@ -1,9 +1,9 @@
class Test {
public static void main(String[] args) {
String arg = newMethod(args[0]); //comment
String arg = newMethod(args); //comment
}
private static String newMethod(String arg) {
return arg;
private static String newMethod(String[] args) {
return args[0];
}
}
@@ -1291,6 +1291,10 @@ public class ExtractMethodTest extends LightCodeInsightTestCase {
doDuplicatesTest();
}
public void testDuplicateSubexpression() throws Exception {
doDuplicatesTest();
}
public void testBeforeCommentAfterSelectedFragment() throws Exception {
doTest();
}