[java-highlighting] Minor fixes in switch statement highlighting

Part of IDEA-365344 Create a new Java error highlighter with minimal dependencies (PSI only)

GitOrigin-RevId: 2924ae6c9984caa6e8a45f640c4de54e02134ad3
This commit is contained in:
Tagir Valeev
2025-02-18 15:04:04 +01:00
committed by intellij-monorepo-bot
parent ae7957d3c7
commit f8b210a481
4 changed files with 31 additions and 3 deletions

View File

@@ -924,7 +924,16 @@ public final class JavaErrorKinds {
parameterized(PsiExpression.class, JavaIncompatibleTypeErrorContext.class, "switch.expression.incompatible.type")
.withRawDescription((expr, context) -> message("switch.expression.incompatible.type", formatType(context.rType()), formatType(context.lType())));
public static final Simple<PsiElement> SWITCH_LABEL_EXPECTED = error(PsiElement.class, "switch.label.expected");
public static final Simple<PsiElement> SWITCH_DIFFERENT_CASE_KINDS = error("switch.different.case.kinds");
public static final Simple<PsiStatement> SWITCH_DIFFERENT_CASE_KINDS = error(PsiStatement.class, "switch.different.case.kinds")
.withRange(statement -> {
if (statement instanceof PsiSwitchLabeledRuleStatement rule) {
PsiCaseLabelElementList list = rule.getCaseLabelElementList();
if (list != null) {
return TextRange.create(0, list.getTextRangeInParent().getEndOffset());
}
}
return null;
});
public static final Parameterized<PsiExpression, JavaPsiSwitchUtil.SelectorKind> SWITCH_SELECTOR_TYPE_INVALID =
parameterized(PsiExpression.class, JavaPsiSwitchUtil.SelectorKind.class, "switch.selector.type.invalid")
.withRawDescription((expr, kind) -> kind.getFeature() == null ?

View File

@@ -173,7 +173,7 @@ public final class JavaPsiSwitchUtil {
SelectorKind(@Nullable JavaFeature feature) { myFeature = feature; }
/**
* @return java feature required for this selector kind; null if it's always available
* @return java feature required for this selector kind; null if it's always available or non-applicable
*/
public @Nullable JavaFeature getFeature() {
return myFeature;

View File

@@ -29,4 +29,11 @@ class SwitchStatement {
<error descr="Local class 'Local' cannot be referenced from another switch branch">Local</error> x = new <error descr="Local class 'Local' cannot be referenced from another switch branch">Local</error>();
}
}
int unsupported(int i) {
return <error descr="'switch' expressions are not supported at language level '1.4'">switch</error> (i) {
default:
throw new IllegalStateException("Unexpected value: " + i);
};
}
}

View File

@@ -23,7 +23,7 @@ class EnhancedSwitchStatements {
}
switch (new Random().nextInt()) {
case 0: throw new IllegalStateException("no args"); break;
<error descr="Different 'case' kinds used in 'switch'">case 1 -> { System.out.println("one"); }</error>
<error descr="Different 'case' kinds used in 'switch'">case 1</error> -> { System.out.println("one"); }
}
{ <error descr="Case statement outside switch">case 11 -> System.out.println("hi there");</error> }
@@ -88,4 +88,16 @@ class EnhancedSwitchStatements {
}
private static void noop() { }
void differentCase() {
int a = 1;
switch (a) {
case 1:
System.out.println(1);
break;
<error descr="Different 'case' kinds used in 'switch'">case 2</error> -> {
System.out.println(2);
}
}
}
}