[java-highlighting] Provide Split values of 'switch' branch quick-fix for "Illegal fall-through from/to a pattern" error

IDEA-273885

GitOrigin-RevId: 49ea03660bc8079eb93b76c2ace9264ae1c2cc7a
This commit is contained in:
Andrey.Cherkasov
2022-09-01 12:09:54 +04:00
committed by intellij-monorepo-bot
parent 67998a0600
commit 600967c68e
14 changed files with 179 additions and 7 deletions

View File

@@ -615,4 +615,6 @@ public abstract class QuickFixFactory {
* @return a fix that converts an anonymous class to an inner class
*/
public abstract @NotNull IntentionAction createConvertAnonymousToInnerAction(@NotNull PsiAnonymousClass anonymousClass);
public abstract @NotNull IntentionAction createSplitSwitchBranchWithSeveralCaseValuesAction();
}

View File

@@ -722,7 +722,9 @@ public class SwitchBlockHighlightingModel {
if (existPattern) {
PsiElement defaultKeyword = switchLabelElement.getFirstChild();
alreadyFallThroughElements.add(defaultKeyword);
holder.add(createError(defaultKeyword, JavaErrorBundle.message("switch.illegal.fall.through.from")));
HighlightInfo info = createError(defaultKeyword, JavaErrorBundle.message("switch.illegal.fall.through.from"));
QuickFixAction.registerQuickFixAction(info, getFixFactory().createSplitSwitchBranchWithSeveralCaseValuesAction());
holder.add(info);
}
existsDefault = true;
continue;
@@ -736,28 +738,36 @@ public class SwitchBlockHighlightingModel {
}
if (existPattern || existsConst || (existsNull && !existsTypeTestPattern) || existsDefault) {
alreadyFallThroughElements.add(currentElement);
holder.add(createError(currentElement, JavaErrorBundle.message("switch.illegal.fall.through.to")));
HighlightInfo info = createError(currentElement, JavaErrorBundle.message("switch.illegal.fall.through.to"));
QuickFixAction.registerQuickFixAction(info, getFixFactory().createSplitSwitchBranchWithSeveralCaseValuesAction());
holder.add(info);
}
existPattern = true;
}
else if (isNullType(currentElement)) {
if (existPattern && !existsTypeTestPattern) {
alreadyFallThroughElements.add(currentElement);
holder.add(createError(currentElement, JavaErrorBundle.message("switch.illegal.fall.through.from")));
HighlightInfo info = createError(currentElement, JavaErrorBundle.message("switch.illegal.fall.through.from"));
QuickFixAction.registerQuickFixAction(info, getFixFactory().createSplitSwitchBranchWithSeveralCaseValuesAction());
holder.add(info);
}
existsNull = true;
}
else if (isConstantLabelElement(currentElement)) {
if (existPattern) {
alreadyFallThroughElements.add(currentElement);
holder.add(createError(currentElement, JavaErrorBundle.message("switch.illegal.fall.through.from")));
HighlightInfo info = createError(currentElement, JavaErrorBundle.message("switch.illegal.fall.through.from"));
QuickFixAction.registerQuickFixAction(info, getFixFactory().createSplitSwitchBranchWithSeveralCaseValuesAction());
holder.add(info);
}
existsConst = true;
}
else if (currentElement instanceof PsiDefaultCaseLabelElement) {
if (existPattern) {
alreadyFallThroughElements.add(currentElement);
holder.add(createError(currentElement, JavaErrorBundle.message("switch.illegal.fall.through.from")));
HighlightInfo info = createError(currentElement, JavaErrorBundle.message("switch.illegal.fall.through.from"));
QuickFixAction.registerQuickFixAction(info, getFixFactory().createSplitSwitchBranchWithSeveralCaseValuesAction());
holder.add(info);
}
existsDefault = true;
}

View File

@@ -158,8 +158,16 @@ public class SplitSwitchBranchWithSeveralCaseValuesAction extends PsiElementBase
PsiElement previousElement = getPreviousElement(editor, element);
labelElement = PsiTreeUtil.getNonStrictParentOfType(previousElement, PsiCaseLabelElement.class);
}
while (labelElement != null && labelElement.getParent() instanceof PsiCaseLabelElement) {
labelElement = (PsiCaseLabelElement)labelElement.getParent();
while (labelElement != null) {
PsiElement parent = labelElement.getParent();
if (parent instanceof PsiCaseLabelElement label) {
labelElement = label;
}
else if (parent instanceof PsiDeconstructionList) {
labelElement = (PsiCaseLabelElement)parent.getParent();
} else {
break;
}
}
return labelElement;
}

