none
How to insert a new UsingDirective and a method in the code.

    Question

  • For example, if my code is like 

     

    ------------------------------------------------------------------

    using system;

    namespace abcd

    {

    class xyz

    {

    }

    };

    ------------------------------------------------------------------

     

    I want to add a new user directives as well as new method into the tree. 

    Like I want to read the code into compilationUnit. And while parsing the nodes, I want to add a new directive, and add a method and then write back the code to a the file. 

    So after writing back, my code should look like 

    ------------------------------------------------------------------------

    using system;

    using system.collections;

     

    namespace abcd

    {

    class xyz

    {

     void sample()

    {

    }

    {

    }

    }

    };

     

    -----------------------------------------------------------------------------------------------

     

     

    ------------------------------------------------------------------

     

     

    I am able to read the code into compilation unit, but not able add a new directive or a node into the tree.

     

    Is this possible with Roslyn ? If so, can you please give me some pointers ? 

     

    Thanks

    Rajendra

     


    • Edited by rajendrab Thursday, January 12, 2012 2:15 PM
    Thursday, January 12, 2012 2:14 PM

Answers

  • Hi - Yes this is possible using Roslyn. Trees and nodes are immutable in Roslyn - so you cannot modify existing tree and node objects. But you can create new trees and nodes from existing ones. For this you can use the Syntax.* factory methods to gnerate new nodes and then implement a SyntaxRewriter to add them to your existing tree. You can also delete nodes from a tree using the SyntaxRewriter. Nodes also have methods named .Update() and .ReplaceNode() which you can use for generating new trees.

    The syntax model is explained in better detail in the Roslyn Project Overview document (this document is also installed on your machine when you install the CTP). Take a look at section 3 "Working with Syntax".

    There were some previous threads on this forum around using rewriters. I am including some links below but you should also be able to find them by searching for "SyntaxRewriter" on this forum. These threads have code samples which should help get you started -

    Rewriting Comments
    Deleting Comments and Trailing Whitespace

    Deleting #Regions
    Deleting a Statement
    Changing a Symbol's name

    Hope this helps - let us know if you have further questions.


    Shyam Namboodiripad | Software Development Engineer in Test | Roslyn Compilers Team
    Thursday, January 12, 2012 8:27 PM

All replies

  • Hi - Yes this is possible using Roslyn. Trees and nodes are immutable in Roslyn - so you cannot modify existing tree and node objects. But you can create new trees and nodes from existing ones. For this you can use the Syntax.* factory methods to gnerate new nodes and then implement a SyntaxRewriter to add them to your existing tree. You can also delete nodes from a tree using the SyntaxRewriter. Nodes also have methods named .Update() and .ReplaceNode() which you can use for generating new trees.

    The syntax model is explained in better detail in the Roslyn Project Overview document (this document is also installed on your machine when you install the CTP). Take a look at section 3 "Working with Syntax".

    There were some previous threads on this forum around using rewriters. I am including some links below but you should also be able to find them by searching for "SyntaxRewriter" on this forum. These threads have code samples which should help get you started -

    Rewriting Comments
    Deleting Comments and Trailing Whitespace

    Deleting #Regions
    Deleting a Statement
    Changing a Symbol's name

    Hope this helps - let us know if you have further questions.


    Shyam Namboodiripad | Software Development Engineer in Test | Roslyn Compilers Team
    Thursday, January 12, 2012 8:27 PM
  • Thanks Shyam for the answer. I will check the syntaxrewriter.

     

    Tuesday, January 17, 2012 5:54 AM