views:

27

answers:

1

I'm making some experiments with Visual Studio's CodeModel. I tried creating a file with two namespaces in there through VS's Code Model:

CodeModel codeModel = projItem.ContainingProject.CodeModel;
codeModel.AddNamespace("Namespaces.FirstNamespace", "namespacestest.cs");
codeModel.AddNamespace("Namespaces.SecondNamespace", "namespacestest.cs");

the output of such code is:

//file namespacestest.cs
namespace Namespaces.FirstNamespace {
}
namespace Namespaces.SecondNamespace {
}

Which is almost what I'm looking for. The only catch here is that I'd like to control the spacing: having at least one line between the first and the second namespace. Does by chance anyone know of a way of achieving this?

This is what I want:

//file namespacestest.cs
namespace Namespaces.FirstNamespace {
}

namespace Namespaces.SecondNamespace {
}

Thanks

+1  A: 

The statement codeModel.AddNamespace will return you an object of CodeNamespace. you can find the EditPoint of the namespace using the your first namespace and then insert a line like this.

CodeNamespace yournamespace = codeModel.AddNamespace(....);
EditPoint2 endEditPoint = yournamespace.GetEndPoint(vsCMPart.vsCMPartWhole).CreateEditPoint();
endEditPoint.InsertNewLine(1);

I am not sure whether this will put the line at the place you want but the idea is to get the EditPoint and then use it to insert the line.

24x7Programmer