mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
extract method: inline output variable if it is returned right after creation ( IDEA-54490 )
This commit is contained in:
+18
-1
@@ -33,6 +33,7 @@ import com.intellij.openapi.editor.colors.EditorColors;
|
||||
import com.intellij.openapi.editor.colors.EditorColorsManager;
|
||||
import com.intellij.openapi.editor.markup.TextAttributes;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.Comparing;
|
||||
import com.intellij.openapi.util.Pair;
|
||||
import com.intellij.openapi.util.TextRange;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
@@ -598,7 +599,23 @@ public class ExtractMethodProcessor implements MatchProvider {
|
||||
body.add(myElementFactory.createStatementFromText("return false;", null));
|
||||
}
|
||||
else if (!myHasReturnStatement && hasNormalExit && myOutputVariable != null) {
|
||||
body.add(returnStatement);
|
||||
final PsiReturnStatement insertedReturnStatement = (PsiReturnStatement)body.add(returnStatement);
|
||||
if (myOutputVariables.length == 1) {
|
||||
final PsiExpression returnValue = insertedReturnStatement.getReturnValue();
|
||||
if (returnValue instanceof PsiReferenceExpression) {
|
||||
final PsiElement resolved = ((PsiReferenceExpression)returnValue).resolve();
|
||||
if (resolved instanceof PsiLocalVariable && Comparing.strEqual(((PsiVariable)resolved).getName(), outVariableName)) {
|
||||
final PsiStatement statement = PsiTreeUtil.getPrevSiblingOfType(insertedReturnStatement, PsiStatement.class);
|
||||
if (statement instanceof PsiDeclarationStatement) {
|
||||
final PsiElement[] declaredElements = ((PsiDeclarationStatement)statement).getDeclaredElements();
|
||||
if (ArrayUtil.find(declaredElements, resolved) != -1) {
|
||||
InlineUtil.inlineVariable((PsiVariable)resolved, ((PsiVariable)resolved).getInitializer(), (PsiReferenceExpression)returnValue);
|
||||
resolved.delete();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (myNullConditionalCheck) {
|
||||
final String varName = myOutputVariable.getName();
|
||||
|
||||
+1
-2
@@ -12,8 +12,7 @@ class C {
|
||||
}
|
||||
|
||||
private List newMethod(Object[] array) {
|
||||
List l1 = new ArrayList(Arrays.asList(array));
|
||||
return l1;
|
||||
return new ArrayList(Arrays.asList(array));
|
||||
}
|
||||
|
||||
String[] getObjects() {
|
||||
|
||||
@@ -7,7 +7,6 @@ class C {
|
||||
}
|
||||
|
||||
private int newMethod() {
|
||||
final int i = 128;
|
||||
return i;
|
||||
return 128;
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
class Test {
|
||||
void foo() {
|
||||
<selection>int j = 0;
|
||||
int i = 0;
|
||||
j = 9;</selection>
|
||||
System.out.println(i);
|
||||
}
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
class Test {
|
||||
void foo() {
|
||||
int i = newMethod();
|
||||
System.out.println(i);
|
||||
}
|
||||
|
||||
private int newMethod() {
|
||||
int j = 0;
|
||||
int i = 0;
|
||||
j = 9;
|
||||
return i;
|
||||
}
|
||||
}
|
||||
@@ -257,6 +257,10 @@ public class ExtractMethodTest extends LightCodeInsightTestCase {
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testInlineCreated2ReturnLocalVariablesOnly() throws Exception {
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testGuardMethodDuplicates() throws Exception {
|
||||
doDuplicatesTest();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user