TextRange (@NotNull Project project, @NotNull Editor editor, @NotNull PsiElement container, PsiElement @NotNull [] statements) { statements = MoveDeclarationsOutHelperKt.move(container, statements, true); if (statements.length == 0) { return null; } KtPsiFactory psiFactory = new KtPsiFactory(project); KtCallExpression callExpression = (KtCallExpression) psiFactory.createExpression("run {\n}"); callExpression = (KtCallExpression) container.addAfter(callExpression, statements[statements.length - 1]); container.addBefore(psiFactory.createWhiteSpace(), callExpression); KtLambdaExpression bodyExpression = callExpression.getLambdaArguments().get(0).getLambdaExpression(); assert bodyExpression != null : "Body expression should exists for " + callExpression.getText(); KtBlockExpression blockExpression = bodyExpression.getBodyExpression(); assert blockExpression != null : "JetBlockExpression should exists for " + callExpression.getText(); //Add statements in function literal block addStatementsInBlock(blockExpression, statements); //Delete statements from original code container.deleteChildRange(statements[0], statements[statements.length - 1]); callExpression = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(callExpression); assert callExpression != null; KtExpression literalName = callExpression.getCalleeExpression(); assert literalName != null : "Run expression should have callee expression " + callExpression.getText(); return literalName.getTextRange(); }