NRefactory add namespace to typedeclaration

355 Views Asked by At

Currently I'm working on a project with NRefactory. We're filtering typedeclarations like 'Class' and 'Interface' out of a .cs file. We would like to place these typedeclarations into a custom namespace, but for some reason it's not working. Is anyone able to assist me with this problem?

I've tried the following code:

typeDeclaration.Parent.InsertChildBefore(typeDeclaration, ns, new Role<NamespaceDeclaration>("customNamespace"));
1

There are 1 best solutions below

0
Philip Pittle On

I've never tried to insert a Namespace node before the TypeDeclaration. Instead I've added the Namespace first and then added the TypeDeclaration:

  public static void AddChildTypeDeclaration(
        this AstNode tree, 
        TypeDeclaration newClass,
        NamespaceDeclaration parentNamespace = null)
    {
        if (null != parentNamespace)
        {
            var newNamespaceNode = new NamespaceDeclaration(
                parentNamespace.Name);

            newNamespaceNode.AddMember(newClass);

            tree.AddChild(newNamespaceNode, SyntaxTree.MemberRole);
        }
        else
        {
            tree.AddChild(newClass, Roles.TypeMemberRole);
        }
    }