@@ -0,0 +1,1758 @@
+package com.jetbrains.python.psi.impl;%0A%0Aimport com.intellij.openapi.util.text.StringUtil;%0Aimport com.intellij.psi.PsiElement;%0Aimport com.jetbrains.python.PythonLanguage;%0Aimport com.jetbrains.python.psi.PyElementGenerator;%0Aimport com.jetbrains.python.psi.PyFunction;%0A%0Aimport java.util.ArrayList;%0Aimport java.util.Collections;%0Aimport java.util.List;%0A%0A/**%0A * @author yole%0A */%0Apublic class PyFunctionBuilder %7B%0A  private String myName;%0A  private List%3CString%3E myParameters = new ArrayList%3CString%3E();%0A  private List%3CString%3E myStatements = new ArrayList%3CString%3E();%0A%0A  public PyFunctionBuilder(String name) %7B%0A    myName = name;%0A  %7D%0A%0A  public PyFunctionBuilder parameter(String baseName) %7B%0A    String name = baseName;%0A    int uniqueIndex = 0;%0A    while(myParameters.contains(name)) %7B%0A      uniqueIndex++;%0A      name = baseName + uniqueIndex;%0A    %7D%0A    myParameters.add(name);%0A    return this;%0A  %7D%0A%0A  public PyFunctionBuilder statement(String text) %7B%0A    myStatements.add(text);%0A    return this;%0A  %7D%0A%0A  public PyFunction addFunction(PsiElement target) %7B%0A    String text = buildText();%0A    PyElementGenerator generator = PythonLanguage.getInstance().getElementGenerator();%0A    PyFunction function = generator.createFromText(target.getProject(), PyFunction.class, text);%0A    return (PyFunction) target.add(function);%0A  %7D%0A%0A  private String buildText() %7B%0A    StringBuilder builder = new StringBuilder(%22def %22);%0A    builder.append(myName).append(%22(%22);%0A    builder.append(StringUtil.join(myParameters, %22, %22));%0A    builder.append(%22):%22);%0A    List%3CString%3E statements = myStatements.isEmpty() ? Collections.singletonList(%22pass%22) : myStatements;%0A    for (String statement : statements) %7B%0A      builder.append(%22%5Cn    %22).append(statement);%0A    %7D%0A    return builder.toString();%0A  %7D%0A%7D%0A

create view method fix fixed and refactored (PY-487)