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

This commit is contained in:
Pavel Dolgov
2018-07-23 16:41:26 +03:00
parent 03ff6a98a6
commit 3e7bc47796
4 changed files with 48 additions and 5 deletions

View File

@@ -30,7 +30,6 @@ 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;
@@ -216,8 +215,6 @@ 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)) {
@@ -228,7 +225,7 @@ public class ParametersFolder {
if (isAccessedForWriting((PsiExpression)expression)) {
return null;
}
if (expression == scopeExpression || isAncestor(expression, scopeElements)) {
if (isAncestor(expression, scopeElements)) {
break;
}
if (dependsOnLocals(expression, inputVariables)) {
@@ -285,7 +282,7 @@ public class ParametersFolder {
private static boolean isAncestor(PsiElement expression, PsiElement[] scopeElements) {
for (PsiElement scopeElement : scopeElements) {
if (PsiTreeUtil.isAncestor(expression, scopeElement, true)) {
if (PsiTreeUtil.isAncestor(expression, scopeElement, false)) {
return true;
}
}

View File

@@ -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;
}
}

View File

@@ -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;
}
}

View File

@@ -1295,6 +1295,10 @@ public class ExtractMethodTest extends LightCodeInsightTestCase {
doDuplicatesTest();
}
public void testDuplicateSubexpressionWithParentheses() throws Exception {
doDuplicatesTest();
}
public void testBeforeCommentAfterSelectedFragment() throws Exception {
doTest();
}