Files
WpfExtended/WpfExtended.SourceGeneration/Templates/PropertyTemplate.cs
T
Alexandru Macocian 7d3c2fbb9b Alpha implementation of dependency property source generator.
Testing project for source generator
2021-04-06 10:40:06 +02:00

68 lines
2.1 KiB
C#

using System.Collections.Generic;
namespace System.Extensions.Templates
{
internal sealed class PropertyTemplate : AbstractTemplate
{
public static GetterTemplate DefaultGetter = new GetterTemplate()
.WithCode(
new SimpleCodeTemplate()
.WithCode(";"));
public static SetterTemplate DefaultSetter = new SetterTemplate()
.WithCode(
new SimpleCodeTemplate()
.WithCode(";"));
public List<Modifier> Modifiers { get; private set; } = new List<Modifier>() { Modifier.Public };
public GetterTemplate Getter { get; set; } = DefaultGetter;
public SetterTemplate Setter { get; set; } = DefaultSetter;
public string Type { get; set; }
public string Name { get; set; }
public PropertyTemplate WithGetter(GetterTemplate getter)
{
this.Getter = getter;
return this;
}
public PropertyTemplate WithSetter(SetterTemplate setter)
{
this.Setter = setter;
return this;
}
public PropertyTemplate WithModifiers(params Modifier[] modifiers)
{
this.Modifiers.Clear();
this.Modifiers.AddRange(modifiers);
return this;
}
public PropertyTemplate WithName(string name)
{
this.Name = name;
return this;
}
public PropertyTemplate WithType(string type)
{
this.Type = type;
return this;
}
public override void Generate(CodeWriter codeWriter)
{
foreach(var modifier in this.Modifiers)
{
codeWriter
.Append(modifier)
.Append(' ');
}
codeWriter
.Append(this.Type)
.Append(' ')
.Append(this.Name)
.BeginCodeBlock()
.Append(this.Getter)
.Append(this.Setter)
.EndCodeBlock();
}
}
}