diff --git a/java/java-analysis-impl/src/com/intellij/codeInsight/daemon/impl/analysis/PatternHighlightingModel.java b/java/java-analysis-impl/src/com/intellij/codeInsight/daemon/impl/analysis/PatternHighlightingModel.java index 5ca8889479f3..9a6a0443cb9e 100644 --- a/java/java-analysis-impl/src/com/intellij/codeInsight/daemon/impl/analysis/PatternHighlightingModel.java +++ b/java/java-analysis-impl/src/com/intellij/codeInsight/daemon/impl/analysis/PatternHighlightingModel.java @@ -782,7 +782,8 @@ final class PatternHighlightingModel { return filtered; } - private static boolean oneOfUnconditional(PatternDeconstructionDescription whoType, PatternDeconstructionDescription overWhom) { + private static boolean oneOfUnconditional(@NotNull PatternDeconstructionDescription whoType, + @NotNull PatternDeconstructionDescription overWhom) { if (!whoType.type().equals(overWhom.type())) { return false; } diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createMissingDeconstructionRecordClassBranches/afterNestedDeconstructionWIthNonRecord.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createMissingDeconstructionRecordClassBranches/afterNestedDeconstructionWIthNonRecord.java index c6bea38a8537..74b281bf3b00 100644 --- a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createMissingDeconstructionRecordClassBranches/afterNestedDeconstructionWIthNonRecord.java +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createMissingDeconstructionRecordClassBranches/afterNestedDeconstructionWIthNonRecord.java @@ -1,4 +1,4 @@ -// "Create missing switch branch 'PairString(Box2 t, java.lang.String t2)'" "true-preview" +// "Create missing switch branch 'PairString(Box2 t, String t2)'" "true-preview" record Rec(L1 l1) {} diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createMissingDeconstructionRecordClassBranches/afterNestedDeconstructionWIthNonRecord2.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createMissingDeconstructionRecordClassBranches/afterNestedDeconstructionWIthNonRecord2.java index db49c3515f53..da92dbde96e9 100644 --- a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createMissingDeconstructionRecordClassBranches/afterNestedDeconstructionWIthNonRecord2.java +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createMissingDeconstructionRecordClassBranches/afterNestedDeconstructionWIthNonRecord2.java @@ -1,4 +1,4 @@ -// "Create missing switch branch 'StringPair(java.lang.String t2, Box t)'" "true-preview" +// "Create missing switch branch 'StringPair(String t2, Box t)'" "true-preview" record Rec(L1 l1) {} diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createMissingDeconstructionRecordClassBranches/beforeNestedDeconstructionWIthNonRecord.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createMissingDeconstructionRecordClassBranches/beforeNestedDeconstructionWIthNonRecord.java index 93f149fb53b6..41678ccba09c 100644 --- a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createMissingDeconstructionRecordClassBranches/beforeNestedDeconstructionWIthNonRecord.java +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createMissingDeconstructionRecordClassBranches/beforeNestedDeconstructionWIthNonRecord.java @@ -1,4 +1,4 @@ -// "Create missing switch branch 'PairString(Box2 t, java.lang.String t2)'" "true-preview" +// "Create missing switch branch 'PairString(Box2 t, String t2)'" "true-preview" record Rec(L1 l1) {} diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createMissingDeconstructionRecordClassBranches/beforeNestedDeconstructionWIthNonRecord2.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createMissingDeconstructionRecordClassBranches/beforeNestedDeconstructionWIthNonRecord2.java index da0b045dbc42..bff5aee61cc5 100644 --- a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createMissingDeconstructionRecordClassBranches/beforeNestedDeconstructionWIthNonRecord2.java +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createMissingDeconstructionRecordClassBranches/beforeNestedDeconstructionWIthNonRecord2.java @@ -1,4 +1,4 @@ -// "Create missing switch branch 'StringPair(java.lang.String t2, Box t)'" "true-preview" +// "Create missing switch branch 'StringPair(String t2, Box t)'" "true-preview" record Rec(L1 l1) {} diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createMissingDeconstructionRecordClassBranches21/afterNestedDeconstruction2.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createMissingDeconstructionRecordClassBranches21/afterNestedDeconstruction2.java index ecc4f9510003..7bd7bb5e34ea 100644 --- a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createMissingDeconstructionRecordClassBranches21/afterNestedDeconstruction2.java +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createMissingDeconstructionRecordClassBranches21/afterNestedDeconstruction2.java @@ -1,7 +1,4 @@ // "Create missing switch branch 'Pair(Box b1, Boxed b2)'" "true-preview" - -record Rec(L1 l1) {} - record Pair(T b1, K b2) {} sealed interface Boxed permits Box, Box2 {} diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createMissingDeconstructionRecordClassBranches21/afterNestedDeconstructionWIthNonRecord.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createMissingDeconstructionRecordClassBranches21/afterNestedDeconstructionWIthNonRecord.java index c6bea38a8537..1cb3088de4dd 100644 --- a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createMissingDeconstructionRecordClassBranches21/afterNestedDeconstructionWIthNonRecord.java +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createMissingDeconstructionRecordClassBranches21/afterNestedDeconstructionWIthNonRecord.java @@ -1,8 +1,4 @@ -// "Create missing switch branch 'PairString(Box2 t, java.lang.String t2)'" "true-preview" - -record Rec(L1 l1) {} - -record Pair(T b1, K b2) {} +// "Create missing switch branch 'PairString(Box2 t, String t2)'" "true-preview" sealed interface Boxed permits Box, Box2 {} @@ -18,8 +14,6 @@ final class L22 implements L1 {} record PairString(T t, String t2) { } -record StringPair(String t2, T t) { } - class Test { void foo4(PairString o) { switch (o) { diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createMissingDeconstructionRecordClassBranches21/afterNestedDeconstructionWIthNonRecord2.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createMissingDeconstructionRecordClassBranches21/afterNestedDeconstructionWIthNonRecord2.java index db49c3515f53..2453eb611083 100644 --- a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createMissingDeconstructionRecordClassBranches21/afterNestedDeconstructionWIthNonRecord2.java +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createMissingDeconstructionRecordClassBranches21/afterNestedDeconstructionWIthNonRecord2.java @@ -1,8 +1,4 @@ -// "Create missing switch branch 'StringPair(java.lang.String t2, Box t)'" "true-preview" - -record Rec(L1 l1) {} - -record Pair(T b1, K b2) {} +// "Create missing switch branch 'StringPair(String t2, Box t)'" "true-preview" sealed interface Boxed permits Box, Box2 {} @@ -16,8 +12,6 @@ final class L21 implements L1 {} final class L22 implements L1 {} -record PairString(T t, String t2) { } - record StringPair(String t2, T t) { } class Test { diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createMissingDeconstructionRecordClassBranches21/afterSeveralBranches.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createMissingDeconstructionRecordClassBranches21/afterSeveralBranches.java new file mode 100644 index 000000000000..5bdc6064393c --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createMissingDeconstructionRecordClassBranches21/afterSeveralBranches.java @@ -0,0 +1,22 @@ +// "Create missing branches: 'Pair(A sc1, B sc2)', and 'Pair(B sc1, A sc2)'" "true-preview" +record Pair(SC sc1, SC sc2) { + +} + +sealed interface SC{} +final class A implements SC{} +final class B implements SC{} + +class Test { + + void foo1(Pair o) { + switch (o) { + case Pair(A sc1, A sc2) -> System.out.println("1"); + case Pair(B sc1, B sc2) -> System.out.println("1"); + case Pair(A sc1, B sc2) -> { + } + case Pair(B sc1, A sc2) -> { + } + } + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createMissingDeconstructionRecordClassBranches21/afterSeveralBranches2.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createMissingDeconstructionRecordClassBranches21/afterSeveralBranches2.java new file mode 100644 index 000000000000..72a131c4e48d --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createMissingDeconstructionRecordClassBranches21/afterSeveralBranches2.java @@ -0,0 +1,21 @@ +// "Create missing branches: 'Pair(A sc1, A sc2)', and 'Pair(B sc1, B sc2)'" "true-preview" +record Pair(SC sc1, SC sc2) { + +} + +sealed interface SC{} +final class A implements SC{} +final class B implements SC{} + +class Test { + void foo2(Pair o) { + switch (o) { + case Pair(A sc1, B sc2) -> System.out.println("1"); + case Pair(B sc1, A sc2) -> System.out.println("1"); + case Pair(A sc1, A sc2) -> { + } + case Pair(B sc1, B sc2) -> { + } + } + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createMissingDeconstructionRecordClassBranches21/afterSeveralBranches3.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createMissingDeconstructionRecordClassBranches21/afterSeveralBranches3.java new file mode 100644 index 000000000000..d142c3d9c730 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createMissingDeconstructionRecordClassBranches21/afterSeveralBranches3.java @@ -0,0 +1,32 @@ +// "Create missing branches: 'Pair(A sc1, B sc2)', 'Pair(A sc1, C sc2)', ..." "true-preview" +record Pair(SC sc1, SC sc2) { + +} + +sealed interface SC{} +final class A implements SC{} +final class B implements SC{} +final class C implements SC{} + +class Test { + + void foo1(Pair o) { + switch (o) { + case Pair(A sc1, A sc2) -> System.out.println("1"); + case Pair(B sc1, B sc2) -> System.out.println("1"); + case Pair(C sc1, C sc2) -> System.out.println("1"); + case Pair(A sc1, B sc2) -> { + } + case Pair(A sc1, C sc2) -> { + } + case Pair(B sc1, A sc2) -> { + } + case Pair(B sc1, C sc2) -> { + } + case Pair(C sc1, A sc2) -> { + } + case Pair(C sc1, B sc2) -> { + } + } + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createMissingDeconstructionRecordClassBranches21/beforeNestedDeconstruction2.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createMissingDeconstructionRecordClassBranches21/beforeNestedDeconstruction2.java index 799ed714f7ed..223a39cfe475 100644 --- a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createMissingDeconstructionRecordClassBranches21/beforeNestedDeconstruction2.java +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createMissingDeconstructionRecordClassBranches21/beforeNestedDeconstruction2.java @@ -1,7 +1,4 @@ // "Create missing switch branch 'Pair(Box b1, Boxed b2)'" "true-preview" - -record Rec(L1 l1) {} - record Pair(T b1, K b2) {} sealed interface Boxed permits Box, Box2 {} diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createMissingDeconstructionRecordClassBranches21/beforeNestedDeconstructionWIthNonRecord.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createMissingDeconstructionRecordClassBranches21/beforeNestedDeconstructionWIthNonRecord.java index 93f149fb53b6..6b1904dbec7a 100644 --- a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createMissingDeconstructionRecordClassBranches21/beforeNestedDeconstructionWIthNonRecord.java +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createMissingDeconstructionRecordClassBranches21/beforeNestedDeconstructionWIthNonRecord.java @@ -1,8 +1,4 @@ -// "Create missing switch branch 'PairString(Box2 t, java.lang.String t2)'" "true-preview" - -record Rec(L1 l1) {} - -record Pair(T b1, K b2) {} +// "Create missing switch branch 'PairString(Box2 t, String t2)'" "true-preview" sealed interface Boxed permits Box, Box2 {} @@ -18,8 +14,6 @@ final class L22 implements L1 {} record PairString(T t, String t2) { } -record StringPair(String t2, T t) { } - class Test { void foo4(PairString o) { switch (o) { diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createMissingDeconstructionRecordClassBranches21/beforeNestedDeconstructionWIthNonRecord2.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createMissingDeconstructionRecordClassBranches21/beforeNestedDeconstructionWIthNonRecord2.java index da0b045dbc42..d5a9a202a5ae 100644 --- a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createMissingDeconstructionRecordClassBranches21/beforeNestedDeconstructionWIthNonRecord2.java +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createMissingDeconstructionRecordClassBranches21/beforeNestedDeconstructionWIthNonRecord2.java @@ -1,8 +1,4 @@ -// "Create missing switch branch 'StringPair(java.lang.String t2, Box t)'" "true-preview" - -record Rec(L1 l1) {} - -record Pair(T b1, K b2) {} +// "Create missing switch branch 'StringPair(String t2, Box t)'" "true-preview" sealed interface Boxed permits Box, Box2 {} @@ -16,8 +12,6 @@ final class L21 implements L1 {} final class L22 implements L1 {} -record PairString(T t, String t2) { } - record StringPair(String t2, T t) { } class Test { diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createMissingDeconstructionRecordClassBranches21/beforeSeveralBranches.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createMissingDeconstructionRecordClassBranches21/beforeSeveralBranches.java new file mode 100644 index 000000000000..ba998777c607 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createMissingDeconstructionRecordClassBranches21/beforeSeveralBranches.java @@ -0,0 +1,18 @@ +// "Create missing branches: 'Pair(A sc1, B sc2)', and 'Pair(B sc1, A sc2)'" "true-preview" +record Pair(SC sc1, SC sc2) { + +} + +sealed interface SC{} +final class A implements SC{} +final class B implements SC{} + +class Test { + + void foo1(Pair o) { + switch (o) { + case Pair(A sc1, A sc2) -> System.out.println("1"); + case Pair(B sc1, B sc2) -> System.out.println("1"); + } + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createMissingDeconstructionRecordClassBranches21/beforeSeveralBranches2.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createMissingDeconstructionRecordClassBranches21/beforeSeveralBranches2.java new file mode 100644 index 000000000000..89dc8032e477 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createMissingDeconstructionRecordClassBranches21/beforeSeveralBranches2.java @@ -0,0 +1,17 @@ +// "Create missing branches: 'Pair(A sc1, A sc2)', and 'Pair(B sc1, B sc2)'" "true-preview" +record Pair(SC sc1, SC sc2) { + +} + +sealed interface SC{} +final class A implements SC{} +final class B implements SC{} + +class Test { + void foo2(Pair o) { + switch (o) { + case Pair(A sc1, B sc2) -> System.out.println("1"); + case Pair(B sc1, A sc2) -> System.out.println("1"); + } + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createMissingDeconstructionRecordClassBranches21/beforeSeveralBranches3.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createMissingDeconstructionRecordClassBranches21/beforeSeveralBranches3.java new file mode 100644 index 000000000000..41598aa4a936 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/createMissingDeconstructionRecordClassBranches21/beforeSeveralBranches3.java @@ -0,0 +1,20 @@ +// "Create missing branches: 'Pair(A sc1, B sc2)', 'Pair(A sc1, C sc2)', ..." "true-preview" +record Pair(SC sc1, SC sc2) { + +} + +sealed interface SC{} +final class A implements SC{} +final class B implements SC{} +final class C implements SC{} + +class Test { + + void foo1(Pair o) { + switch (o) { + case Pair(A sc1, A sc2) -> System.out.println("1"); + case Pair(B sc1, B sc2) -> System.out.println("1"); + case Pair(C sc1, C sc2) -> System.out.println("1"); + } + } +} \ No newline at end of file diff --git a/plugins/InspectionGadgets/src/com/siyeh/ig/fixes/CreateMissingDeconstructionRecordClassBranchesFix.java b/plugins/InspectionGadgets/src/com/siyeh/ig/fixes/CreateMissingDeconstructionRecordClassBranchesFix.java index f33788637863..3a53ad884d8b 100644 --- a/plugins/InspectionGadgets/src/com/siyeh/ig/fixes/CreateMissingDeconstructionRecordClassBranchesFix.java +++ b/plugins/InspectionGadgets/src/com/siyeh/ig/fixes/CreateMissingDeconstructionRecordClassBranchesFix.java @@ -7,6 +7,7 @@ import com.intellij.psi.util.PsiUtil; import com.intellij.psi.util.TypeConversionUtil; import com.intellij.util.containers.ContainerUtil; import com.siyeh.InspectionGadgetsBundle; +import com.siyeh.ig.psiutils.CreateSwitchBranchesUtil; import com.siyeh.ig.psiutils.SwitchUtils; import org.jetbrains.annotations.Nls; import org.jetbrains.annotations.NotNull; @@ -18,12 +19,20 @@ import java.util.function.Function; public final class CreateMissingDeconstructionRecordClassBranchesFix extends CreateMissingSwitchBranchesFix { private final List allNames; + private final List missedShorten; private CreateMissingDeconstructionRecordClassBranchesFix(@NotNull PsiSwitchBlock block, @NotNull Set missedNames, - @NotNull List allNames) { + @NotNull List allNames, + @NotNull List missedShorten) { super(block, missedNames); this.allNames = allNames; + this.missedShorten = missedShorten; + } + + @Override + protected String getText(@NotNull PsiSwitchBlock switchBlock) { + return CreateSwitchBranchesUtil.getActionName(missedShorten.stream().sorted().toList()); } @Override @@ -82,18 +91,23 @@ public final class CreateMissingDeconstructionRecordClassBranchesFix extends Cre if (element == null) return null; allLabels.add(element.getText()); } - List missedLabels = getMissedLabels(switchBlock, missedBranches); + List missedLabels = getMissedLabels(switchBlock, missedBranches, false); if (missedLabels == null || missedLabels.isEmpty()) { return null; } allLabels.addAll(lastDeconstructionPatternIndex + 1, missedLabels); allLabels = allLabels.stream().distinct().toList(); - return new CreateMissingDeconstructionRecordClassBranchesFix(switchBlock, new HashSet<>(missedLabels), allLabels); + List shortenLabels = getMissedLabels(switchBlock, missedBranches, true); + if (shortenLabels == null) { + return null; + } + return new CreateMissingDeconstructionRecordClassBranchesFix(switchBlock, new HashSet<>(missedLabels), allLabels, shortenLabels); } @Nullable private static List getMissedLabels(@NotNull PsiSwitchBlock block, - @NotNull Map>> branchesByType) { + @NotNull Map>> branchesByType, + boolean shorten) { List result = new ArrayList<>(); JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(block.getProject()); for (Map.Entry>> branches : branchesByType.entrySet()) { @@ -114,7 +128,8 @@ public final class CreateMissingDeconstructionRecordClassBranchesFix extends Cre if (branch.size() != variableNames.size()) return null; for (int i = 0; i < branch.size(); i++) { PsiType psiType = branch.get(i); - joiner.add(psiType.getCanonicalText() + " " + variableNames.get(i)); + String typeText = shorten ? psiType.getPresentableText() : psiType.getCanonicalText(); + joiner.add(typeText + " " + variableNames.get(i)); } result.add(recordTypeString + joiner); } diff --git a/plugins/InspectionGadgets/src/com/siyeh/ig/psiutils/CreateSwitchBranchesUtil.java b/plugins/InspectionGadgets/src/com/siyeh/ig/psiutils/CreateSwitchBranchesUtil.java index 0916be92788e..18f7f15911b1 100644 --- a/plugins/InspectionGadgets/src/com/siyeh/ig/psiutils/CreateSwitchBranchesUtil.java +++ b/plugins/InspectionGadgets/src/com/siyeh/ig/psiutils/CreateSwitchBranchesUtil.java @@ -12,6 +12,7 @@ import com.intellij.openapi.project.Project; import com.intellij.openapi.util.Couple; import com.intellij.openapi.util.text.StringUtil; import com.intellij.psi.*; +import com.intellij.psi.codeStyle.JavaCodeStyleManager; import com.intellij.psi.codeStyle.VariableKind; import com.intellij.psi.util.PsiTreeUtil; import com.intellij.psi.util.PsiUtil; @@ -65,6 +66,7 @@ public final class CreateSwitchBranchesUtil { @NotNull List allNames, @NotNull Collection missingNames, @NotNull Function> caseExtractor) { + final JavaCodeStyleManager javaCodeStyleManager = JavaCodeStyleManager.getInstance(switchBlock.getProject()); boolean isRuleBasedFormat = SwitchUtils.isRuleFormatSwitch(switchBlock); final PsiCodeBlock body = switchBlock.getBody(); final PsiExpression switchExpression = switchBlock.getExpression(); @@ -87,6 +89,7 @@ public final class CreateSwitchBranchesUtil { } newStatementText.append('}'); PsiSwitchBlock block = (PsiSwitchBlock)commentTracker.replaceAndRestoreComments(switchBlock, newStatementText.toString()); + javaCodeStyleManager.shortenClassReferences(block); return PsiTreeUtil.getChildrenOfTypeAsList(block.getBody(), PsiSwitchLabelStatementBase.class); } Map prevToNext = StreamEx.of(allNames).pairMap(Couple::of).toMap(c -> c.getFirst(), c -> c.getSecond()); @@ -126,6 +129,7 @@ public final class CreateSwitchBranchesUtil { addedLabels.add(addSwitchLabelStatementBefore(missingElement, lastChild, switchBlock, isRuleBasedFormat, isPatternsGenerated)); } } + addedLabels.forEach(label -> javaCodeStyleManager.shortenClassReferences(label)); return addedLabels; }