How do i generate comments or region with comments
Closed this issue · 8 comments
Hello,
I would like to generate comments or regions with comments. Can you please share samples.
.WithRegions(regionMember:new RegionBuildMember("Comments",new List<IBuildMember>(){}))
Thanks,
Nitin
Hey!
Comments are actually a bit tricky to generate as you must connect them to something else. So I have added support for XML comments but not looked at regular comments.
Do you have an example of what you want? For example just something like this:
#region MyRegion
/*
MyComment
*/
#endregion
Because then I can look into it and see if I can add better support for comments. But it's easier if you have good examples of what you want.
Hello,
I would actually like to add in-line comments in a class. Something like -
//This is just comments
I didn't find any support so I was thinking about adding a region.
Oh I see! I will look into it tomorrow and see what I can do.
I looked into it a bit and the easiest way to do it is with leading/trailing trivias. This would make the code actually look something like this:
var classBuilder = new ClassBuilder("Cat", "Models");
var @class = classBuilder
.WithUsings("System")
.WithMethods(new MethodBuilder("MyMethod")
.WithParameters(new Parameter("MyParameter", typeof(string)))
.WithBody(
BodyGenerator.Create(
Statement.Declaration.Declare("hello", typeof(int)).WithComment("My comment above"),
Statement.Declaration.Declare("hello", typeof(int)).WithComment("My comment to the side", CommentPosition.Right)
))
.Build())
.Build();
Which will generate:
using System;
namespace Models
{
public class Cat
{
void MyMethod(string MyParameter)
{
//My comment above
int hello;
int hello; //My comment to the side
}
}
}
Sadly it seems like making newlines (\n) will destroy the formatting so you can only use it for single line comments so far. Is that still fine? If so I will create a nuget update later today.
Above sounds good at the method level. Can we do this at the Class level?
At class level I only have XML documentation support for methods. I can try to add XML support for fields, properties, constructor and classes too (feels like that is more useful than singleline comments?).
So this is what I have right now:
var @class = classBuilder
.WithUsings("System")
.WithConstructor(ConstructorGenerator.Create(
"Cat",
BodyGenerator.Create(
Statement.Declaration.Assign("Name", ReferenceGenerator.Create(new VariableReference("name"))),
Statement.Declaration.Assign("Age", ReferenceGenerator.Create(new VariableReference("age")))),
new List<Parameter> { new Parameter("name", typeof(string)), new Parameter("age", typeof(int), xmlDocumentation: "My parameter") },
new List<Modifiers> { Modifiers.Public },
summary: "MyConstructor summary"))
.WithProperties(new AutoProperty("MyProperty", typeof(int), PropertyTypes.GetAndSet, summary: "MyPropertySummary"))
.WithFields(
new Field("_name", typeof(string), new List<Modifiers>() { Modifiers.Private }, summary: "My field summary"))
.WithMethods(new MethodBuilder("MyMethod")
.WithParameters(new Parameter("MyParameter", typeof(string)))
.WithBody(
BodyGenerator.Create(
Statement.Declaration.Declare("hello", typeof(int)).WithComment("My comment above").WithComment("hej"),
Statement.Declaration.Declare("hello", typeof(int)).WithComment("My comment to the side", CommentPosition.Right)
))
.Build())
.Build();
Which gives:
using System;
namespace Models
{
public class Cat
{
/// <summary>
/// MyConstructor summary
/// </summary>
/// <param name="age">My parameter</param>
public Cat(string name, int age)
{
Name = name;
Age = age;
}
/// <summary>
/// MyPropertySummary
/// </summary>
int MyProperty { get; set; }
/// <summary>
/// My field summary
/// </summary>
private string _name;
void MyMethod(string MyParameter)
{
//hej
int hello;
int hello; //My comment to the side
}
}
}
Anything more you think you need?
I have uploaded a new version (0.16.0) with all of these.