ConvertSwitchToIfIntention: fix processing of empty blocks

IDEA-205137 `Replace Switch with If` produce exceptions
IDEA-205122 Unwrap switch fails with exception
This commit is contained in:
Tagir Valeev
2019-01-10 12:40:04 +07:00
parent 107adb48ab
commit 0835bd0976
6 changed files with 39 additions and 8 deletions
@@ -340,7 +340,7 @@ public class ConvertSwitchToIfIntention implements IntentionAction {
final PsiCodeBlock codeBlock = blockStatement.getCodeBlock();
PsiElement start = PsiTreeUtil.skipWhitespacesForward(codeBlock.getFirstBodyElement());
PsiElement end = PsiTreeUtil.skipWhitespacesBackward(codeBlock.getLastBodyElement());
if (start != null && end != null) {
if (start != null && end != null && start != codeBlock.getRBrace()) {
for (PsiElement child = start; child != null; child = child.getNextSibling()) {
out.append(commentTracker.text(child));
if (child == end) break;
@@ -0,0 +1,8 @@
// "Replace 'switch' with 'if'" "true"
class Test {
void m() {
if (0 == 1) {
}
}
}
@@ -0,0 +1,11 @@
// "Replace 'switch' with 'if'" "true"
class Test {
void m() {
switc<caret>h (0) {
case 1 -> {
}
default -> {}
}
}
}
@@ -0,0 +1,7 @@
// "Unwrap 'switch'" "true"
public class One {
void n() {
if (true) {
}
}
}
@@ -0,0 +1,11 @@
// "Unwrap 'switch'" "true"
public class One {
void n() {
if (true) {
swit<caret>ch (0) {
default -> {
}
}
}
}
}
@@ -1,14 +1,12 @@
// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.java.propertyBased;
import com.intellij.codeInspection.CommonQuickFixBundle;
import com.intellij.openapi.application.PathManager;
import com.intellij.openapi.projectRoots.impl.JavaAwareProjectJdkTableImpl;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.pom.java.LanguageLevel;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiKeyword;
import com.intellij.psi.PsiSwitchBlock;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.testFramework.IdeaTestUtil;
@@ -68,11 +66,7 @@ public class Java12SwitchExpressionSanityTest extends LightCodeInsightFixtureTes
InvokeIntentionAroundSwitch anyIntentionInSwitchRange = new InvokeIntentionAroundSwitch(file, new JavaIntentionPolicy() {
@Override
protected boolean shouldSkipByFamilyName(@NotNull String familyName) {
return super.shouldSkipByFamilyName(familyName)
//IDEA-205122
|| CommonQuickFixBundle.message("fix.unwrap", PsiKeyword.SWITCH).equals(familyName)
//IDEA-205137
|| "Replace Switch with If".equals(familyName);
return super.shouldSkipByFamilyName(familyName);
}
});