public Document GetRefactoredDocument_new(CancellationToken cancellationToken)
        {
            lock (this) {
                var start = DateTime.Now;

                Utils.Trace(string.Format("Trying to extract method, span: {0}", this.textSpan));

                var localDocument = this.document;

                var extracted = ExtractMethodService.ExtractMethod(localDocument, this.textSpan, new ExtractMethodOptions(true), cancellationToken);
                Utils.TraceTime("Roslyn extract method", DateTime.Now - start);
                if (!extracted.Succeeded)
                {
                    Utils.Trace("Method Extraction failed!");
                    return(null);
                }
                Utils.Trace("Extract method: ok");
                var clock = DateTime.Now;

                var root = extracted.Document.GetSyntaxRoot(cancellationToken);
                var closestEnclosingStatement = extracted.InvocationNameToken.Parent.FirstAncestorOrSelf <StatementSyntax>();
                if (closestEnclosingStatement == null)
                {
                    return(null);
                }

                var newMethod = extracted.MethodDeclarationNode as MethodDeclarationSyntax;
                if (newMethod == null)
                {
                    return(null);
                }
                var body = newMethod.Body;


                Utils.Trace("Generating the Precondition marker");

                var actualParameters = "";
                var formalParameters = "";

                var ps = newMethod.ParameterList.Parameters;
                for (int i = 0, j = 0, n = ps.Count; i < n; i++)
                {
                    var p = ps[i];
                    {
                        var modifiers = "";

                        foreach (var modifier in p.Modifiers)
                        {
                            modifiers = modifiers + modifier.ValueText + " ";
                        }

                        // Avoid the refs when emitting the condition
                        if (!String.IsNullOrEmpty(modifiers))
                        {
                            if (0 < j)
                            {
                                actualParameters += ",";
                                formalParameters += ",";
                            }

                            var name = p.Identifier.ValueText;
                            actualParameters += name;
                            formalParameters += p.Type + " " + name;

                            j++;
                        }
                    }
                }

                // todo: just use invocation, but replace the method name with __PreconditionMarker!
                var preconditionMarkerString = "__PreconditionMarker(" + actualParameters + ");";
                var preconditionMarker       = Formatter.Annotation.AddAnnotationTo(SyntaxFactory.ParseStatement(preconditionMarkerString));

                var postconditionMarker = Formatter.Annotation.AddAnnotationTo(SyntaxFactory.ParseStatement("Console.WriteLine(5);\n"));


                var b = SyntaxFactory.Block(preconditionMarker, closestEnclosingStatement, postconditionMarker);


                //var finalRoot = ((SyntaxNode)root).ReplaceNode(
                //     closestEnclosingStatement,
                //     b.WithAdditionalAnnotations(CodeActionAnnotations.CreateRenameAnnotation())
                //     );

                Utils.Trace("Adding dummy methods");

                var preconditionMarkerDefString = "[Pure]\npublic void __PreconditionMarker(" + formalParameters + "){}\n";
                var preconditionMarkerDef       = SyntaxFactory.MethodDeclaration(null, new SyntaxTokenList(), SyntaxFactory.ParseTypeName("void"), null, SyntaxFactory.ParseToken("__PreconditionMarker"), null, SyntaxFactory.ParseParameterList("(" + formalParameters + ")"), null, SyntaxFactory.Block());

                var r1       = (SyntaxNode)root;
                var classDef = closestEnclosingStatement.FirstAncestorOrSelf <TypeDeclarationSyntax>();
                var r2       = new AddMarkerMethods(classDef, preconditionMarkerDef).Visit(r1);
                var r3       = r2.ReplaceNode(
                    closestEnclosingStatement,
                    b.WithAdditionalAnnotations(CodeActionAnnotations.CreateRenameAnnotation())
                    );
                return(this.document.WithSyntaxRoot(r3));


#if false
                var resultingTree = (SyntaxNode)extracted.MethodDeclarationNode;

                var newMethodIdentified = (SyntaxToken)extracted.InvocationNameToken;


                var oldTree = localDocument.GetSyntaxTree(cancellationToken);
                if (oldTree == null)
                {
                    return(null);
                }

                Utils.Trace("Got the original syntax tree");
                var oldRoot = oldTree.GetRoot(cancellationToken) as SyntaxNode;
#endif
            }
        }
    public Document GetRefactoredDocument_new(CancellationToken cancellationToken) {

      if (this.textSpan.IsEmpty)
      {        
        return null;
      }

      lock (this) {
        var start = DateTime.Now;

        Utils.Trace(string.Format("Trying to extract method, span: {0}", this.textSpan));

        var localDocument = this.document;

        var extracted = ExtractMethodService.ExtractMethodAsync(localDocument, this.textSpan, new ExtractMethodOptions(true), cancellationToken).Result;
        Utils.TraceTime("Roslyn extract method", DateTime.Now - start);
        if (!extracted.Succeeded) {
          Utils.Trace("Method Extraction failed!");
          return null;
        }
        Utils.Trace("Extract method: ok");
        var clock = DateTime.Now;

        var root = extracted.Document.GetSyntaxRoot(cancellationToken);
        var closestEnclosingStatement = extracted.InvocationNameToken.Parent.FirstAncestorOrSelf<StatementSyntax>();
        if (closestEnclosingStatement == null) return null;

        var newMethod = extracted.MethodDeclarationNode as MethodDeclarationSyntax;
        if (newMethod == null) return null;
        var body = newMethod.Body;


        Utils.Trace("Generating the Precondition marker");

        var actualParameters = "";
        var formalParameters = "";

        var ps = newMethod.ParameterList.Parameters;
        for (int i = 0, j = 0, n = ps.Count; i < n; i++) {
          var p = ps[i];
          {
            var modifiers = "";

            foreach (var modifier in p.Modifiers) {
              modifiers = modifiers + modifier.ValueText + " ";
            }

            // Avoid the refs when emitting the condition
            if (!String.IsNullOrEmpty(modifiers)) {
              if (0 < j) {
                actualParameters += ",";
                formalParameters += ",";
              }

              var name = p.Identifier.ValueText;
              actualParameters += name;
              formalParameters += p.Type + " " + name;

              j++;
            }
          }
        }

        // todo: just use invocation, but replace the method name with __PreconditionMarker!
        var preconditionMarkerString = "__PreconditionMarker(" + actualParameters + ");";
        var preconditionMarker = Formatter.Annotation.AddAnnotationTo(SyntaxFactory.ParseStatement(preconditionMarkerString));

        var postconditionMarker = Formatter.Annotation.AddAnnotationTo(SyntaxFactory.ParseStatement("Console.WriteLine(5);\n"));


        var b = SyntaxFactory.Block(preconditionMarker, closestEnclosingStatement, postconditionMarker);


        //var finalRoot = ((SyntaxNode)root).ReplaceNode(
        //     closestEnclosingStatement,
        //     b.WithAdditionalAnnotations(CodeActionAnnotations.CreateRenameAnnotation())
        //     );

        Utils.Trace("Adding dummy methods");

        var preconditionMarkerDefString = "[Pure]\npublic static void __PreconditionMarker(" + formalParameters + "){}\n";
        var preconditionMarkerDef = SyntaxFactory.MethodDeclaration(null, new SyntaxTokenList(), SyntaxFactory.ParseTypeName("void"), null, SyntaxFactory.ParseToken("__PreconditionMarker"), null, SyntaxFactory.ParseParameterList("(" + formalParameters + ")"), null, SyntaxFactory.Block());

        var r1 = (SyntaxNode)root;
        var classDef = closestEnclosingStatement.FirstAncestorOrSelf<TypeDeclarationSyntax>();
        var r2 = new AddMarkerMethods(classDef, preconditionMarkerDef).Visit(r1);
        var r3 = r2.ReplaceNode(
             closestEnclosingStatement,
             b.WithAdditionalAnnotations(CodeActionAnnotations.CreateRenameAnnotation())
             );
        return this.document.WithSyntaxRoot(r3);

 
#if false



        var resultingTree = (SyntaxNode)extracted.MethodDeclarationNode;

        var newMethodIdentified = (SyntaxToken)extracted.InvocationNameToken;


        var oldTree = localDocument.GetSyntaxTree(cancellationToken);
        if (oldTree == null) return null;
        
        Utils.Trace("Got the original syntax tree");
        var oldRoot = oldTree.GetRoot(cancellationToken) as SyntaxNode;



#endif
      }
    }