|
10 | 10 | using System.Threading;
|
11 | 11 | using CommunityToolkit.Mvvm.SourceGenerators.ComponentModel.Models;
|
12 | 12 | using CommunityToolkit.Mvvm.SourceGenerators.Extensions;
|
| 13 | +using CommunityToolkit.Mvvm.SourceGenerators.Helpers; |
13 | 14 | using CommunityToolkit.Mvvm.SourceGenerators.Models;
|
14 | 15 | using Microsoft.CodeAnalysis;
|
15 | 16 | using Microsoft.CodeAnalysis.CSharp;
|
@@ -45,7 +46,7 @@ public static bool TryGetInfo(
|
45 | 46 | [NotNullWhen(true)] out PropertyInfo? propertyInfo,
|
46 | 47 | out ImmutableArray<DiagnosticInfo> diagnostics)
|
47 | 48 | {
|
48 |
| - ImmutableArray<DiagnosticInfo>.Builder builder = ImmutableArray.CreateBuilder<DiagnosticInfo>(); |
| 49 | + using ImmutableArrayBuilder<DiagnosticInfo>.Lease builder = ImmutableArrayBuilder<DiagnosticInfo>.Rent(); |
49 | 50 |
|
50 | 51 | // Validate the target type
|
51 | 52 | if (!IsTargetTypeValid(fieldSymbol, out bool shouldInvokeOnPropertyChanging))
|
@@ -100,10 +101,11 @@ public static bool TryGetInfo(
|
100 | 101 | return false;
|
101 | 102 | }
|
102 | 103 |
|
103 |
| - ImmutableArray<string>.Builder propertyChangedNames = ImmutableArray.CreateBuilder<string>(); |
104 |
| - ImmutableArray<string>.Builder propertyChangingNames = ImmutableArray.CreateBuilder<string>(); |
105 |
| - ImmutableArray<string>.Builder notifiedCommandNames = ImmutableArray.CreateBuilder<string>(); |
106 |
| - ImmutableArray<AttributeInfo>.Builder forwardedAttributes = ImmutableArray.CreateBuilder<AttributeInfo>(); |
| 104 | + using ImmutableArrayBuilder<string>.Lease propertyChangedNames = ImmutableArrayBuilder<string>.Rent(); |
| 105 | + using ImmutableArrayBuilder<string>.Lease propertyChangingNames = ImmutableArrayBuilder<string>.Rent(); |
| 106 | + using ImmutableArrayBuilder<string>.Lease notifiedCommandNames = ImmutableArrayBuilder<string>.Rent(); |
| 107 | + using ImmutableArrayBuilder<AttributeInfo>.Lease forwardedAttributes = ImmutableArrayBuilder<AttributeInfo>.Rent(); |
| 108 | + |
107 | 109 | bool notifyRecipients = false;
|
108 | 110 | bool notifyDataErrorInfo = false;
|
109 | 111 | bool hasOrInheritsClassLevelNotifyPropertyChangedRecipients = false;
|
@@ -137,22 +139,22 @@ public static bool TryGetInfo(
|
137 | 139 | foreach (AttributeData attributeData in fieldSymbol.GetAttributes())
|
138 | 140 | {
|
139 | 141 | // Gather dependent property and command names
|
140 |
| - if (TryGatherDependentPropertyChangedNames(fieldSymbol, attributeData, propertyChangedNames, builder) || |
141 |
| - TryGatherDependentCommandNames(fieldSymbol, attributeData, notifiedCommandNames, builder)) |
| 142 | + if (TryGatherDependentPropertyChangedNames(fieldSymbol, attributeData, in propertyChangedNames, in builder) || |
| 143 | + TryGatherDependentCommandNames(fieldSymbol, attributeData, in notifiedCommandNames, in builder)) |
142 | 144 | {
|
143 | 145 | continue;
|
144 | 146 | }
|
145 | 147 |
|
146 | 148 | // Check whether the property should also notify recipients
|
147 |
| - if (TryGetIsNotifyingRecipients(fieldSymbol, attributeData, builder, hasOrInheritsClassLevelNotifyPropertyChangedRecipients, out isBroadcastTargetValid)) |
| 149 | + if (TryGetIsNotifyingRecipients(fieldSymbol, attributeData, in builder, hasOrInheritsClassLevelNotifyPropertyChangedRecipients, out isBroadcastTargetValid)) |
148 | 150 | {
|
149 | 151 | notifyRecipients = isBroadcastTargetValid;
|
150 | 152 |
|
151 | 153 | continue;
|
152 | 154 | }
|
153 | 155 |
|
154 | 156 | // Check whether the property should also be validated
|
155 |
| - if (TryGetNotifyDataErrorInfo(fieldSymbol, attributeData, builder, hasOrInheritsClassLevelNotifyDataErrorInfo, out isValidationTargetValid)) |
| 157 | + if (TryGetNotifyDataErrorInfo(fieldSymbol, attributeData, in builder, hasOrInheritsClassLevelNotifyDataErrorInfo, out isValidationTargetValid)) |
156 | 158 | {
|
157 | 159 | notifyDataErrorInfo = isValidationTargetValid;
|
158 | 160 |
|
@@ -266,9 +268,7 @@ public static bool TryGetInfo(
|
266 | 268 | /// <param name="fieldSymbol">The input <see cref="IFieldSymbol"/> instance to process.</param>
|
267 | 269 | /// <param name="shouldInvokeOnPropertyChanging">Whether or not property changing events should also be raised.</param>
|
268 | 270 | /// <returns>Whether or not the containing type for <paramref name="fieldSymbol"/> is valid.</returns>
|
269 |
| - private static bool IsTargetTypeValid( |
270 |
| - IFieldSymbol fieldSymbol, |
271 |
| - out bool shouldInvokeOnPropertyChanging) |
| 271 | + private static bool IsTargetTypeValid(IFieldSymbol fieldSymbol, out bool shouldInvokeOnPropertyChanging) |
272 | 272 | {
|
273 | 273 | // The [ObservableProperty] attribute can only be used in types that are known to expose the necessary OnPropertyChanged and OnPropertyChanging methods.
|
274 | 274 | // That means that the containing type for the field needs to match one of the following conditions:
|
@@ -318,8 +318,8 @@ private static bool IsGeneratedPropertyInvalid(string propertyName, ITypeSymbol
|
318 | 318 | private static bool TryGatherDependentPropertyChangedNames(
|
319 | 319 | IFieldSymbol fieldSymbol,
|
320 | 320 | AttributeData attributeData,
|
321 |
| - ImmutableArray<string>.Builder propertyChangedNames, |
322 |
| - ImmutableArray<DiagnosticInfo>.Builder diagnostics) |
| 321 | + in ImmutableArrayBuilder<string>.Lease propertyChangedNames, |
| 322 | + in ImmutableArrayBuilder<DiagnosticInfo>.Lease diagnostics) |
323 | 323 | {
|
324 | 324 | // Validates a property name using existing properties
|
325 | 325 | bool IsPropertyNameValid(string propertyName)
|
@@ -383,8 +383,8 @@ bool IsPropertyNameValidWithGeneratedMembers(string propertyName)
|
383 | 383 | private static bool TryGatherDependentCommandNames(
|
384 | 384 | IFieldSymbol fieldSymbol,
|
385 | 385 | AttributeData attributeData,
|
386 |
| - ImmutableArray<string>.Builder notifiedCommandNames, |
387 |
| - ImmutableArray<DiagnosticInfo>.Builder diagnostics) |
| 386 | + in ImmutableArrayBuilder<string>.Lease notifiedCommandNames, |
| 387 | + in ImmutableArrayBuilder<DiagnosticInfo>.Lease diagnostics) |
388 | 388 | {
|
389 | 389 | // Validates a command name using existing properties
|
390 | 390 | bool IsCommandNameValid(string commandName, out bool shouldLookForGeneratedMembersToo)
|
@@ -505,7 +505,7 @@ private static bool TryGetIsNotifyingRecipients(IFieldSymbol fieldSymbol, out bo
|
505 | 505 | private static bool TryGetIsNotifyingRecipients(
|
506 | 506 | IFieldSymbol fieldSymbol,
|
507 | 507 | AttributeData attributeData,
|
508 |
| - ImmutableArray<DiagnosticInfo>.Builder diagnostics, |
| 508 | + in ImmutableArrayBuilder<DiagnosticInfo>.Lease diagnostics, |
509 | 509 | bool hasOrInheritsClassLevelNotifyPropertyChangedRecipients,
|
510 | 510 | out bool isBroadcastTargetValid)
|
511 | 511 | {
|
@@ -608,7 +608,7 @@ private static bool TryGetNotifyDataErrorInfo(IFieldSymbol fieldSymbol, out bool
|
608 | 608 | private static bool TryGetNotifyDataErrorInfo(
|
609 | 609 | IFieldSymbol fieldSymbol,
|
610 | 610 | AttributeData attributeData,
|
611 |
| - ImmutableArray<DiagnosticInfo>.Builder diagnostics, |
| 611 | + in ImmutableArrayBuilder<DiagnosticInfo>.Lease diagnostics, |
612 | 612 | bool hasOrInheritsClassLevelNotifyDataErrorInfo,
|
613 | 613 | out bool isValidationTargetValid)
|
614 | 614 | {
|
@@ -701,7 +701,7 @@ private static bool TryGetNotifyDataErrorInfo(
|
701 | 701 | /// <returns>The generated <see cref="MemberDeclarationSyntax"/> instance for <paramref name="propertyInfo"/>.</returns>
|
702 | 702 | public static MemberDeclarationSyntax GetPropertySyntax(PropertyInfo propertyInfo)
|
703 | 703 | {
|
704 |
| - ImmutableArray<StatementSyntax>.Builder setterStatements = ImmutableArray.CreateBuilder<StatementSyntax>(); |
| 704 | + using ImmutableArrayBuilder<StatementSyntax>.Lease setterStatements = ImmutableArrayBuilder<StatementSyntax>.Rent(); |
705 | 705 |
|
706 | 706 | // Get the property type syntax
|
707 | 707 | TypeSyntax propertyType = IdentifierName(propertyInfo.TypeNameWithNullabilityAnnotations);
|
@@ -848,7 +848,7 @@ public static MemberDeclarationSyntax GetPropertySyntax(PropertyInfo propertyInf
|
848 | 848 | .AddArgumentListArguments(
|
849 | 849 | Argument(fieldExpression),
|
850 | 850 | Argument(IdentifierName("value")))),
|
851 |
| - Block(setterStatements)); |
| 851 | + Block(setterStatements.ToArray())); |
852 | 852 |
|
853 | 853 | // Prepare the forwarded attributes, if any
|
854 | 854 | ImmutableArray<AttributeListSyntax> forwardedAttributes =
|
|
0 commit comments