Surrounder () { return new JavaExpressionSurrounder() { @Override public boolean isApplicable(PsiExpression expr) { return expr.isPhysical() && SWITCH_TYPE.value(expr); } @Override public TextRange surroundExpression(Project project, Editor editor, PsiExpression expr) throws IncorrectOperationException { PsiElementFactory factory = JavaPsiFacade.getElementFactory(project); CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(project); PsiElement parent = expr.getParent(); if (parent instanceof PsiExpressionStatement) { PsiSwitchStatement switchStatement = (PsiSwitchStatement)factory.createStatementFromText("switch(1){case 1:}", null); return postprocessSwitch(editor, expr, codeStyleManager, parent, switchStatement); } else if (HighlightingFeature.ENHANCED_SWITCH.isAvailable(expr)) { PsiSwitchExpression switchExpression = (PsiSwitchExpression)factory.createExpressionFromText("switch(1){case 1->1;}", null); return postprocessSwitch(editor, expr, codeStyleManager, expr, switchExpression); } return TextRange.from(editor.getCaretModel().getOffset(), 0); } @NotNull private static TextRange postprocessSwitch(Editor editor, PsiExpression expr, CodeStyleManager codeStyleManager, PsiElement toReplace, PsiSwitchBlock switchBlock) { switchBlock = (PsiSwitchBlock)codeStyleManager.reformat(switchBlock); PsiExpression selectorExpression = switchBlock.getExpression(); if (selectorExpression != null) { selectorExpression.replace(expr); } switchBlock = (PsiSwitchBlock)toReplace.replace(switchBlock); PsiCodeBlock body = switchBlock.getBody(); if (body != null) { body = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(body); if (body != null) { TextRange range = body.getStatements()[0].getTextRange(); editor.getDocument().deleteString(range.getStartOffset(), range.getEndOffset()); return TextRange.from(range.getStartOffset(), 0); } } return TextRange.from(editor.getCaretModel().getOffset(), 0); } @Override public String getTemplateDescription() { return JavaBundle.message("switch.stmt.template.description"); } }; }