extract method from expression: declare necessary variables inside; reassign result when it was assigned before (IDEA-63593)

This commit is contained in:
anna
2010-12-29 13:49:30 +03:00
parent e54da3264b
commit fa41e76126
6 changed files with 50 additions and 1 deletions

View File

@@ -718,6 +718,7 @@ public class ExtractMethodProcessor implements MatchProvider {
deleteExtracted();
}
else {
declareNecessaryVariablesInsideBody(body);
if (myHasExpressionOutput) {
PsiReturnStatement returnStatement = (PsiReturnStatement)myElementFactory.createStatementFromText("return x;", null);
final PsiExpression returnValue = RefactoringUtil.convertInitializerToNormalExpression(myExpression, myForcedReturnType);
@@ -729,7 +730,11 @@ public class ExtractMethodProcessor implements MatchProvider {
statement.getExpression().replace(myExpression);
body.add(statement);
}
final PsiElement replacement = IntroduceVariableBase.replace(myExpression, myMethodCall, myProject);
PsiExpression expression2Replace = myExpression;
if (myExpression instanceof PsiAssignmentExpression) {
expression2Replace = ((PsiAssignmentExpression)myExpression).getRExpression();
}
final PsiElement replacement = IntroduceVariableBase.replace(expression2Replace, myMethodCall, myProject);
myMethodCall = PsiTreeUtil.getParentOfType(replacement.findElementAt(replacement.getText().indexOf(myMethodCall.getText())), PsiMethodCallExpression.class);
}

View File

@@ -0,0 +1,6 @@
class Foo {
void foo() {
int x = 0;
int y = <selection>x = 1</selection>;
}
}

View File

@@ -0,0 +1,7 @@
class Foo {
void foo() {
int x = 0;
<selection>x = 1</selection>;
}
}

View File

@@ -0,0 +1,12 @@
class Foo {
void foo() {
int x = 0;
newMethod();
}
private void newMethod() {
int x;
x = 1;
}
}

View File

@@ -0,0 +1,11 @@
class Foo {
void foo() {
int x = 0;
int y = x = newMethod();
}
private int newMethod() {
int x;
return x = 1;
}
}

View File

@@ -151,6 +151,14 @@ public class ExtractMethodTest extends LightCodeInsightTestCase {
doTest();
}
public void testExtractAssignmentExpression() throws Exception {
doTest();
}
public void testExtractAssignmentExpressionFromStatement() throws Exception {
doTest();
}
public void _testExtractFromTryFinally2() throws Exception { // IDEADEV-11844
doTest();
}