private static GroovyFile generateNewScript(GroovyFile file, PsiPackage newPackage) {
    for (GrImportStatement importStatement : file.getImportStatements()) {
      importStatement.delete();
    }
    final GroovyFile newFile = GroovyPsiElementFactory.getInstance(file.getProject()).createGroovyFile("", true, null);

    newFile.addRange(file.getFirstChild(), file.getLastChild());

    final PsiClass[] newFileClasses = newFile.getClasses();
    for (PsiClass psiClass : newFileClasses) {
      if (psiClass instanceof GroovyScriptClass) continue;
      final GrDocComment docComment = GrDocCommentUtil.findDocComment((GrDocCommentOwner)psiClass);
      if (docComment != null) docComment.delete();
      psiClass.delete();
    }

    final GrPackageDefinition packageDefinition = newFile.getPackageDefinition();
    if (packageDefinition != null) packageDefinition.delete();

    PsiElement cur = newFile.getFirstChild();
    while (cur != null && PsiImplUtil.isWhiteSpaceOrNls(cur)) {
      cur = cur.getNextSibling();
    }
    if (cur != null && cur != newFile.getFirstChild()) {
      cur = cur.getPrevSibling();
      newFile.deleteChildRange(newFile.getFirstChild(), cur);
    }

    cur = newFile.getLastChild();
    while (cur != null && PsiImplUtil.isWhiteSpaceOrNls(cur)) {
      cur = cur.getPrevSibling();
    }
    if (cur != null && cur != newFile.getLastChild()) {
      cur = cur.getNextSibling();
      newFile.deleteChildRange(cur, newFile.getLastChild());
    }

    newFile.setName(file.getName());
    setPackageDefinition(file, newFile, newPackage.getQualifiedName());

    GroovyChangeContextUtil.decodeContextInfo(newFile, null, null);
    return newFile;
  }
private static GroovyFile generateNewScript(GroovyFile file, PsiPackage newPackage)