mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-01-07 22:09:38 +07:00
extract method: do not fold params by default when they are used inside conditional stmts (IDEA-92815)
This commit is contained in:
@@ -120,8 +120,10 @@ public class ParametersFolder {
|
||||
PsiExpression expression = mentionedInExpressions.get(i);
|
||||
if (expression instanceof PsiArrayAccessExpression) {
|
||||
mostRanked = expression;
|
||||
myFoldingSelectedByDefault = true;
|
||||
break;
|
||||
if (!isConditional(expression, scope)) {
|
||||
myFoldingSelectedByDefault = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
final int r = findUsedVariables(data, inputVariables, expression).size();
|
||||
if (currentRank < r) {
|
||||
@@ -144,6 +146,25 @@ public class ParametersFolder {
|
||||
return mostRanked != null;
|
||||
}
|
||||
|
||||
private static boolean isConditional(PsiElement expr, LocalSearchScope scope) {
|
||||
while (expr != null) {
|
||||
final PsiElement parent = expr.getParent();
|
||||
if (parent != null && scope.containsRange(parent.getContainingFile(), parent.getTextRange())) {
|
||||
if (parent instanceof PsiIfStatement) {
|
||||
if (((PsiIfStatement)parent).getCondition() != expr) return true;
|
||||
} else if (parent instanceof PsiConditionalExpression) {
|
||||
if (((PsiConditionalExpression)parent).getCondition() != expr) return true;
|
||||
} else if (parent instanceof PsiSwitchStatement) {
|
||||
if (((PsiSwitchStatement)parent).getExpression() != expr) return true;
|
||||
}
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
expr = parent;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private void setUniqueName(ParameterTablePanel.VariableData data) {
|
||||
int idx = 1;
|
||||
while (myUsedNames.contains(data.name)) {
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
class Test {
|
||||
void foo(String[] a, int i, boolean b) {
|
||||
<selection>if (b) {
|
||||
System.out.println(a[i]);
|
||||
}</selection>
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
class Test {
|
||||
void foo(String[] a, int i, boolean b) {
|
||||
newMethod(a, i, b);
|
||||
}
|
||||
|
||||
private void newMethod(String[] a, int i, boolean b) {
|
||||
if (b) {
|
||||
System.out.println(a[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -514,6 +514,10 @@ public class ExtractMethodTest extends LightCodeInsightTestCase {
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testNonFoldInIfBody() throws Exception {
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testComplexTypeParams() throws Exception {
|
||||
doTest();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user