Question

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

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top