@Override
 protected String getInitialFileContent(final IFile newFileHandle, final SubMonitor m) {
   final String lineDelimiter = TextUtil.getLineDelimiter(newFileHandle.getProject());
   final IRSourceUnit su =
       (IRSourceUnit)
           LTK.getSourceUnitManager()
               .getSourceUnit(
                   LTK.PERSISTENCE_CONTEXT,
                   newFileHandle,
                   getContentType(newFileHandle),
                   true,
                   m);
   try {
     final EvaluatedTemplate data = CodeGeneration.getNewRFileContent(su, lineDelimiter);
     if (data != null) {
       fInitialSelection = data.getRegionToSelect();
       return data.getContent();
     }
   } catch (final CoreException e) {
     RUIPlugin.logError(
         ICommonStatusConstants.INTERNAL_TEMPLATE,
         "An error occured when applying template to new R script file.",
         e); //$NON-NLS-1$
   } finally {
     if (su != null) {
       su.disconnect(m);
     }
   }
   return null;
 }
  @Override
  public RefactoringStatus checkInitialConditions(final IProgressMonitor monitor)
      throws CoreException {
    final SubMonitor progress = SubMonitor.convert(monitor, 6);
    try {
      if (fSelectionRegion != null) {
        fSourceUnit.connect(progress.newChild(1));
        try {
          final AbstractDocument document = fSourceUnit.getDocument(monitor);

          final IRModelInfo modelInfo =
              (IRModelInfo)
                  fSourceUnit.getModelInfo(
                      RModel.TYPE_ID, IRModelManager.MODEL_FILE, progress.newChild(1));
          if (modelInfo != null) {
            final IRegion region = fAdapter.trimToAstRegion(document, fSelectionRegion);
            ISourceStructElement element =
                LTKUtil.getCoveringSourceElement(modelInfo.getSourceElement(), region);
            while (element != null) {
              if (element instanceof IRMethod) {
                fFunction = (IRMethod) element;
                break;
              }
              element = element.getSourceParent();
            }
          }

          if (fFunction != null) {
            final ISourceStructElement source = (ISourceStructElement) fFunction;
            fOperationRegion =
                fAdapter.expandSelectionRegion(document, source.getSourceRange(), fSelectionRegion);
          }
        } finally {
          fSourceUnit.disconnect(progress.newChild(1));
        }
      }

      if (fFunction == null) {
        return RefactoringStatus.createFatalErrorStatus(
            Messages.FunctionToS4Method_error_InvalidSelection_message);
      }
      final RefactoringStatus result = new RefactoringStatus();
      fAdapter.checkInitialForModification(result, fElementSet);
      progress.worked(1);

      if (result.hasFatalError()) {
        return result;
      }

      checkFunction(result);
      progress.worked(2);
      return result;
    } finally {
      progress.done();
    }
  }
  @Override
  public RefactoringStatus checkInitialConditions(final IProgressMonitor monitor)
      throws CoreException {
    final SubMonitor progress = SubMonitor.convert(monitor, 6);
    RAstNode rootNode = null;
    try {
      if (fSelectionRegion != null) {
        fSourceUnit.connect(progress.newChild(1));
        try {
          final AbstractDocument document = fSourceUnit.getDocument(monitor);
          final RHeuristicTokenScanner scanner = fAdapter.getScanner(fSourceUnit);

          final IRModelInfo modelInfo =
              (IRModelInfo)
                  fSourceUnit.getModelInfo(
                      RModel.TYPE_ID, IRModelManager.MODEL_FILE, progress.newChild(1));
          if (modelInfo != null) {
            final IRegion region = fAdapter.trimToAstRegion(document, fSelectionRegion, scanner);
            final AstInfo ast = modelInfo.getAst();
            if (ast != null) {
              rootNode =
                  (RAstNode)
                      AstSelection.search(
                              ast.root,
                              region.getOffset(),
                              region.getOffset() + region.getLength(),
                              AstSelection.MODE_COVERING_SAME_LAST)
                          .getCovering();
            }
          }
        } finally {
          fSourceUnit.disconnect(progress.newChild(1));
        }
      }

      if (rootNode == null) {
        return RefactoringStatus.createFatalErrorStatus(
            Messages.ExtractTemp_error_InvalidSelection_message);
      }
      final RefactoringStatus result = new RefactoringStatus();
      fAdapter.checkInitialToModify(result, fElementSet);
      progress.worked(1);

      if (result.hasFatalError()) {
        return result;
      }

      searchVariables(rootNode, result);
      progress.worked(2);
      return result;
    } finally {
      progress.done();
    }
  }
 private List<String> createChanges(final TextFileChange change, final SubMonitor progress)
     throws BadLocationException {
   final List<String> names = new ArrayList<String>();
   int remaining = fVariablesList.size() + 3;
   progress.setWorkRemaining(remaining);
   remaining -= 3;
   fSourceUnit.connect(progress.newChild(2));
   try {
     for (final Map<String, Variable> frameList : fVariablesList.values()) {
       progress.setWorkRemaining(remaining--);
       createMainChanges(frameList, change, names);
     }
     return names;
   } finally {
     fSourceUnit.disconnect(progress.newChild(1));
   }
 }
  @Override
  public Change createChange(final IProgressMonitor monitor) throws CoreException {
    try {
      final SubMonitor progress =
          SubMonitor.convert(monitor, RefactoringMessages.Common_CreateChanges_label, 3);

      final TextFileChange textFileChange = new SourceUnitChange(fSourceUnit);
      if (fSourceUnit.getWorkingContext() == LTK.EDITOR_CONTEXT) {
        textFileChange.setSaveMode(TextFileChange.LEAVE_DIRTY);
      }
      createChanges(textFileChange, progress.newChild(1));

      final Map<String, String> arguments = new HashMap<String, String>();
      final String description =
          NLS.bind(
              Messages.FunctionToS4Method_Descriptor_description,
              RRefactoringAdapter.getQuotedIdentifier(fFunctionName));
      final IProject resource = fElementSet.getSingleProject();
      final String project = (resource != null) ? resource.getName() : null;
      final String source =
          (project != null)
              ? NLS.bind(RefactoringMessages.Common_Source_Project_label, project)
              : RefactoringMessages.Common_Source_Workspace_label;
      final int flags = 0;
      final String comment = ""; // $NON-NLS-1$
      final CommonRefactoringDescriptor descriptor =
          new CommonRefactoringDescriptor(
              getIdentifier(), project, description, comment, arguments, flags);

      return new RefactoringChange(
          descriptor, Messages.FunctionToS4Method_label, new Change[] {textFileChange});
    } catch (final BadLocationException e) {
      throw new CoreException(
          new Status(IStatus.ERROR, RCore.PLUGIN_ID, "Unexpected error (concurrent change?)", e));
    } finally {
      monitor.done();
    }
  }
  private void createChanges(final TextFileChange change, final SubMonitor progress)
      throws BadLocationException, CoreException {
    fSourceUnit.connect(progress.newChild(1));
    try {
      final AbstractDocument document = fSourceUnit.getDocument(progress.newChild(1));
      final RCodeStyleSettings codeStyle = RRefactoringAdapter.getCodeStyle(fSourceUnit);

      RAstNode firstParentChild = (RAstNode) fFunction.getAdapter(IAstNode.class);
      while (true) {
        final RAstNode parent = firstParentChild.getRParent();
        if (parent == null
            || parent.getNodeType() == NodeType.SOURCELINES
            || parent.getNodeType() == NodeType.BLOCK) {
          break;
        }
        firstParentChild = parent;
      }

      final IRegion region = fAdapter.expandWhitespaceBlock(document, fOperationRegion);
      final int insertOffset =
          fAdapter
              .expandWhitespaceBlock(
                  document,
                  fAdapter.expandSelectionRegion(
                      document, new Region(firstParentChild.getOffset(), 0), fOperationRegion))
              .getOffset();
      final FDef fdefNode = (FDef) fFunction.getAdapter(FDef.class);
      final IRegion fbodyRegion =
          fAdapter.expandWhitespaceBlock(
              document,
              fAdapter.expandSelectionRegion(document, fdefNode.getContChild(), fOperationRegion));

      TextChangeCompatibility.addTextEdit(
          change,
          Messages.FunctionToS4Method_Changes_DeleteOld_name,
          new DeleteEdit(region.getOffset(), region.getLength()));

      final String nl = document.getDefaultLineDelimiter();
      final String argAssign = codeStyle.getArgAssignString();

      final StringBuilder sb = new StringBuilder();
      sb.append("setGeneric(\""); // $NON-NLS-1$
      sb.append(fFunctionName);
      sb.append("\","); // $NON-NLS-1$
      sb.append(nl);
      sb.append("function("); // $NON-NLS-1$
      boolean dots = false;
      for (final Variable variable : fVariablesList) {
        if (variable.getName().equals(RTerminal.S_ELLIPSIS)) {
          dots = true;
        }
        if (variable.getUseAsGenericArgument()) {
          sb.append(
              RElementName.create(RElementName.MAIN_DEFAULT, variable.getName()).getDisplayName());
          sb.append(", "); // $NON-NLS-1$
        }
      }
      if (!dots) {
        sb.append("..., "); // $NON-NLS-1$
      }
      sb.delete(sb.length() - 2, sb.length());
      sb.append(')');
      if (codeStyle.getNewlineFDefBodyBlockBefore()) {
        sb.append(nl);
      } else {
        sb.append(' ');
      }
      sb.append('{');
      sb.append(nl);
      sb.append("standardGeneric(\""); // $NON-NLS-1$
      sb.append(fFunctionName);
      sb.append("\")"); // $NON-NLS-1$
      sb.append(nl);
      sb.append("})"); // $NON-NLS-1$
      sb.append(nl);
      sb.append(nl);
      final String genericDef =
          RRefactoringAdapter.indent(sb, document, firstParentChild.getOffset(), fSourceUnit);
      TextChangeCompatibility.addTextEdit(
          change,
          Messages.FunctionToS4Method_Changes_AddGenericDef_name,
          new InsertEdit(insertOffset, genericDef));

      sb.setLength(0);
      sb.append("setMethod(\""); // $NON-NLS-1$
      sb.append(fFunctionName);
      sb.append("\","); // $NON-NLS-1$
      sb.append(nl);
      sb.append("signature("); // $NON-NLS-1$
      boolean hasType = false;
      for (final Variable variable : fVariablesList) {
        if (variable.getUseAsGenericArgument() && variable.getArgumentType() != null) {
          hasType = true;
          sb.append(
              RElementName.create(RElementName.MAIN_DEFAULT, variable.getName()).getDisplayName());
          sb.append(argAssign);
          sb.append("\""); // $NON-NLS-1$
          sb.append(variable.getArgumentType());
          sb.append("\", "); // $NON-NLS-1$
        }
      }
      if (hasType) {
        sb.delete(sb.length() - 2, sb.length());
      }
      sb.append("),"); // $NON-NLS-1$
      sb.append(nl);
      sb.append("function("); // $NON-NLS-1$
      final FDef.Args argsNode = fdefNode.getArgsChild();
      for (final Variable variable : fVariablesList) {
        sb.append(
            RElementName.create(RElementName.MAIN_DEFAULT, variable.getName()).getDisplayName());
        final FDef.Arg argNode = argsNode.getChild(variable.fArg.index);
        if (argNode.hasDefault()) {
          sb.append(argAssign);
          sb.append(
              document.get(
                  argNode.getDefaultChild().getOffset(), argNode.getDefaultChild().getLength()));
        }
        sb.append(", "); // $NON-NLS-1$
      }
      if (!fVariablesList.isEmpty()) {
        sb.delete(sb.length() - 2, sb.length());
      }
      sb.append(')');
      if (codeStyle.getNewlineFDefBodyBlockBefore()
          || fdefNode.getContChild().getNodeType() != NodeType.BLOCK) {
        sb.append(nl);
      } else {
        sb.append(' ');
      }
      sb.append(document.get(fbodyRegion.getOffset(), fbodyRegion.getLength()).trim());
      sb.append(")"); // $NON-NLS-1$
      sb.append(nl);
      final String methodDef =
          RRefactoringAdapter.indent(sb, document, firstParentChild.getOffset(), fSourceUnit);
      TextChangeCompatibility.addTextEdit(
          change,
          Messages.FunctionToS4Method_Changes_AddMethodDef_name,
          new InsertEdit(insertOffset, methodDef));
    } finally {
      fSourceUnit.disconnect(progress.newChild(1));
    }
  }