PyFunction (@NotNull final String methodName, final AbstractVariableData @NotNull [] variableData, @NotNull final List<PsiElement> elementsRange, @Nullable PyUtil.MethodFlags flags, boolean isAsync) { assert !elementsRange.isEmpty() : "Empty statements list was selected!"; final PyFunctionBuilder builder = new PyFunctionBuilder(methodName, elementsRange.get(0)); if (isAsync) { builder.makeAsync(); } addDecorators(builder, flags); addFakeParameters(builder, variableData); final PyFunction method = builder.buildFunction(); final PyStatementList statementList = method.getStatementList(); for (PsiElement element : elementsRange) { if (element instanceof PsiWhiteSpace) { continue; } statementList.add(element); } // remove last instruction final PsiElement child = statementList.getFirstChild(); if (child != null) { child.delete(); } PsiElement last = statementList; while (last != null) { last = last.getLastChild(); if (last instanceof PsiWhiteSpace) { last.delete(); } } return method; }