PsiIfStatement (Project project, PsiElement container, PsiElement[] statements, String condition) { statements = SurroundWithUtil.moveDeclarationsOut(container, statements, true); if (statements.length == 0) { return null; } @NonNls String text = "if(" + condition + "){\n}"; PsiIfStatement ifStatement = (PsiIfStatement)JavaPsiFacade.getElementFactory(project).createStatementFromText(text, null); ifStatement = (PsiIfStatement)CodeStyleManager.getInstance(project).reformat(ifStatement); ifStatement = (PsiIfStatement)addAfter(ifStatement, container, statements); final PsiStatement thenBranch = ifStatement.getThenBranch(); if (thenBranch != null) { PsiCodeBlock thenBlock = ((PsiBlockStatement)thenBranch).getCodeBlock(); SurroundWithUtil.indentCommentIfNecessary(thenBlock, statements); addRangeWithinContainer(thenBlock, container, statements, true); container.deleteChildRange(statements[0], statements[statements.length - 1]); } return ifStatement; }