TextRange (@NotNull Project project, @NotNull Editor editor, @NotNull PsiElement container, PsiElement @NotNull [] statements) { statements = MoveDeclarationsOutHelperKt.move(container, statements, isGenerateDefaultInitializers()); if (statements.length == 0) { return null; } KtIfExpression ifExpression = (KtIfExpression) new KtPsiFactory(project).createExpression(getCodeTemplate()); ifExpression = (KtIfExpression) container.addAfter(ifExpression, statements[statements.length - 1]); // TODO move a comment for first statement KtBlockExpression thenBranch = (KtBlockExpression) ifExpression.getThen(); assert thenBranch != null : "Then branch should exist for created if expression: " + ifExpression.getText(); // Add statements in then branch of created if addStatementsInBlock(thenBranch, statements); // Delete statements from original code container.deleteChildRange(statements[0], statements[statements.length - 1]); ifExpression = CodeInsightUtilBase.forcePsiPostprocessAndRestoreElement(ifExpression); if (ifExpression == null) { return null; } return getRange(editor, ifExpression); }