How to Use NRefactory to Modify C# Code

317 Views Asked by At

I am trying to use NRefactory to modify an existing piece of C# code. I have tried to use the method described in this article. I'm getting an exception complaining about duplicate changes. Can someone point out what I am doing wrong, and maybe point me in the right direction?

I have the following section of code:

// contains the source code that needs to be modified
var text = "...";
// is the name of the file in which the target class exists
var fileName = "Foo";
// is the name of the target class
var className = "Foo.cs"; 

// FormattingOptions and TextEditorOptions are properties available to
// the class containing this code...

var parser = new CSharpParser();

var syntaxTree = parser.Parse(text, fileName);

syntaxTree.Freeze();

var document = new StringBuilderDocument();

document.Text = syntaxTree.ToString(FormattingOptions);

using (var documentScript = new DocumentScript(document, FormattingOptions, TextEditorOptions))
{
    var typeDeclarations = syntaxTree.Descendants.OfType<TypeDeclaration>().Where(typeDeclaration => typeDeclaration.Name == className);
    var templateProviderDeclaration = typeDeclarations.SingleOrDefault();

    if (templateProviderDeclaration != null)
    {
        var constructorDeclarations = templateProviderDeclaration.Descendants.OfType<ConstructorDeclaration>();

        foreach (var constructorDeclaration in constructorDeclarations)
        {
            var assignmentExpressions = constructorDeclaration.Descendants.OfType<AssignmentExpression>();

            foreach (var assignmentExpression in assignmentExpressions)
            {
                var leftExpression = assignmentExpression.Left;

                if (leftExpression != null && leftExpression.ToString(FormattingOptions) == "this.templates")
                {
                    var rightExpression = assignmentExpression.Right;

                    foreach (var arrayInitializerExpression in rightExpression.Children.OfType<ArrayInitializerExpression>())
                    {
                        // uses AddTemplate(), which is an extension method
                        // which takes in an expression and modifies it, and
                        // returns an expression
                        var newExpression = ((ArrayInitializerExpression)arrayInitializerExpression).AddTemplate();

                        documentScript.Replace(arrayInitializerExpression, newExpression);
                    }
                }
            }
        }
    }

    // text retrieved here is *clobbered*, by a few characters
    text = documentScript.CurrentDocument.Text;
}

Other Code...

public static class ExtensionMethods
{
    public static ArrayInitializerExpression AddTemplate(this ArrayInitializerExpression expression)
    {
        // TODO : will modify the AST for the target expression

        return (ArrayInitializerExpression)expression.Clone();
    }
}

Update:

I am trying to change a line like this:

this.templates = new Dictionary<string, ITemplate>
{
    { "Foo", new FooTemplate() }
};

to:

this.templates = new Dictionary<string, ITemplate>
{
    { "Foo", new FooTemplate() },
    { "Bar", new BarTemplate() }
};

For the purposes of this question, the exact specifics of the change I am trying to make aren't important though. I suspect that I have something not constructed quite correctly, but I cannot figure out if that's the issue.

0

There are 0 best solutions below