[java-intention] WrapSwitchRuleStatementsIntoBlock: support erroneous semicolon; fix-all option

Fixes EA-261429 - AE: JavaParserUtil.parseFragment

GitOrigin-RevId: 3939658513b2b1bec5eca693e50386fd4fe2e58e
This commit is contained in:
Tagir Valeev
2021-03-18 11:36:32 +07:00
committed by intellij-monorepo-bot
parent 232eab2954
commit 1e2b4cfe0d
5 changed files with 50 additions and 1 deletions

View File

@@ -2,11 +2,13 @@
package com.intellij.codeInsight.daemon.impl.quickfix;
import com.intellij.codeInsight.daemon.QuickFixBundle;
import com.intellij.codeInsight.daemon.impl.actions.IntentionActionWithFixAllOption;
import com.intellij.codeInsight.intention.impl.BaseIntentionAction;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.util.PsiUtil;
import com.intellij.util.ArrayUtil;
import com.intellij.util.IncorrectOperationException;
import com.intellij.util.ObjectUtils;
@@ -15,7 +17,7 @@ import org.jetbrains.annotations.NotNull;
import java.util.Objects;
public class WrapSwitchRuleStatementsIntoBlockFix extends BaseIntentionAction {
public class WrapSwitchRuleStatementsIntoBlockFix extends BaseIntentionAction implements IntentionActionWithFixAllOption {
@NotNull
private final PsiSwitchLabeledRuleStatement myRuleStatement;
@@ -65,6 +67,12 @@ public class WrapSwitchRuleStatementsIntoBlockFix extends BaseIntentionAction {
oldBody = myRuleStatement.getBody().copy();
myRuleStatement.getBody().delete();
}
for (PsiElement lastChild = myRuleStatement.getLastChild(); lastChild != null; lastChild = lastChild.getPrevSibling()) {
if (PsiUtil.isJavaToken(lastChild, JavaTokenType.SEMICOLON)) {
lastChild.delete();
break;
}
}
PsiSwitchLabeledRuleStatement newRule = (PsiSwitchLabeledRuleStatement)JavaPsiFacade.getElementFactory(project).createStatementFromText(
myRuleStatement.getText() + "{}", myRuleStatement);
PsiCodeBlock block = ((PsiBlockStatement)Objects.requireNonNull(newRule.getBody())).getCodeBlock();

View File

@@ -0,0 +1,13 @@
// "Apply all 'Create block' fixes in file" "true"
class X {
void test(int x) {
switch(x) {
case 1 -> {
}
case 2 -> {
}
case 3 -> {
}
}
}
}

View File

@@ -0,0 +1,10 @@
// "Wrap with block" "true"
class X {
void test(int x) {
switch(x) {
case 1 -> { /**/
;
}
}
}
}

View File

@@ -0,0 +1,10 @@
// "Apply all 'Create block' fixes in file" "true"
class X {
void test(int x) {
switch(x) {
case 1 -> <caret>;
case 2 -> ;
case 3 -> ;
}
}
}

View File

@@ -0,0 +1,8 @@
// "Wrap with block" "true"
class X {
void test(int x) {
switch(x) {
case 1 -> <caret>; /**/ ;
}
}
}