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();
|
Label end = new Label();
|
||||||
boolean hasElse = false;
|
boolean hasElse = JetPsiUtil.checkWhenExpressionHasSingleElse(expression);
|
||||||
for (JetWhenEntry whenEntry : expression.getEntries()) {
|
|
||||||
if (whenEntry.isElse()) {
|
|
||||||
hasElse = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Label nextCondition = null;
|
Label nextCondition = null;
|
||||||
for (JetWhenEntry whenEntry : expression.getEntries()) {
|
for (JetWhenEntry whenEntry : expression.getEntries()) {
|
||||||
|
|||||||
@@ -682,4 +682,14 @@ public class JetPsiUtil {
|
|||||||
|
|
||||||
return false;
|
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) {
|
private static boolean checkFoldableWhenExpressionWithAssignments(JetWhenExpression whenExpression) {
|
||||||
|
if (!JetPsiUtil.checkWhenExpressionHasSingleElse(whenExpression)) return false;
|
||||||
|
|
||||||
List<JetWhenEntry> entries = whenExpression.getEntries();
|
List<JetWhenEntry> entries = whenExpression.getEntries();
|
||||||
|
|
||||||
if (entries.isEmpty()) return false;
|
if (entries.isEmpty()) return false;
|
||||||
|
|
||||||
boolean hasElse = false;
|
|
||||||
List<JetBinaryExpression> assignments = new ArrayList<JetBinaryExpression>();
|
List<JetBinaryExpression> assignments = new ArrayList<JetBinaryExpression>();
|
||||||
for (JetWhenEntry entry : entries) {
|
for (JetWhenEntry entry : entries) {
|
||||||
if (entry.isElse()) {
|
|
||||||
hasElse = true;
|
|
||||||
}
|
|
||||||
JetBinaryExpression assignment = checkAndGetFoldableBranchedAssignment(entry.getExpression());
|
JetBinaryExpression assignment = checkAndGetFoldableBranchedAssignment(entry.getExpression());
|
||||||
if (assignment == null) return false;
|
if (assignment == null) return false;
|
||||||
assignments.add(assignment);
|
assignments.add(assignment);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!hasElse) return false;
|
|
||||||
|
|
||||||
assert !assignments.isEmpty();
|
assert !assignments.isEmpty();
|
||||||
|
|
||||||
JetBinaryExpression firstAssignment = assignments.get(0);
|
JetBinaryExpression firstAssignment = assignments.get(0);
|
||||||
@@ -121,24 +117,22 @@ public class BranchedFoldingUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static boolean checkFoldableWhenExpressionWithReturns(JetWhenExpression whenExpression) {
|
private static boolean checkFoldableWhenExpressionWithReturns(JetWhenExpression whenExpression) {
|
||||||
|
if (!JetPsiUtil.checkWhenExpressionHasSingleElse(whenExpression)) return false;
|
||||||
|
|
||||||
List<JetWhenEntry> entries = whenExpression.getEntries();
|
List<JetWhenEntry> entries = whenExpression.getEntries();
|
||||||
|
|
||||||
if (entries.isEmpty()) return false;
|
if (entries.isEmpty()) return false;
|
||||||
|
|
||||||
boolean hasElse = false;
|
|
||||||
for (JetWhenEntry entry : entries) {
|
for (JetWhenEntry entry : entries) {
|
||||||
if (entry.isElse()) {
|
|
||||||
hasElse = true;
|
|
||||||
}
|
|
||||||
if (checkAndGetFoldableBranchedReturn(entry.getExpression()) == null) return false;
|
if (checkAndGetFoldableBranchedReturn(entry.getExpression()) == null) return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return hasElse;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static boolean checkFoldableIfExpressionWithAsymmetricReturns(JetIfExpression ifExpression) {
|
private static boolean checkFoldableIfExpressionWithAsymmetricReturns(JetIfExpression ifExpression) {
|
||||||
if (checkAndGetFoldableBranchedReturn(ifExpression.getThen()) == null ||
|
if (checkAndGetFoldableBranchedReturn(ifExpression.getThen()) == null ||
|
||||||
checkAndGetFoldableBranchedReturn(ifExpression.getElse()) != null) {
|
ifExpression.getElse() != null) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -27,6 +27,7 @@ import org.jetbrains.annotations.NotNull;
|
|||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
import org.jetbrains.jet.lang.diagnostics.Diagnostic;
|
import org.jetbrains.jet.lang.diagnostics.Diagnostic;
|
||||||
import org.jetbrains.jet.lang.psi.JetPsiFactory;
|
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.JetWhenEntry;
|
||||||
import org.jetbrains.jet.lang.psi.JetWhenExpression;
|
import org.jetbrains.jet.lang.psi.JetWhenExpression;
|
||||||
import org.jetbrains.jet.plugin.JetBundle;
|
import org.jetbrains.jet.plugin.JetBundle;
|
||||||
@@ -53,13 +54,7 @@ public class MoveWhenElseBranchFix extends JetIntentionAction<JetWhenExpression>
|
|||||||
if (!super.isAvailable(project, editor, file)) {
|
if (!super.isAvailable(project, editor, file)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
int elseCount = 0;
|
return JetPsiUtil.checkWhenExpressionHasSingleElse(element);
|
||||||
for (JetWhenEntry entry : element.getEntries()) {
|
|
||||||
if (entry.isElse()) {
|
|
||||||
elseCount++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return (elseCount == 1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
Reference in New Issue
Block a user