12

I'm trying to learn Roslyn by building an existing but simple application from the ground up, which seems to be a productive way to learn this. Anyhow, I have the following code:

var root = (CompilationUnitSyntax)document.GetSyntaxRoot();

    // Add the namespace
    var namespaceAnnotation = new SyntaxAnnotation();
    root = root.WithMembers(
        Syntax.NamespaceDeclaration(
            Syntax.ParseName("ACO"))
                .NormalizeWhitespace()
                .WithAdditionalAnnotations(namespaceAnnotation));
    document = document.UpdateSyntaxRoot(root);

    // Add a class to the newly created namespace, and update the document
    var namespaceNode = (NamespaceDeclarationSyntax)root
        .GetAnnotatedNodesAndTokens(namespaceAnnotation)
        .Single()
        .AsNode();

    var classAnnotation = new SyntaxAnnotation();
    var baseTypeName = Syntax.ParseTypeName("System.Windows.Forms.Form");
    SyntaxTokenList syntaxTokenList = new SyntaxTokenList()
        {
            Syntax.Token(SyntaxKind.PublicKeyword)
        };

    var newNamespaceNode = namespaceNode
        .WithMembers(
            Syntax.List<MemberDeclarationSyntax>(
                Syntax.ClassDeclaration("MainForm")
                    .WithAdditionalAnnotations(classAnnotation)
                    .AddBaseListTypes(baseTypeName)
                    .WithModifiers(Syntax.Token(SyntaxKind.PublicKeyword))));

    root = root.ReplaceNode(namespaceNode, newNamespaceNode).NormalizeWhitespace();
    document = document.UpdateSyntaxRoot(root);


    var attributes = Syntax.List(Syntax.AttributeDeclaration(Syntax.SeparatedList(Syntax.Attribute(Syntax.ParseName("STAThread")))));


    // Find the class just created, add a method to it and update the document
    var classNode = (ClassDeclarationSyntax)root
        .GetAnnotatedNodesAndTokens(classAnnotation)
        .Single()
        .AsNode();

        var syntaxList = Syntax.List<MemberDeclarationSyntax>(
                Syntax.MethodDeclaration(
                    Syntax.ParseTypeName("void"), "Main")
                    .WithModifiers(Syntax.TokenList(Syntax.Token(SyntaxKind.PublicKeyword)))
                    .WithAttributes(attributes)
                    .WithBody(
                        Syntax.Block()));
        syntaxList.Add(Syntax.PropertyDeclaration(Syntax.ParseTypeName("System.Windows.Forms.Timer"), "Ticker"));
        var newClassNode = classNode
            .WithMembers(syntaxList);

    root = root.ReplaceNode(classNode, newClassNode).NormalizeWhitespace();
    document = document.UpdateSyntaxRoot(root);

Which outputs the following code in the IDocument.

namespace ACO
{
    public class MainForm : System.Windows.Forms.Form
    {
        [STAThread]
        public void Main()
        {
        }
    }
}

Although it should look more like this (notice I tried to add the Timer property)

namespace ACO
{
    public class MainForm : System.Windows.Forms.Form
    {
    public System.Windows.Forms.Timer Ticker {get; set;}

        [STAThread]
        public void Main()
        {
        }
    }
}

Also, it appears that the code I'm writing for such a simple process seems excessive. In addition to my main question can people offer suggestions on how I can go about this in a more elegant manner? Maybe a link to a blog, or code snippets or something?


It turns out that I needed to change this line:

syntaxList.Add(Syntax.PropertyDeclaration(Syntax.ParseTypeName("System.Windows.Forms.Timer"), "Ticker"));

To this line:

syntaxList = syntaxList.Add(Syntax.PropertyDeclaration(Syntax.ParseTypeName("System.Windows.Forms.Timer"), "Ticker"));

However, now I get this output:

namespace ACO
{
    public class MainForm : System.Windows.Forms.Form
    {
        [STAThread]
        public void Main()
        {
        }

        System.Windows.Forms.Timer Ticker
        {
        }
    }
}

Now I'm not getting the "get; set;" text within the property. Does anyone know what I am missing?

casperOne
  • 73,706
  • 19
  • 184
  • 253
Beaker
  • 2,804
  • 5
  • 33
  • 54

2 Answers2

7

I think the reason why the property wasn't added is that SyntaxLists, like everything else in Roslyn, are immutable. Doesn't Add() return the updated SyntaxList? (I can't verify this right now, I haven't switched to the new CTP yet.)

And code like this in Roslyn can be very verbose, but you're making it more complicated than necessary. You don't have to update root after every change, if you build the syntax tree from bottom up: first create the members of the class, then the class, then the namespace. If you do that, you won't have to deal with all the annotations at all.

svick
  • 236,525
  • 50
  • 385
  • 514
  • Your reminder of immutability lead me to realize what I was doing wrong which I posted below. I will ask a separate question about how to actually build my particular Syntax Tree from the bottom up. – Beaker Jul 05 '12 at 20:09
  • Here is the link to my new question on how do I go about building my Syntax Tree from the ground up as you suggested. http://stackoverflow.com/questions/11351977/roslyn-ctp-building-a-syntaxtree-from-the-ground-up – Beaker Jul 05 '12 at 20:19
  • 1
    In that other question of yours I've added a link to a tool called Quoter that can help in automatically generating Roslyn syntax API calls for any program: http://blogs.msdn.com/b/kirillosenkov/archive/2012/07/22/roslyn-code-quoter-tool-generating-syntax-tree-api-calls-for-any-c-program.aspx – Kirill Osenkov Jul 23 '12 at 05:31
7

Using the new fluent API (.With...() methods) you can now use:

Syntax.PropertyDeclaration(Syntax.ParseTypeName("int"), "MyProperty")
      .WithAccessorList(
          Syntax.AccessorList(
              Syntax.List(
                  Syntax.AccessorDeclaration(SyntaxKind.GetAccessorDeclaration)
                        .WithSemicolonToken(Syntax.Token(SyntaxKind.SemicolonToken)),
                  Syntax.AccessorDeclaration(SyntaxKind.SetAccessorDeclaration)
                        .WithSemicolonToken(Syntax.Token(SyntaxKind.SemicolonToken)))));
Maciej Wozniak
  • 1,174
  • 15
  • 27