-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathReactiveGenerator.Execute.cs
More file actions
429 lines (367 loc) · 15.4 KB
/
ReactiveGenerator.Execute.cs
File metadata and controls
429 lines (367 loc) · 15.4 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
// Copyright (c) 2025 ReactiveUI and contributors. All rights reserved.
// Licensed to the ReactiveUI and contributors under one or more agreements.
// The ReactiveUI and contributors licenses this file to you under the MIT license.
// See the LICENSE file in the project root for full license information.
using System;
using System.Collections.Immutable;
using System.Linq;
using System.Threading;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using ReactiveUI.SourceGenerators.Extensions;
using ReactiveUI.SourceGenerators.Helpers;
using ReactiveUI.SourceGenerators.Models;
using ReactiveUI.SourceGenerators.Reactive.Models;
using static ReactiveUI.SourceGenerators.Diagnostics.DiagnosticDescriptors;
namespace ReactiveUI.SourceGenerators;
/// <summary>
/// ReactiveGenerator.
/// </summary>
/// <seealso cref="IIncrementalGenerator" />
public sealed partial class ReactiveGenerator
{
internal static readonly string GeneratorName = typeof(ReactiveGenerator).FullName!;
internal static readonly string GeneratorVersion = typeof(ReactiveGenerator).Assembly.GetName().Version.ToString();
#if ROSYLN_412
private static Result<PropertyInfo?>? GetPropertyInfo(in GeneratorAttributeSyntaxContext context, CancellationToken token)
{
using var builder = ImmutableArrayBuilder<DiagnosticInfo>.Rent();
var symbol = context.TargetSymbol;
if (!symbol.TryGetAttributeWithFullyQualifiedMetadataName(AttributeDefinitions.ReactiveAttributeType, out var attributeData))
{
return default;
}
if (symbol is not IPropertySymbol propertySymbol)
{
return default;
}
if (!propertySymbol.IsPartialDefinition || propertySymbol.IsStatic)
{
return default;
}
if (!propertySymbol.IsTargetTypeValid())
{
builder.Add(
InvalidReactiveObjectError,
propertySymbol,
propertySymbol.ContainingType,
propertySymbol.Name);
return new(default, builder.ToImmutable());
}
token.ThrowIfCancellationRequested();
// Get Property AccessModifier.
var propertyAccessModifier = propertySymbol.DeclaredAccessibility.ToString().ToLower();
if (propertyAccessModifier?.Contains("and") == true)
{
propertyAccessModifier = propertyAccessModifier.Replace("and", " ");
}
else if (propertyAccessModifier?.Contains("or") == true)
{
propertyAccessModifier = propertyAccessModifier.Replace("or", " ");
}
token.ThrowIfCancellationRequested();
// Get Set AccessModifier.
var setAccessModifier = $"{propertySymbol.SetMethod?.DeclaredAccessibility} set".ToLower();
if (setAccessModifier.StartsWith("public", StringComparison.Ordinal))
{
setAccessModifier = "set";
}
else if (setAccessModifier?.Contains("and") == true)
{
if (setAccessModifier.Contains("protectedandinternal"))
{
setAccessModifier = setAccessModifier.Replace("protectedandinternal", "private protected");
}
else
{
setAccessModifier = setAccessModifier.Replace("and", " ");
}
}
else if (setAccessModifier?.Contains("or") == true)
{
setAccessModifier = setAccessModifier.Replace("or", " ");
}
if (propertyAccessModifier == "private" && setAccessModifier == "private set")
{
setAccessModifier = "set";
}
else if (propertyAccessModifier == "internal" && setAccessModifier == "internal set")
{
setAccessModifier = "set";
}
else if (propertyAccessModifier == "protected" && setAccessModifier == "protected set")
{
setAccessModifier = "set";
}
else if (propertyAccessModifier == "protected internal" && setAccessModifier == "protected internal set")
{
setAccessModifier = "set";
}
else if (propertyAccessModifier == "private protected" && setAccessModifier == "private protected set")
{
setAccessModifier = "set";
}
token.ThrowIfCancellationRequested();
var inheritance = propertySymbol.IsVirtual ? " virtual" : propertySymbol.IsOverride ? " override" : string.Empty;
var useRequired = propertySymbol.IsRequired ? "required " : string.Empty;
var typeNameWithNullabilityAnnotations = propertySymbol.Type.GetFullyQualifiedNameWithNullabilityAnnotations();
var fieldName = propertySymbol.GetGeneratedFieldName();
if (context.SemanticModel.Compilation is CSharpCompilation compilation && compilation.LanguageVersion == LanguageVersion.Preview)
{
fieldName = "field";
}
var propertyName = propertySymbol.Name;
// Get the nullability info for the property
propertySymbol.GetNullabilityInfo(
context.SemanticModel,
out var isReferenceTypeOrUnconstraindTypeParameter,
out var includeMemberNotNullOnSetAccessor);
var propertyDeclaration = (PropertyDeclarationSyntax)context.TargetNode;
context.GetForwardedAttributes(
builder,
propertySymbol,
propertyDeclaration.AttributeLists,
token,
out var forwardedAttributesString);
token.ThrowIfCancellationRequested();
// Get the containing type info
var targetInfo = TargetInfo.From(propertySymbol.ContainingType);
token.ThrowIfCancellationRequested();
return new(
new(
targetInfo,
typeNameWithNullabilityAnnotations,
fieldName,
propertyName,
isReferenceTypeOrUnconstraindTypeParameter,
includeMemberNotNullOnSetAccessor,
forwardedAttributesString,
setAccessModifier!,
inheritance,
useRequired,
true,
propertyAccessModifier!),
builder.ToImmutable());
}
#endif
/// <summary>
/// Gets the observable method information.
/// </summary>
/// <param name="context">The context.</param>
/// <param name="token">The token.</param>
/// <returns>
/// The value.
/// </returns>
private static Result<PropertyInfo?>? GetVariableInfo(in GeneratorAttributeSyntaxContext context, CancellationToken token)
{
using var builder = ImmutableArrayBuilder<DiagnosticInfo>.Rent();
var symbol = context.TargetSymbol;
if (!symbol.TryGetAttributeWithFullyQualifiedMetadataName(AttributeDefinitions.ReactiveAttributeType, out var attributeData))
{
return default;
}
if (symbol is not IFieldSymbol fieldSymbol)
{
return default;
}
if (!fieldSymbol.IsTargetTypeValid())
{
builder.Add(
InvalidReactiveObjectError,
fieldSymbol,
fieldSymbol.ContainingType,
fieldSymbol.Name);
return new(default, builder.ToImmutable());
}
token.ThrowIfCancellationRequested();
// Get AccessModifier enum value from the attribute
attributeData.TryGetNamedArgument("SetModifier", out int accessModifierArgument);
var setAccessModifier = accessModifierArgument switch
{
1 => "protected set",
2 => "internal set",
3 => "private set",
4 => "protected internal set",
5 => "private protected set",
6 => "init",
_ => "set",
};
token.ThrowIfCancellationRequested();
// Get Inheritance value from the attribute
attributeData.TryGetNamedArgument("Inheritance", out int inheritanceArgument);
var inheritance = inheritanceArgument switch
{
1 => " virtual",
2 => " override",
3 => " new",
_ => string.Empty,
};
token.ThrowIfCancellationRequested();
// Get Inheritance value from the attribute
attributeData.TryGetNamedArgument("UseRequired", out bool useRequiredArgument);
var useRequired = useRequiredArgument ? "required " : string.Empty;
token.ThrowIfCancellationRequested();
// Get the property type and name
var typeNameWithNullabilityAnnotations = fieldSymbol.Type.GetFullyQualifiedNameWithNullabilityAnnotations();
var fieldName = fieldSymbol.Name;
var propertyName = fieldSymbol.GetGeneratedPropertyName();
if (fieldName == propertyName)
{
builder.Add(
ReactivePropertyNameCollisionError,
fieldSymbol,
fieldSymbol.ContainingType,
fieldSymbol.Name);
return new(default, builder.ToImmutable());
}
token.ThrowIfCancellationRequested();
// Get the nullability info for the property
fieldSymbol.GetNullabilityInfo(
context.SemanticModel,
out var isReferenceTypeOrUnconstraindTypeParameter,
out var includeMemberNotNullOnSetAccessor);
token.ThrowIfCancellationRequested();
var fieldDeclaration = (FieldDeclarationSyntax)context.TargetNode.Parent!.Parent!;
context.GetForwardedAttributes(
builder,
fieldSymbol,
fieldDeclaration.AttributeLists,
token,
out var forwardedAttributesString);
token.ThrowIfCancellationRequested();
// Get the containing type info
var targetInfo = TargetInfo.From(fieldSymbol.ContainingType);
token.ThrowIfCancellationRequested();
return new(
new(
targetInfo,
typeNameWithNullabilityAnnotations,
fieldName,
propertyName,
isReferenceTypeOrUnconstraindTypeParameter,
includeMemberNotNullOnSetAccessor,
forwardedAttributesString,
setAccessModifier,
inheritance,
useRequired,
false,
"public"),
builder.ToImmutable());
}
/// <summary>
/// Generates the source code.
/// </summary>
/// <param name="containingTypeName">The contain type name.</param>
/// <param name="containingNamespace">The containing namespace.</param>
/// <param name="containingClassVisibility">The containing class visibility.</param>
/// <param name="containingType">The containing type.</param>
/// <param name="properties">The properties.</param>
/// <returns>The value.</returns>
private static string GenerateSource(string containingTypeName, string containingNamespace, string containingClassVisibility, string containingType, PropertyInfo[] properties)
{
// Get Parent class details from properties.ParentInfo
var (parentClassDeclarationsString, closingBrackets) = TargetInfo.GenerateParentClassDeclarations([.. properties.Select(p => p.TargetInfo.ParentInfo)]);
var classes = GenerateClassWithProperties(containingTypeName, containingNamespace, containingClassVisibility, containingType, properties);
return
$$"""
// <auto-generated/>
using ReactiveUI;
#pragma warning disable
#nullable enable
namespace {{containingNamespace}}
{
{{parentClassDeclarationsString}}{{classes}}{{closingBrackets}}
}
#nullable restore
#pragma warning restore
""";
}
/// <summary>
/// Generates the source code.
/// </summary>
/// <param name="containingTypeName">The contain type name.</param>
/// <param name="containingNamespace">The containing namespace.</param>
/// <param name="containingClassVisibility">The containing class visibility.</param>
/// <param name="containingType">The containing type.</param>
/// <param name="properties">The properties.</param>
/// <returns>The value.</returns>
private static string GenerateClassWithProperties(string containingTypeName, string containingNamespace, string containingClassVisibility, string containingType, PropertyInfo[] properties)
{
// Includes 2 tabs from the property declarations so no need to add them here.
var propertyDeclarations = string.Join("\n", properties.Select(GetPropertySyntax));
return
$$"""
/// <summary>
/// Partial class for the {{containingTypeName}} which contains ReactiveUI Reactive property initialization.
/// </summary>
{{containingClassVisibility}} partial {{containingType}} {{containingTypeName}}
{
[global::System.CodeDom.Compiler.GeneratedCode("{{GeneratorName}}", "{{GeneratorVersion}}")]
{{propertyDeclarations}}
}
""";
}
/// <summary>
/// Generates property declarations for the given observable method information.
/// </summary>
/// <param name="propertyInfo">Metadata about the observable property.</param>
/// <returns>A string containing the generated code for the property.</returns>
private static string GetPropertySyntax(PropertyInfo propertyInfo)
{
if (propertyInfo.PropertyName is null)
{
return string.Empty;
}
var setFieldName = propertyInfo.FieldName;
var getFieldName = propertyInfo.FieldName;
if (propertyInfo.FieldName == "value")
{
setFieldName = "this.value";
}
var fieldSyntax = string.Empty;
var partialModifier = propertyInfo.IsProperty ? "partial " : string.Empty;
string propertyAttributes;
if (propertyInfo.IsProperty && propertyInfo.FieldName != "field")
{
propertyAttributes = string.Join("\n ", propertyInfo.ForwardedAttributes);
fieldSyntax =
$$"""
{{propertyAttributes}}
private {{propertyInfo.TypeNameWithNullabilityAnnotations}} {{propertyInfo.FieldName}};
""";
}
if (propertyInfo.IsProperty)
{
propertyAttributes = string.Join("\n ", AttributeDefinitions.ExcludeFromCodeCoverage);
}
else
{
propertyAttributes = string.Join("\n ", AttributeDefinitions.ExcludeFromCodeCoverage.Concat(propertyInfo.ForwardedAttributes));
}
var accessModifier = propertyInfo.PropertyAccessModifier;
var setAccessModifier = propertyInfo.SetAccessModifier;
if (propertyInfo.IncludeMemberNotNullOnSetAccessor || propertyInfo.IsReferenceTypeOrUnconstrainedTypeParameter)
{
return
$$"""
{{fieldSyntax}}
/// <inheritdoc cref="{{setFieldName}}"/>
{{propertyAttributes}}
{{accessModifier}}{{propertyInfo.Inheritance}} {{propertyInfo.UseRequired}}{{partialModifier}}{{propertyInfo.TypeNameWithNullabilityAnnotations}} {{propertyInfo.PropertyName}}
{
get => {{getFieldName}};
[global::System.Diagnostics.CodeAnalysis.MemberNotNull("{{setFieldName}}")]
{{setAccessModifier}} => this.RaiseAndSetIfChanged(ref {{setFieldName}}, value);
}
""";
}
return
$$"""
{{fieldSyntax}}
/// <inheritdoc cref="{{setFieldName}}"/>
{{propertyAttributes}}
{{accessModifier}}{{propertyInfo.Inheritance}} {{propertyInfo.UseRequired}}{{partialModifier}}{{propertyInfo.TypeNameWithNullabilityAnnotations}} {{propertyInfo.PropertyName}} { get => {{getFieldName}}; {{setAccessModifier}} => this.RaiseAndSetIfChanged(ref {{setFieldName}}, value); }
""";
}
}