From 32967db59d45318599ea38534d48dae6be95402d Mon Sep 17 00:00:00 2001 From: Tagir Valeev Date: Mon, 23 Mar 2020 11:49:58 +0700 Subject: [PATCH] IDEA-235649 'Unwrap if statement' of 'if' statement inside 'case' statement removes subsequent 'case' statements GitOrigin-RevId: 16a50c6f792fb2d30956e72fdcaaca3ed2d5b985 --- .../quickfix/SimplifyBooleanExpressionFix.java | 7 +++++-- .../unwrapIfStatement/afterInsideSwitch.java | 15 +++++++++++++++ .../unwrapIfStatement/beforeInsideSwitch.java | 18 ++++++++++++++++++ .../util/containers/ContainerUtil.java | 8 ++++++++ 4 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/unwrapIfStatement/afterInsideSwitch.java create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/unwrapIfStatement/beforeInsideSwitch.java diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/SimplifyBooleanExpressionFix.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/SimplifyBooleanExpressionFix.java index 1872f704051d..6888cbd0700f 100644 --- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/SimplifyBooleanExpressionFix.java +++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/SimplifyBooleanExpressionFix.java @@ -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 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]); } } diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/unwrapIfStatement/afterInsideSwitch.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/unwrapIfStatement/afterInsideSwitch.java new file mode 100644 index 000000000000..633a71cd0621 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/unwrapIfStatement/afterInsideSwitch.java @@ -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; + } + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/unwrapIfStatement/beforeInsideSwitch.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/unwrapIfStatement/beforeInsideSwitch.java new file mode 100644 index 000000000000..8f3fc6e498c2 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/unwrapIfStatement/beforeInsideSwitch.java @@ -0,0 +1,18 @@ +// "Unwrap 'if' statement" "true" +class X { + public String testUnwrap(String f) { + switch (f) { + case "A": + return "A"; + case "B": + if (true) { + return "b"; + } + return "B"; + case "D": + return "D"; + default: + return null; + } + } +} \ No newline at end of file diff --git a/platform/util/src/com/intellij/util/containers/ContainerUtil.java b/platform/util/src/com/intellij/util/containers/ContainerUtil.java index 2c0b0d29ac1f..cd0291b48cdf 100644 --- a/platform/util/src/com/intellij/util/containers/ContainerUtil.java +++ b/platform/util/src/com/intellij/util/containers/ContainerUtil.java @@ -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 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 int indexOf(@NotNull List list, @NotNull Condition condition) { for (int i = 0, listSize = list.size(); i < listSize; i++) {