View File

@@ -1173,4 +1173,9 @@ public final class QuickFixFactoryImpl extends QuickFixFactory {
return myMessage;
}
}
@Override
public @NotNull IntentionAction createSplitSwitchBranchWithSeveralCaseValuesAction() {
return new SplitSwitchBranchWithSeveralCaseValuesAction();
}
}

View File

@@ -0,0 +1,9 @@
// "Split values of 'switch' branch" "true-preview"
class C {
void foo(String o) {
switch (o) {
case "42" -> {}
case String s when s.isEmpty() -> {}
}
}
}

View File

@@ -0,0 +1,16 @@
// "Split values of 'switch' branch" "true-preview"
class C {
void foo(Object o) {
switch (o) {
case null -> {}
case Rect(Point(double x1, double y1) point1, Point(double x2, double y2) point2) -> {}
default -> {}
}
}
}
record Point(double x, double y) {
}
record Rect(Point point1, Point point2) {
}

View File

@@ -0,0 +1,18 @@
// "Copy 'switch' branch" "true-preview"
class C {
void foo(Object o) {
switch (o) {
case Rect(Point(double x1, double y1) point1, Point(double x2, double y2) point2):
System.out.println();
break;
case default:
System.out.println();
}
}
}
record Point(double x, double y) {
}
record Rect(Point point1, Point point2) {
}

View File

@@ -0,0 +1,18 @@
// "Copy 'switch' branch" "true-preview"
class C {
void foo(Object o) {
switch (o) {
case Rect(Point(double x1, double y1) point1, Point(double x2, double y2) point2):
System.out.println();
break;
default:
System.out.println();
}
}
}
record Point(double x, double y) {
}
record Rect(Point point1, Point point2) {
}

View File

@@ -0,0 +1,16 @@
// "Split values of 'switch' branch" "true-preview"
class C {
void foo(Object o) {
switch (o) {
case Rect(Point(double x1, double y1) point1, Point(double x2, double y2) point2) -> {}
case null -> {}
default -> {}
}
}
}
record Point(double x, double y) {
}
record Rect(Point point1, Point point2) {
}

View File

@@ -0,0 +1,8 @@
// "Split values of 'switch' branch" "true-preview"
class C {
void foo(String o) {
switch (o) {
case "42", String s when<caret> s.isEmpty() -> {}
}
}
}

View File

@@ -0,0 +1,15 @@
// "Split values of 'switch' branch" "true-preview"
class C {
void foo(Object o) {
switch (o) {
case null, Rect(Point(double x1, double<caret> y1) point1, Point(double x2, double y2) point2) -> {}
default -> {}
}
}
}
record Point(double x, double y) {
}
record Rect(Point point1, Point point2) {
}

View File

@@ -0,0 +1,16 @@
// "Copy 'switch' branch" "true-preview"
class C {
void foo(Object o) {
switch (o) {
case Rect(Point(double x1, double y1) point1, Point(double x2, double y2) point2):
case def<caret>ault:
System.out.println();
}
}
}
record Point(double x, double y) {
}
record Rect(Point point1, Point point2) {
}

View File

@@ -0,0 +1,16 @@
// "Copy 'switch' branch" "true-preview"
class C {
void foo(Object o) {
switch (o) {
case Rect(Point(double x1, double y1) point1, Point(double x2, double y2) point2):
def<caret>ault:
System.out.println();
}
}
}
record Point(double x, double y) {
}
record Rect(Point point1, Point point2) {
}

View File

@@ -0,0 +1,15 @@
// "Split values of 'switch' branch" "true-preview"
class C {
void foo(Object o) {
switch (o) {
case Rect(Point(double x1, double y1) point1, Point(double x2, double y2) point2), nu<caret>ll -> {}
default -> {}
}
}
}
record Point(double x, double y) {
}
record Rect(Point point1, Point point2) {
}