mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-01-06 11:50:54 +07:00
extract method from expression: declare necessary variables inside; reassign result when it was assigned before (IDEA-63593)
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
class Foo {
|
||||
void foo() {
|
||||
int x = 0;
|
||||
int y = <selection>x = 1</selection>;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
|
||||
class Foo {
|
||||
void foo() {
|
||||
int x = 0;
|
||||
<selection>x = 1</selection>;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
|
||||
class Foo {
|
||||
void foo() {
|
||||
int x = 0;
|
||||
newMethod();
|
||||
}
|
||||
|
||||
private void newMethod() {
|
||||
int x;
|
||||
x = 1;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
class Foo {
|
||||
void foo() {
|
||||
int x = 0;
|
||||
int y = x = newMethod();
|
||||
}
|
||||
|
||||
private int newMethod() {
|
||||
int x;
|
||||
return x = 1;
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user