Skip to content

Commit 3699f11

Browse files
committed
Changed GetSyntax to use a ImmutableArrayBuilder for RelayCommandGenerator
1 parent fbb227c commit 3699f11

File tree

1 file changed

+13
-15
lines changed

1 file changed

+13
-15
lines changed

src/CommunityToolkit.Mvvm.SourceGenerators/Input/RelayCommandGenerator.Execute.cs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,9 @@ public static ImmutableArray<MemberDeclarationSyntax> GetSyntax(CommandInfo comm
207207
.Select(static a => AttributeList(SingletonSeparatedList(a.GetSyntax())))
208208
.ToArray();
209209

210+
ImmutableArrayBuilder<MemberDeclarationSyntax> declarations = ImmutableArrayBuilder<MemberDeclarationSyntax>.Rent();
211+
210212
// Declare a backing field if needed
211-
FieldDeclarationSyntax? fieldDeclaration = null;
212213
if (commandInfo.FieldName is not null)
213214
{
214215
// Construct the generated field as follows:
@@ -217,7 +218,7 @@ public static ImmutableArray<MemberDeclarationSyntax> GetSyntax(CommandInfo comm
217218
// [global::System.CodeDom.Compiler.GeneratedCode("...", "...")]
218219
// <FORWARDED_ATTRIBUTES>
219220
// private <COMMAND_TYPE>? <COMMAND_FIELD_NAME>;
220-
fieldDeclaration =
221+
FieldDeclarationSyntax fieldDeclaration =
221222
FieldDeclaration(
222223
VariableDeclaration(NullableType(IdentifierName(commandClassTypeName)))
223224
.AddVariables(VariableDeclarator(Identifier(commandInfo.FieldName))))
@@ -230,6 +231,8 @@ public static ImmutableArray<MemberDeclarationSyntax> GetSyntax(CommandInfo comm
230231
AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(typeof(RelayCommandGenerator).Assembly.GetName().Version.ToString()))))))
231232
.WithOpenBracketToken(Token(TriviaList(Comment($"/// <summary>The backing field for <see cref=\"{commandInfo.PropertyName}\"/>.</summary>")), SyntaxKind.OpenBracketToken, TriviaList())))
232233
.AddAttributeLists(forwardedFieldAttributes);
234+
235+
declarations.Add(fieldDeclaration);
233236
}
234237

235238
// Prepares the argument to pass the underlying method to invoke
@@ -347,22 +350,25 @@ public static ImmutableArray<MemberDeclarationSyntax> GetSyntax(CommandInfo comm
347350
.AddArgumentListArguments(commandCreationArguments.ToArray()))))
348351
.WithSemicolonToken(Token(SyntaxKind.SemicolonToken));
349352

353+
declarations.Add(propertyDeclaration);
354+
350355
// Conditionally declare the additional members for the cancel commands
351356
if (commandInfo.IncludeCancelCommand)
352357
{
353358
// Prepare all necessary member and type names
354359
string? cancelCommandFieldName = commandInfo.FieldName is not null ? $"{commandInfo.FieldName.Substring(0, commandInfo.FieldName.Length - "Command".Length)}CancelCommand" : null;
355360
string cancelCommandPropertyName = $"{commandInfo.PropertyName.Substring(0, commandInfo.PropertyName.Length - "Command".Length)}CancelCommand";
356361

357-
FieldDeclarationSyntax? cancelCommandFieldDeclaration = null;
362+
// Declare a backing field for the cancel command if needed.
363+
// This is only needed if we can't use the field keyword for the main command, as otherwise the cancel command can just use its own field keyword.
358364
if (cancelCommandFieldName is not null)
359365
{
360366
// Construct the generated field for the cancel command as follows:
361367
//
362368
// /// <summary>The backing field for <see cref="<COMMAND_PROPERTY_NAME>"/></summary>
363369
// [global::System.CodeDom.Compiler.GeneratedCode("...", "...")]
364370
// private global::System.Windows.Input.ICommand? <CANCEL_COMMAND_FIELD_NAME>;
365-
cancelCommandFieldDeclaration =
371+
FieldDeclarationSyntax cancelCommandFieldDeclaration =
366372
FieldDeclaration(
367373
VariableDeclaration(NullableType(IdentifierName("global::System.Windows.Input.ICommand")))
368374
.AddVariables(VariableDeclarator(Identifier(cancelCommandFieldName))))
@@ -375,6 +381,7 @@ public static ImmutableArray<MemberDeclarationSyntax> GetSyntax(CommandInfo comm
375381
AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(typeof(RelayCommandGenerator).Assembly.GetName().Version.ToString()))))))
376382
.WithOpenBracketToken(Token(TriviaList(Comment($"/// <summary>The backing field for <see cref=\"{cancelCommandPropertyName}\"/>.</summary>")), SyntaxKind.OpenBracketToken, TriviaList())));
377383

384+
declarations.Add(cancelCommandFieldDeclaration);
378385
}
379386

380387
// Construct the generated property as follows (the explicit delegate cast is needed to avoid overload resolution conflicts):
@@ -412,20 +419,11 @@ public static ImmutableArray<MemberDeclarationSyntax> GetSyntax(CommandInfo comm
412419
.AddArgumentListArguments(Argument(IdentifierName(commandInfo.PropertyName))))))
413420
.WithSemicolonToken(Token(SyntaxKind.SemicolonToken));
414421

415-
if (fieldDeclaration is not null && cancelCommandFieldDeclaration is not null)
416-
{
417-
return ImmutableArray.Create<MemberDeclarationSyntax>(fieldDeclaration, propertyDeclaration, cancelCommandFieldDeclaration, cancelCommandPropertyDeclaration);
418-
}
419-
420-
return ImmutableArray.Create<MemberDeclarationSyntax>(propertyDeclaration, cancelCommandPropertyDeclaration);
421-
}
422+
declarations.Add(cancelCommandPropertyDeclaration);
422423

423-
if (fieldDeclaration is not null)
424-
{
425-
return ImmutableArray.Create<MemberDeclarationSyntax>(fieldDeclaration, propertyDeclaration);
426424
}
427425

428-
return ImmutableArray.Create<MemberDeclarationSyntax>(propertyDeclaration);
426+
return declarations.ToImmutable();
429427
}
430428

431429
/// <summary>

0 commit comments

Comments
 (0)