generated from atc-net/atc-template-dotnet-package
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathViewModelBuilderExtensions.cs
136 lines (123 loc) · 4.39 KB
/
ViewModelBuilderExtensions.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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
namespace Atc.Wpf.SourceGenerators.Extensions.Builder;
[SuppressMessage("Design", "MA0051:Method is too long", Justification = "OK.")]
internal static class ViewModelBuilderExtensions
{
public static void GenerateStart(
this ViewModelBuilder builder,
ViewModelToGenerate viewModelToGenerate)
{
builder.AppendLine("// <auto-generated>");
builder.AppendLine("#nullable enable");
if (viewModelToGenerate.RelayCommandsToGenerate?.Count > 0)
{
builder.AppendLine("using Atc.Wpf.Command;");
}
builder.AppendLine();
builder.AppendLine($"namespace {viewModelToGenerate.NamespaceName};");
builder.AppendLine();
builder.AppendLine($"{viewModelToGenerate.ClassAccessModifier} partial class {viewModelToGenerate.ClassName}");
builder.AppendLine("{");
builder.IncreaseIndent();
}
public static void GenerateProperties(
this ViewModelBuilder builder,
IEnumerable<ObservablePropertyToGenerate>? propertiesToGenerate)
{
if (propertiesToGenerate is null)
{
return;
}
foreach (var propertyToGenerate in propertiesToGenerate)
{
GenerateProperty(builder, propertyToGenerate);
}
}
private static void GenerateProperty(
ViewModelBuilder builder,
ObservablePropertyToGenerate p)
{
builder.AppendLineBeforeMember();
builder.AppendLine($"public {p.Type} {p.Name}");
builder.AppendLine("{");
builder.IncreaseIndent();
builder.AppendLine($"get => {p.BackingFieldName};");
builder.AppendLine("set");
builder.AppendLine("{");
builder.IncreaseIndent();
builder.AppendLine($"if ({p.BackingFieldName} == value)");
builder.AppendLine("{");
builder.IncreaseIndent();
builder.AppendLine("return;");
builder.DecreaseIndent();
builder.AppendLine("}");
builder.AppendLine();
if (p.BeforeChangedCallback is not null)
{
GenerateCallbackInlineCode(builder, p.BeforeChangedCallback);
builder.AppendLine();
}
if (p.BroadcastOnChange)
{
builder.AppendLine($"var oldValue = {p.BackingFieldName};");
}
var nameofName = p.Name.EnsureNameofContent();
builder.AppendLine($"{p.BackingFieldName} = value;");
builder.AppendLine($"RaisePropertyChanged({nameofName});");
if (p.PropertyNamesToInvalidate is not null)
{
foreach (var propertyNameToInvalidate in p.PropertyNamesToInvalidate)
{
var nameofPropertyNameToInvalidate = propertyNameToInvalidate.EnsureNameofContent();
builder.AppendLine($"RaisePropertyChanged({nameofPropertyNameToInvalidate});");
}
}
if (p.BroadcastOnChange)
{
builder.AppendLine($"Broadcast({nameofName}, oldValue, value);");
}
if (p.AfterChangedCallback is not null)
{
builder.AppendLine();
GenerateCallbackInlineCode(builder, p.AfterChangedCallback);
}
builder.DecreaseIndent();
builder.AppendLine("}");
builder.DecreaseIndent();
builder.AppendLine("}");
}
private static void GenerateCallbackInlineCode(
ViewModelBuilder builder,
string value)
{
if (value.StartsWith("nameof(", StringComparison.Ordinal) &&
value.EndsWith(")", StringComparison.Ordinal))
{
var valueContent = value.ExtractInnerContent();
var sa = valueContent.Split([';'], StringSplitOptions.RemoveEmptyEntries);
foreach (var s in sa)
{
var line = s.Trim();
if (line.EndsWith("();", StringComparison.Ordinal))
{
builder.AppendLine(line);
}
else if (line.EndsWith("()", StringComparison.Ordinal))
{
builder.AppendLine(line + ";");
}
else
{
builder.AppendLine(line + "();");
}
}
}
else
{
var sa = value.Split([';'], StringSplitOptions.RemoveEmptyEntries);
foreach (var s in sa)
{
builder.AppendLine(s.Trim() + ";");
}
}
}
}