-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCodeBuilder.cs
40 lines (30 loc) · 1.05 KB
/
CodeBuilder.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
using System.Text;
namespace CodeBuilder;
public abstract class CodeBuilder<TInterface, TClass> : Builder<TInterface, TClass>
where TClass : TInterface where TInterface : class, ICodeBuilder
{
protected readonly List<ICodeBuilder> SubBuilders = new();
public string BuildLine(int indent) => Build(new StringBuilder(), indent).ToString();
public string Name { get; }
public ICodeBuilderBase Parent { get; }
protected CodeBuilder(ICodeBuilderBase parent, string name)
{
Parent = parent;
Name = name;
}
protected abstract void DoBuild(StringBuilder sb, int indent);
public StringBuilder Build(StringBuilder sb, int indent = 0)
{
return BuildTemplate(sb, indent);
}
protected virtual StringBuilder BuildTemplate(StringBuilder sb, int indent = 0)
{
DoBuild(sb, indent);
SubBuilders.ForEach(it => it.Build(sb, indent + 1));
AfterSubBuild(sb, indent);
return sb;
}
protected virtual void AfterSubBuild(StringBuilder sb, int indent)
{
}
}