Extract method for else branch check in when expression
This commit is contained in:
@@ -3626,13 +3626,7 @@ The "returned" value of try expression with no finally is either the last expres
|
||||
}
|
||||
|
||||
Label end = new Label();
|
||||
boolean hasElse = false;
|
||||
for (JetWhenEntry whenEntry : expression.getEntries()) {
|
||||
if (whenEntry.isElse()) {
|
||||
hasElse = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
boolean hasElse = JetPsiUtil.checkWhenExpressionHasSingleElse(expression);
|
||||
|
||||
Label nextCondition = null;
|
||||
for (JetWhenEntry whenEntry : expression.getEntries()) {
|
||||
|
||||
@@ -682,4 +682,14 @@ public class JetPsiUtil {
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static boolean checkWhenExpressionHasSingleElse(JetWhenExpression whenExpression) {
|
||||
int elseCount = 0;
|
||||
for (JetWhenEntry entry : whenExpression.getEntries()) {
|
||||
if (entry.isElse()) {
|
||||
elseCount++;
|
||||
}
|
||||
}
|
||||
return (elseCount == 1);
|
||||
}
|
||||
}
|
||||
|
||||
+6
-12
@@ -88,23 +88,19 @@ public class BranchedFoldingUtils {
|
||||
}
|
||||
|
||||
private static boolean checkFoldableWhenExpressionWithAssignments(JetWhenExpression whenExpression) {
|
||||
if (!JetPsiUtil.checkWhenExpressionHasSingleElse(whenExpression)) return false;
|
||||
|
||||
List<JetWhenEntry> entries = whenExpression.getEntries();
|
||||
|
||||
if (entries.isEmpty()) return false;
|
||||
|
||||
boolean hasElse = false;
|
||||
List<JetBinaryExpression> assignments = new ArrayList<JetBinaryExpression>();
|
||||
for (JetWhenEntry entry : entries) {
|
||||
if (entry.isElse()) {
|
||||
hasElse = true;
|
||||
}
|
||||
JetBinaryExpression assignment = checkAndGetFoldableBranchedAssignment(entry.getExpression());
|
||||
if (assignment == null) return false;
|
||||
assignments.add(assignment);
|
||||
}
|
||||
|
||||
if (!hasElse) return false;
|
||||
|
||||
assert !assignments.isEmpty();
|
||||
|
||||
JetBinaryExpression firstAssignment = assignments.get(0);
|
||||
@@ -121,24 +117,22 @@ public class BranchedFoldingUtils {
|
||||
}
|
||||
|
||||
private static boolean checkFoldableWhenExpressionWithReturns(JetWhenExpression whenExpression) {
|
||||
if (!JetPsiUtil.checkWhenExpressionHasSingleElse(whenExpression)) return false;
|
||||
|
||||
List<JetWhenEntry> entries = whenExpression.getEntries();
|
||||
|
||||
if (entries.isEmpty()) return false;
|
||||
|
||||
boolean hasElse = false;
|
||||
for (JetWhenEntry entry : entries) {
|
||||
if (entry.isElse()) {
|
||||
hasElse = true;
|
||||
}
|
||||
if (checkAndGetFoldableBranchedReturn(entry.getExpression()) == null) return false;
|
||||
}
|
||||
|
||||
return hasElse;
|
||||
return true;
|
||||
}
|
||||
|
||||
private static boolean checkFoldableIfExpressionWithAsymmetricReturns(JetIfExpression ifExpression) {
|
||||
if (checkAndGetFoldableBranchedReturn(ifExpression.getThen()) == null ||
|
||||
checkAndGetFoldableBranchedReturn(ifExpression.getElse()) != null) {
|
||||
ifExpression.getElse() != null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -27,6 +27,7 @@ import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.jet.lang.diagnostics.Diagnostic;
|
||||
import org.jetbrains.jet.lang.psi.JetPsiFactory;
|
||||
import org.jetbrains.jet.lang.psi.JetPsiUtil;
|
||||
import org.jetbrains.jet.lang.psi.JetWhenEntry;
|
||||
import org.jetbrains.jet.lang.psi.JetWhenExpression;
|
||||
import org.jetbrains.jet.plugin.JetBundle;
|
||||
@@ -53,13 +54,7 @@ public class MoveWhenElseBranchFix extends JetIntentionAction<JetWhenExpression>
|
||||
if (!super.isAvailable(project, editor, file)) {
|
||||
return false;
|
||||
}
|
||||
int elseCount = 0;
|
||||
for (JetWhenEntry entry : element.getEntries()) {
|
||||
if (entry.isElse()) {
|
||||
elseCount++;
|
||||
}
|
||||
}
|
||||
return (elseCount == 1);
|
||||
return JetPsiUtil.checkWhenExpressionHasSingleElse(element);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user