IDEA-235649 'Unwrap if statement' of 'if' statement inside 'case' statement removes subsequent 'case' statements

GitOrigin-RevId: 16a50c6f792fb2d30956e72fdcaaca3ed2d5b985
This commit is contained in:
Tagir Valeev
2020-03-23 05:04:23 +00:00
committed by intellij-monorepo-bot
parent d84fd8b7dd
commit 32967db59d
4 changed files with 46 additions and 2 deletions
@@ -339,9 +339,12 @@ public class SimplifyBooleanExpressionFix extends LocalQuickFixOnPsiElement {
private static void removeFollowingStatements(@NotNull PsiStatement anchor, @NotNull PsiCodeBlock parentBlock) {
PsiStatement[] siblingStatements = parentBlock.getStatements();
int ifIndex = Arrays.asList(siblingStatements).indexOf(anchor);
List<PsiStatement> statements = Arrays.asList(siblingStatements);
int ifIndex = statements.indexOf(anchor);
if (ifIndex >= 0 && ifIndex < siblingStatements.length - 1) {
parentBlock.deleteChildRange(siblingStatements[ifIndex + 1], siblingStatements[siblingStatements.length - 1]);
int labelIndex = ContainerUtil.indexOf(statements.subList(ifIndex, statements.size()), st -> st instanceof PsiSwitchLabelStatement);
int limit = labelIndex != -1 ? labelIndex + ifIndex : siblingStatements.length;
parentBlock.deleteChildRange(siblingStatements[ifIndex + 1], siblingStatements[limit - 1]);
}
}
@@ -0,0 +1,15 @@
// "Unwrap 'if' statement" "true"
class X {
public String testUnwrap(String f) {
switch (f) {
case "A":
return "A";
case "B":
return "b";
case "D":
return "D";
default:
return null;
}
}
}
@@ -0,0 +1,18 @@
// "Unwrap 'if' statement" "true"
class X {
public String testUnwrap(String f) {
switch (f) {
case "A":
return "A";
case "B":
if (t<caret>rue) {
return "b";
}
return "B";
case "D":
return "D";
default:
return null;
}
}
}
@@ -2611,6 +2611,14 @@ public class ContainerUtil extends ContainerUtilRt {
return true;
}
/**
* Finds the first element in the list that satisfies given condition.
*
* @param list list to scan
* @param condition condition that should be satisfied
* @param <T> type of the list elements
* @return index of the first element in the list that satisfies the condition; -1 if no element in the list satisfies the condition.
*/
@Contract(pure=true)
public static <T> int indexOf(@NotNull List<? extends T> list, @NotNull Condition<? super T> condition) {
for (int i = 0, listSize = list.size(); i < listSize; i++) {