-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuildContext.cs
More file actions
115 lines (91 loc) · 2.51 KB
/
BuildContext.cs
File metadata and controls
115 lines (91 loc) · 2.51 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
using System;
using System.Text;
using BrokenEvent.Object2Code.Interfaces;
namespace BrokenEvent.Object2Code
{
internal class BuildContext: IBuildContext
{
private int indentLevel;
private readonly StringBuilder stringBuilder;
public BuildContext(object target, ITypeDictionary dictionary, BuilderSettings settings, StringBuilder stringBuilder)
{
Dictionary = dictionary;
indentLevel = settings.InitialIndentLevel;
Settings = settings;
this.stringBuilder = stringBuilder;
}
public StringBuilder StringBuilder
{
get { return stringBuilder; }
}
public BuilderSettings Settings { get; }
public ITypeDictionary Dictionary { get; }
public void AppendTypeName(Type type)
{
if (Settings.KeywordsInsteadOfTypes)
{
string keyword = Dictionary.GetKeywordForType(type);
if (keyword != null)
{
stringBuilder.Append(keyword);
return;
}
}
string name = Settings.UseFullNames ? type.FullName : type.Name;
// fix for generics like List`1
int index = name.LastIndexOf('`');
if (index != -1)
name = name.Substring(0, index);
stringBuilder.Append(name);
if (!type.IsGenericType)
return;
// special handling for generic types
stringBuilder.Append("<");
bool firstArg = true;
foreach (Type arg in type.GetGenericArguments())
{
if (!firstArg)
stringBuilder.Append(", ");
firstArg = false;
AppendTypeName(arg);
}
stringBuilder.Append(">");
}
public void Append(string text)
{
stringBuilder.Append(text);
}
public void AppendContent(object target, bool useConstructor = true)
{
if (target == null)
{
stringBuilder.Append("null");
return;
}
IBuilder builder = Dictionary.GetBuilder(target.GetType());
if (builder is IBuilderEx builderEx)
builderEx.Build(target, useConstructor, this);
else
builder.Build(target, this);
}
public void AppendIndent()
{
for (int i = 0; i < indentLevel; i++)
stringBuilder.Append(Settings.Indent);
}
public void AppendLineBreak(bool indent = true)
{
stringBuilder.Append(Settings.LineBreak);
if (indent)
AppendIndent();
}
public void IncreaseIndent(int number = 1)
{
indentLevel += number;
}
public void DecreaseIndent(int number = 1)
{
indentLevel -= number;
}
}
}