mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
introduce parameter: do not insert additional arg when selection was in the self call (IDEA-66896)
This commit is contained in:
+28
-3
@@ -473,16 +473,41 @@ public class IntroduceParameterProcessor extends BaseRefactoringProcessor implem
|
||||
if (element.getParent() instanceof PsiMethodCallExpression) {
|
||||
PsiMethodCallExpression methodCall = (PsiMethodCallExpression)element.getParent();
|
||||
|
||||
if (myMethodToReplaceIn == myMethodToSearchFor && PsiTreeUtil.isAncestor(methodCall, myParameterInitializer, false)) return;
|
||||
|
||||
PsiElementFactory factory = JavaPsiFacade.getInstance(methodCall.getProject()).getElementFactory();
|
||||
PsiExpression expression = factory.createExpressionFromText(myParameterName, null);
|
||||
final PsiExpressionList argList = methodCall.getArgumentList();
|
||||
final PsiExpression[] exprs = argList.getExpressions();
|
||||
|
||||
if (exprs.length > 0) {
|
||||
argList.addAfter(expression, exprs[exprs.length - 1]);
|
||||
boolean first = false;
|
||||
PsiElement anchor = null;
|
||||
if (myMethodToSearchFor.isVarArgs()) {
|
||||
final int oldParamCount = myMethodToSearchFor.getParameterList().getParametersCount() - 1;
|
||||
if (exprs.length >= oldParamCount) {
|
||||
if (oldParamCount > 1) {
|
||||
anchor = exprs[oldParamCount - 2];
|
||||
}
|
||||
else {
|
||||
first = true;
|
||||
anchor = null;
|
||||
}
|
||||
} else {
|
||||
anchor = exprs[exprs.length -1];
|
||||
}
|
||||
} else if (exprs.length > 0) {
|
||||
anchor = exprs[exprs.length - 1];
|
||||
}
|
||||
|
||||
if (anchor != null) {
|
||||
argList.addAfter(expression, anchor);
|
||||
}
|
||||
else {
|
||||
argList.add(expression);
|
||||
if (first && exprs.length > 0) {
|
||||
argList.addBefore(expression, exprs[0]);
|
||||
} else {
|
||||
argList.add(expression);
|
||||
}
|
||||
}
|
||||
|
||||
removeParametersFromCall(argList);
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
class Test {
|
||||
void foo(boolean flag, int i, final int anObject) {
|
||||
if (flag) {
|
||||
foo(false, i, anObject);
|
||||
}
|
||||
System.out.println();
|
||||
foo(false, i, anObject);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
class Test {
|
||||
void foo(boolean flag, final String anObject, int... i) {
|
||||
if (flag) {
|
||||
foo(false, anObject, i);
|
||||
}
|
||||
System.out.println();
|
||||
foo(flag, anObject, i);
|
||||
}
|
||||
}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
class Test {
|
||||
void foo(final String anObject, int... i) {
|
||||
foo(anObject, 1);
|
||||
foo(anObject, 3);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
class Test {
|
||||
void foo(boolean flag, int i) {
|
||||
if (flag) {
|
||||
foo(false, i, <selection>i + 1</selection>);
|
||||
}
|
||||
System.out.println();
|
||||
foo(false, i);
|
||||
}
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
class Test {
|
||||
void foo(boolean flag, int... i) {
|
||||
if (flag) {
|
||||
foo(false, <selection>""</selection>, i);
|
||||
}
|
||||
System.out.println();
|
||||
foo(flag, i);
|
||||
}
|
||||
}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
class Test {
|
||||
void foo(int... i) {
|
||||
foo(<selection>""</selection>, 1);
|
||||
foo(3);
|
||||
}
|
||||
}
|
||||
@@ -283,6 +283,18 @@ public class IntroduceParameterTest extends LightRefactoringTestCase {
|
||||
doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_ALL, true, false, true, true);
|
||||
}
|
||||
|
||||
public void testSelfReference() throws Exception {
|
||||
doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_ALL, true, false, true, false);
|
||||
}
|
||||
|
||||
public void testSelfReferenceVarargs() throws Exception {
|
||||
doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_ALL, true, false, true, false);
|
||||
}
|
||||
|
||||
public void testSelfReferenceVarargs1() throws Exception {
|
||||
doTest(IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_ALL, true, false, true, false);
|
||||
}
|
||||
|
||||
private void doTestThroughHandler() throws Exception {
|
||||
configureByFile("/refactoring/introduceParameter/before" + getTestName(false) + ".java");
|
||||
boolean enabled = true;
|
||||
|
||||
Reference in New Issue
Block a user