mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
replace if with conditional: proceed lambda when else is missed (IDEA-154185)
This commit is contained in:
+23
-14
@@ -26,6 +26,7 @@ import com.siyeh.ipp.base.Intention;
|
||||
import com.siyeh.ipp.base.PsiElementPredicate;
|
||||
import org.jetbrains.annotations.NonNls;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
@@ -88,13 +89,10 @@ public class ReplaceIfWithConditionalIntention extends Intention {
|
||||
if (elseReturnValue == null) {
|
||||
return;
|
||||
}
|
||||
final PsiElement method = PsiTreeUtil.getParentOfType(thenReturn, PsiMethod.class, PsiLambdaExpression.class);
|
||||
if (method == null) {
|
||||
final String conditional = getConditional(condition, thenReturn, thenReturnValue, elseReturnValue);
|
||||
if (conditional == null) {
|
||||
return;
|
||||
}
|
||||
final PsiType returnType = method instanceof PsiMethod ? ((PsiMethod)method).getReturnType()
|
||||
: LambdaUtil.getFunctionalInterfaceReturnType((PsiLambdaExpression)method);
|
||||
final String conditional = getConditionalText(condition, thenReturnValue, elseReturnValue, returnType);
|
||||
replaceIfStatement(ifStatement, "return " + conditional + ';');
|
||||
}
|
||||
else if (ReplaceIfWithConditionalPredicate.isReplaceableMethodCall(ifStatement)) {
|
||||
@@ -159,20 +157,31 @@ public class ReplaceIfWithConditionalIntention extends Intention {
|
||||
if (elseReturnValue == null) {
|
||||
return;
|
||||
}
|
||||
final PsiMethod method = PsiTreeUtil.getParentOfType(thenBranch, PsiMethod.class);
|
||||
if (method == null) {
|
||||
return;
|
||||
}
|
||||
final PsiType methodType = method.getReturnType();
|
||||
final String conditional = getConditionalText(condition, thenReturnValue, elseReturnValue, methodType);
|
||||
if (conditional == null) {
|
||||
return;
|
||||
}
|
||||
final String conditional = getConditional(condition, thenBranch, thenReturnValue, elseReturnValue);
|
||||
if (conditional == null) return;
|
||||
replaceIfStatement(ifStatement, "return " + conditional + ';');
|
||||
elseBranch.delete();
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static String getConditional(PsiExpression condition,
|
||||
PsiElement thenBranch,
|
||||
PsiExpression thenReturnValue,
|
||||
PsiExpression elseReturnValue) {
|
||||
final PsiElement method = PsiTreeUtil.getParentOfType(thenBranch, PsiMethod.class, PsiLambdaExpression.class);
|
||||
if (method == null) {
|
||||
return null;
|
||||
}
|
||||
final PsiType methodType = method instanceof PsiMethod ? ((PsiMethod)method).getReturnType()
|
||||
: LambdaUtil.getFunctionalInterfaceReturnType((PsiLambdaExpression)method);
|
||||
final String conditional = getConditionalText(condition, thenReturnValue, elseReturnValue, methodType);
|
||||
if (conditional == null) {
|
||||
return null;
|
||||
}
|
||||
return conditional;
|
||||
}
|
||||
|
||||
private static void replaceIfStatement(PsiIfStatement ifStatement, String text) {
|
||||
PsiElement parent = ifStatement.getParent();
|
||||
final Collection<PsiComment> comments = PsiTreeUtil.findChildrenOfType(ifStatement, PsiComment.class);
|
||||
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
class Test {
|
||||
interface I {
|
||||
int m();
|
||||
}
|
||||
|
||||
String foo() {
|
||||
I i = () -> {
|
||||
<caret>if (true) {
|
||||
return 1;
|
||||
}
|
||||
return new Integer(2);
|
||||
};
|
||||
return null;
|
||||
}
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
class Test {
|
||||
interface I {
|
||||
int m();
|
||||
}
|
||||
|
||||
String foo() {
|
||||
I i = () -> {
|
||||
return true ? 1 : new Integer(2);
|
||||
};
|
||||
return null;
|
||||
}
|
||||
}
|
||||
+4
@@ -30,6 +30,10 @@ public class ReplaceIfWithConditionalIntentionTest extends IPPTestCase {
|
||||
public void testComments() { doTest(); }
|
||||
public void testInsideLambda() { doTest(); }
|
||||
|
||||
public void testInsideLambda1() {
|
||||
doTest();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getIntentionName() {
|
||||
return IntentionPowerPackBundle.message("replace.if.with.conditional.intention.name");
|
||||
|
||||
Reference in New Issue
Block a user