extract method: do not double parameters

This commit is contained in:
anna
2010-07-09 20:03:29 +04:00
parent 616023e610
commit c899f8f467
4 changed files with 46 additions and 1 deletions
@@ -0,0 +1,12 @@
class Test {
void f() {
new Runnable() {
public void run() {
int j = 0;
if (j == 0 && <selection>j < 0 && j > 0</selection>) {
assert false;
}
}
};
}
}
@@ -0,0 +1,16 @@
class Test {
void f() {
new Runnable() {
public void run() {
int j = 0;
if (j == 0 && newMethod(j)) {
assert false;
}
}
};
}
private boolean newMethod(int j) {
return j < 0 && j > 0;
}
}
@@ -9,10 +9,13 @@ import com.intellij.openapi.projectRoots.impl.JavaSdkImpl;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiExpression;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiType;
import com.intellij.psi.codeStyle.CodeStyleSettings;
import com.intellij.psi.codeStyle.CodeStyleSettingsManager;
import com.intellij.refactoring.extractMethod.ExtractMethodProcessor;
import com.intellij.refactoring.extractMethod.PrepareFailedException;
import com.intellij.refactoring.introduceVariable.IntroduceVariableBase;
import com.intellij.refactoring.util.RefactoringUtil;
import com.intellij.refactoring.util.duplicates.Match;
import com.intellij.testFramework.LightCodeInsightTestCase;
import com.intellij.util.IncorrectOperationException;
@@ -448,6 +451,10 @@ public class ExtractMethodTest extends LightCodeInsightTestCase {
doDuplicatesTest();
}
public void testParametersFromAnonymous() throws Exception {
doTest();
}
private void doPrepareErrorTest(final String expectedMessage) throws Exception {
String expectedError = null;
try {
@@ -503,6 +510,12 @@ public class ExtractMethodTest extends LightCodeInsightTestCase {
else {
elements = CodeInsightUtil.findStatementsInRange(file, startOffset, endOffset);
}
if (elements.length == 0) {
final PsiExpression expression = IntroduceVariableBase.getSelectedExpression(project, file, startOffset, endOffset);
if (expression != null) {
elements = new PsiElement[]{expression};
}
}
assertTrue(elements.length > 0);
final ExtractMethodProcessor processor =