PsiSwitchLabeledRuleStatement (@NotNull PsiCaseLabelElement labelElement, @NotNull PsiSwitchLabeledRuleStatement labeledRule) { Project project = labeledRule.getProject(); PsiElementFactory factory = JavaPsiFacade.getElementFactory(project); PsiSwitchLabeledRuleStatement newRule; if (labelElement instanceof PsiDefaultCaseLabelElement) { newRule = (PsiSwitchLabeledRuleStatement)factory.createStatementFromText("default->{}", null); } else { PsiElement defaultElement = SwitchUtils.findDefaultElement(labeledRule); if (defaultElement != null) { return moveRuleAfter((PsiCaseLabelElement)defaultElement, labeledRule); } var guard = labelElement.getParent().getParent() instanceof PsiSwitchLabelStatementBase label ? label.getGuardExpression() : null; newRule = (PsiSwitchLabeledRuleStatement)factory.createStatementFromText( "case 1" + (guard == null ? "" : " when " + guard.getText()) + "->{}", null); Objects.requireNonNull(newRule.getCaseLabelElementList()).getElements()[0].replace(labelElement); } newRule = (PsiSwitchLabeledRuleStatement)labeledRule.getParent().addAfter(newRule, labeledRule); newRule = (PsiSwitchLabeledRuleStatement)CodeStyleManager.getInstance(project).reformat(newRule); Objects.requireNonNull(newRule.getBody()).replace(Objects.requireNonNull(labeledRule.getBody())); labelElement.delete(); return newRule; }