-
Notifications
You must be signed in to change notification settings - Fork 307
/
Copy pathRelayCommandGenerator.Execute.cs
1068 lines (942 loc) · 58.2 KB
/
RelayCommandGenerator.Execute.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
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Linq;
using System.Threading;
using CommunityToolkit.Mvvm.SourceGenerators.ComponentModel.Models;
using CommunityToolkit.Mvvm.SourceGenerators.Extensions;
using CommunityToolkit.Mvvm.SourceGenerators.Helpers;
using CommunityToolkit.Mvvm.SourceGenerators.Input.Models;
using CommunityToolkit.Mvvm.SourceGenerators.Models;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using static CommunityToolkit.Mvvm.SourceGenerators.Diagnostics.DiagnosticDescriptors;
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
namespace CommunityToolkit.Mvvm.SourceGenerators;
/// <inheritdoc/>
partial class RelayCommandGenerator
{
/// <summary>
/// A container for all the logic for <see cref="RelayCommandGenerator"/>.
/// </summary>
internal static class Execute
{
/// <summary>
/// Processes a given annotated methods and produces command info, if possible.
/// </summary>
/// <param name="methodSymbol">The input <see cref="IMethodSymbol"/> instance to process.</param>
/// <param name="attributeData">The <see cref="AttributeData"/> instance the method was annotated with.</param>
/// <param name="semanticModel">The <see cref="SemanticModel"/> instance for the current run.</param>
/// <param name="token">The cancellation token for the current operation.</param>
/// <param name="commandInfo">The resulting <see cref="CommandInfo"/> instance, if successfully generated.</param>
/// <param name="diagnostics">The resulting diagnostics from the processing operation.</param>
/// <returns>Whether a <see cref="CommandInfo"/> instance could be generated successfully.</returns>
public static bool TryGetInfo(
IMethodSymbol methodSymbol,
AttributeData attributeData,
SemanticModel semanticModel,
CancellationToken token,
[NotNullWhen(true)] out CommandInfo? commandInfo,
out ImmutableArray<DiagnosticInfo> diagnostics)
{
using ImmutableArrayBuilder<DiagnosticInfo> builder = ImmutableArrayBuilder<DiagnosticInfo>.Rent();
// Validate the method definition is unique
if (!IsCommandDefinitionUnique(methodSymbol, builder))
{
goto Failure;
}
token.ThrowIfCancellationRequested();
// Get the command field and property names
(string fieldName, string propertyName) = GetGeneratedFieldAndPropertyNames(methodSymbol);
token.ThrowIfCancellationRequested();
// Get the command type symbols
if (!TryMapCommandTypesFromMethod(
methodSymbol,
in builder,
out string? commandInterfaceType,
out string? commandClassType,
out string? delegateType,
out bool supportsCancellation,
out ImmutableArray<string> commandTypeArguments,
out ImmutableArray<string> commandTypeArgumentsWithNullabilityAnnotations,
out ImmutableArray<string> delegateTypeArgumentsWithNullabilityAnnotations))
{
goto Failure;
}
token.ThrowIfCancellationRequested();
// Check the switch to allow concurrent executions
if (!TryGetAllowConcurrentExecutionsSwitch(
methodSymbol,
attributeData,
commandClassType,
in builder,
out bool allowConcurrentExecutions))
{
goto Failure;
}
token.ThrowIfCancellationRequested();
// Check the switch to control exception flow
if (!TryGetFlowExceptionsToTaskSchedulerSwitch(
methodSymbol,
attributeData,
commandClassType,
in builder,
out bool flowExceptionsToTaskScheduler))
{
goto Failure;
}
token.ThrowIfCancellationRequested();
// Get the CanExecute expression type, if any
if (!TryGetCanExecuteExpressionType(
methodSymbol,
attributeData,
commandTypeArguments,
token,
in builder,
out string? canExecuteMemberName,
out CanExecuteExpressionType? canExecuteExpressionType))
{
goto Failure;
}
token.ThrowIfCancellationRequested();
// Get the option to include a cancel command, if any
if (!TryGetIncludeCancelCommandSwitch(
methodSymbol,
attributeData,
commandClassType,
supportsCancellation,
in builder,
out bool generateCancelCommand))
{
goto Failure;
}
token.ThrowIfCancellationRequested();
// Get all forwarded attributes (don't stop in case of errors, just ignore faulting attributes)
GatherForwardedAttributes(
methodSymbol,
semanticModel,
token,
in builder,
out ImmutableArray<AttributeInfo> forwardedAttributes);
token.ThrowIfCancellationRequested();
commandInfo = new CommandInfo(
methodSymbol.Name,
fieldName,
propertyName,
commandInterfaceType,
commandClassType,
delegateType,
commandTypeArgumentsWithNullabilityAnnotations,
delegateTypeArgumentsWithNullabilityAnnotations,
canExecuteMemberName,
canExecuteExpressionType,
allowConcurrentExecutions,
flowExceptionsToTaskScheduler,
generateCancelCommand,
forwardedAttributes);
diagnostics = builder.ToImmutable();
return true;
Failure:
commandInfo = null;
diagnostics = builder.ToImmutable();
return false;
}
/// <summary>
/// Creates the <see cref="MemberDeclarationSyntax"/> instances for a specified command.
/// </summary>
/// <param name="commandInfo">The input <see cref="CommandInfo"/> instance with the info to generate the command.</param>
/// <returns>The <see cref="MemberDeclarationSyntax"/> instances for the input command.</returns>
public static ImmutableArray<MemberDeclarationSyntax> GetSyntax(CommandInfo commandInfo)
{
// Prepare all necessary type names with type arguments
string commandInterfaceTypeXmlName = commandInfo.CommandTypeArguments.IsEmpty
? commandInfo.CommandInterfaceType
: commandInfo.CommandInterfaceType + "{T}";
string commandClassTypeName = commandInfo.CommandTypeArguments.IsEmpty
? commandInfo.CommandClassType
: $"{commandInfo.CommandClassType}<{string.Join(", ", commandInfo.CommandTypeArguments)}>";
string commandInterfaceTypeName = commandInfo.CommandTypeArguments.IsEmpty
? commandInfo.CommandInterfaceType
: $"{commandInfo.CommandInterfaceType}<{string.Join(", ", commandInfo.CommandTypeArguments)}>";
string delegateTypeName = commandInfo.DelegateTypeArguments.IsEmpty
? commandInfo.DelegateType
: $"{commandInfo.DelegateType}<{string.Join(", ", commandInfo.DelegateTypeArguments)}>";
// Prepare the forwarded field attributes, if any
AttributeListSyntax[] forwardedFieldAttributes =
commandInfo.ForwardedAttributes
.Where(static a => a.AttributeTarget is SyntaxKind.FieldKeyword)
.Select(static a => AttributeList(SingletonSeparatedList(a.GetSyntax())))
.ToArray();
// Also prepare any forwarded property attributes
AttributeListSyntax[] forwardedPropertyAttributes =
commandInfo.ForwardedAttributes
.Where(static a => a.AttributeTarget is SyntaxKind.PropertyKeyword)
.Select(static a => AttributeList(SingletonSeparatedList(a.GetSyntax())))
.ToArray();
// Construct the generated field as follows:
//
// /// <summary>The backing field for <see cref="<COMMAND_PROPERTY_NAME>"/></summary>
// [global::System.CodeDom.Compiler.GeneratedCode("...", "...")]
// <FORWARDED_ATTRIBUTES>
// private <COMMAND_TYPE>? <COMMAND_FIELD_NAME>;
FieldDeclarationSyntax fieldDeclaration =
FieldDeclaration(
VariableDeclaration(NullableType(IdentifierName(commandClassTypeName)))
.AddVariables(VariableDeclarator(Identifier(commandInfo.FieldName))))
.AddModifiers(Token(SyntaxKind.PrivateKeyword))
.AddAttributeLists(
AttributeList(SingletonSeparatedList(
Attribute(IdentifierName("global::System.CodeDom.Compiler.GeneratedCode"))
.AddArgumentListArguments(
AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(typeof(RelayCommandGenerator).FullName))),
AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(typeof(RelayCommandGenerator).Assembly.GetName().Version.ToString()))))))
.WithOpenBracketToken(Token(TriviaList(Comment($"/// <summary>The backing field for <see cref=\"{commandInfo.PropertyName}\"/>.</summary>")), SyntaxKind.OpenBracketToken, TriviaList())))
.AddAttributeLists(forwardedFieldAttributes);
// Prepares the argument to pass the underlying method to invoke
using ImmutableArrayBuilder<ArgumentSyntax> commandCreationArguments = ImmutableArrayBuilder<ArgumentSyntax>.Rent();
// The first argument is the execute method, which is always present
commandCreationArguments.Add(
Argument(
ObjectCreationExpression(IdentifierName(delegateTypeName))
.AddArgumentListArguments(Argument(IdentifierName(commandInfo.MethodName)))));
// Get the can execute expression, if available
ExpressionSyntax? canExecuteExpression = commandInfo.CanExecuteExpressionType switch
{
// Create a lambda expression ignoring the input value:
//
// new <RELAY_COMMAND_TYPE>(<METHOD_EXPRESSION>, _ => <CAN_EXECUTE_METHOD>());
CanExecuteExpressionType.MethodInvocationLambdaWithDiscard =>
SimpleLambdaExpression(
Parameter(Identifier(TriviaList(), SyntaxKind.UnderscoreToken, "_", "_", TriviaList())))
.WithExpressionBody(InvocationExpression(IdentifierName(commandInfo.CanExecuteMemberName!))),
// Create a lambda expression returning the property value:
//
// new <RELAY_COMMAND_TYPE>(<METHOD_EXPRESSION>, () => <CAN_EXECUTE_PROPERTY>);
CanExecuteExpressionType.PropertyAccessLambda =>
ParenthesizedLambdaExpression()
.WithExpressionBody(IdentifierName(commandInfo.CanExecuteMemberName!)),
// Create a lambda expression again, but discarding the input value:
//
// new <RELAY_COMMAND_TYPE>(<METHOD_EXPRESSION>, _ => <CAN_EXECUTE_PROPERTY>);
CanExecuteExpressionType.PropertyAccessLambdaWithDiscard =>
SimpleLambdaExpression(
Parameter(Identifier(TriviaList(), SyntaxKind.UnderscoreToken, "_", "_", TriviaList())))
.WithExpressionBody(IdentifierName(commandInfo.CanExecuteMemberName!)),
// Create a method group expression, which will become:
//
// new <RELAY_COMMAND_TYPE>(<METHOD_EXPRESSION>, <CAN_EXECUTE_METHOD>);
CanExecuteExpressionType.MethodGroup => IdentifierName(commandInfo.CanExecuteMemberName!),
_ => null
};
// Add the can execute expression to the arguments, if available
if (canExecuteExpression is not null)
{
commandCreationArguments.Add(Argument(canExecuteExpression));
}
// Enable concurrent executions, if requested
if (commandInfo.AllowConcurrentExecutions && !commandInfo.FlowExceptionsToTaskScheduler)
{
commandCreationArguments.Add(
Argument(MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
IdentifierName("global::CommunityToolkit.Mvvm.Input.AsyncRelayCommandOptions"),
IdentifierName("AllowConcurrentExecutions"))));
}
else if (commandInfo.FlowExceptionsToTaskScheduler && !commandInfo.AllowConcurrentExecutions)
{
// Enable exception flow, if requested
commandCreationArguments.Add(
Argument(MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
IdentifierName("global::CommunityToolkit.Mvvm.Input.AsyncRelayCommandOptions"),
IdentifierName("FlowExceptionsToTaskScheduler"))));
}
else if (commandInfo.AllowConcurrentExecutions && commandInfo.FlowExceptionsToTaskScheduler)
{
// Enable both concurrency control and exception flow
commandCreationArguments.Add(
Argument(BinaryExpression(
SyntaxKind.BitwiseOrExpression,
MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
IdentifierName("global::CommunityToolkit.Mvvm.Input.AsyncRelayCommandOptions"),
IdentifierName("AllowConcurrentExecutions")),
MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
IdentifierName("global::CommunityToolkit.Mvvm.Input.AsyncRelayCommandOptions"),
IdentifierName("FlowExceptionsToTaskScheduler")))));
}
// Construct the generated property as follows (the explicit delegate cast is needed to avoid overload resolution conflicts):
//
// /// <summary>Gets an <see cref="<COMMAND_INTERFACE_TYPE>" instance wrapping <see cref="<METHOD_NAME>"/> and <see cref="<OPTIONAL_CAN_EXECUTE>"/>.</summary>
// [global::System.CodeDom.Compiler.GeneratedCode("...", "...")]
// [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
// <FORWARDED_ATTRIBUTES>
// public <COMMAND_TYPE> <COMMAND_PROPERTY_NAME> => <COMMAND_FIELD_NAME> ??= new <RELAY_COMMAND_TYPE>(<COMMAND_CREATION_ARGUMENTS>);
PropertyDeclarationSyntax propertyDeclaration =
PropertyDeclaration(
IdentifierName(commandInterfaceTypeName),
Identifier(commandInfo.PropertyName))
.AddModifiers(Token(SyntaxKind.PublicKeyword))
.AddAttributeLists(
AttributeList(SingletonSeparatedList(
Attribute(IdentifierName("global::System.CodeDom.Compiler.GeneratedCode"))
.AddArgumentListArguments(
AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(typeof(RelayCommandGenerator).FullName))),
AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(typeof(RelayCommandGenerator).Assembly.GetName().Version.ToString()))))))
.WithOpenBracketToken(Token(TriviaList(Comment(
$"/// <summary>Gets an <see cref=\"{commandInterfaceTypeXmlName}\"/> instance wrapping <see cref=\"{commandInfo.MethodName}\"/>.</summary>")),
SyntaxKind.OpenBracketToken,
TriviaList())),
AttributeList(SingletonSeparatedList(Attribute(IdentifierName("global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage")))))
.AddAttributeLists(forwardedPropertyAttributes)
.WithExpressionBody(
ArrowExpressionClause(
AssignmentExpression(
SyntaxKind.CoalesceAssignmentExpression,
IdentifierName(commandInfo.FieldName),
ObjectCreationExpression(IdentifierName(commandClassTypeName))
.AddArgumentListArguments(commandCreationArguments.ToArray()))))
.WithSemicolonToken(Token(SyntaxKind.SemicolonToken));
// Conditionally declare the additional members for the cancel commands
if (commandInfo.IncludeCancelCommand)
{
// Prepare all necessary member and type names
string cancelCommandFieldName = $"{commandInfo.FieldName.Substring(0, commandInfo.FieldName.Length - "Command".Length)}CancelCommand";
string cancelCommandPropertyName = $"{commandInfo.PropertyName.Substring(0, commandInfo.PropertyName.Length - "Command".Length)}CancelCommand";
// Construct the generated field for the cancel command as follows:
//
// /// <summary>The backing field for <see cref="<COMMAND_PROPERTY_NAME>"/></summary>
// [global::System.CodeDom.Compiler.GeneratedCode("...", "...")]
// private global::System.Windows.Input.ICommand? <CANCEL_COMMAND_FIELD_NAME>;
FieldDeclarationSyntax cancelCommandFieldDeclaration =
FieldDeclaration(
VariableDeclaration(NullableType(IdentifierName("global::System.Windows.Input.ICommand")))
.AddVariables(VariableDeclarator(Identifier(cancelCommandFieldName))))
.AddModifiers(Token(SyntaxKind.PrivateKeyword))
.AddAttributeLists(
AttributeList(SingletonSeparatedList(
Attribute(IdentifierName("global::System.CodeDom.Compiler.GeneratedCode"))
.AddArgumentListArguments(
AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(typeof(RelayCommandGenerator).FullName))),
AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(typeof(RelayCommandGenerator).Assembly.GetName().Version.ToString()))))))
.WithOpenBracketToken(Token(TriviaList(Comment($"/// <summary>The backing field for <see cref=\"{cancelCommandPropertyName}\"/>.</summary>")), SyntaxKind.OpenBracketToken, TriviaList())));
// Construct the generated property as follows (the explicit delegate cast is needed to avoid overload resolution conflicts):
//
// /// <summary>Gets an <see cref="global::System.Windows.Input.ICommand" instance that can be used to cancel <see cref="<COMMAND_PROPERTY_NAME>"/>.</summary>
// [global::System.CodeDom.Compiler.GeneratedCode("...", "...")]
// [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
// public global::System.Windows.Input.ICommand <CANCEL_COMMAND_PROPERTY_NAME> => <CANCEL_COMMAND_FIELD_NAME> ??= global::CommunityToolkit.Mvvm.Input.IAsyncRelayCommandExtensions.CreateCancelCommand(<COMMAND_PROPERTY_NAME>);
PropertyDeclarationSyntax cancelCommandPropertyDeclaration =
PropertyDeclaration(
IdentifierName("global::System.Windows.Input.ICommand"),
Identifier(cancelCommandPropertyName))
.AddModifiers(Token(SyntaxKind.PublicKeyword))
.AddAttributeLists(
AttributeList(SingletonSeparatedList(
Attribute(IdentifierName("global::System.CodeDom.Compiler.GeneratedCode"))
.AddArgumentListArguments(
AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(typeof(RelayCommandGenerator).FullName))),
AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(typeof(RelayCommandGenerator).Assembly.GetName().Version.ToString()))))))
.WithOpenBracketToken(Token(TriviaList(Comment(
$"/// <summary>Gets an <see cref=\"global::System.Windows.Input.ICommand\"/> instance that can be used to cancel <see cref=\"{commandInfo.PropertyName}\"/>.</summary>")),
SyntaxKind.OpenBracketToken,
TriviaList())),
AttributeList(SingletonSeparatedList(Attribute(IdentifierName("global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage")))))
.WithExpressionBody(
ArrowExpressionClause(
AssignmentExpression(
SyntaxKind.CoalesceAssignmentExpression,
IdentifierName(cancelCommandFieldName),
InvocationExpression(
MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
IdentifierName("global::CommunityToolkit.Mvvm.Input.IAsyncRelayCommandExtensions"),
IdentifierName("CreateCancelCommand")))
.AddArgumentListArguments(Argument(IdentifierName(commandInfo.PropertyName))))))
.WithSemicolonToken(Token(SyntaxKind.SemicolonToken));
return ImmutableArray.Create<MemberDeclarationSyntax>(fieldDeclaration, propertyDeclaration, cancelCommandFieldDeclaration, cancelCommandPropertyDeclaration);
}
return ImmutableArray.Create<MemberDeclarationSyntax>(fieldDeclaration, propertyDeclaration);
}
/// <summary>
/// Validates that a target method used as source for a command is unique within its containing type.
/// </summary>
/// <param name="methodSymbol">The input <see cref="IMethodSymbol"/> instance to process.</param>
/// <param name="diagnostics">The current collection of gathered diagnostics.</param>
/// <returns>Whether or not <paramref name="methodSymbol"/> was unique within its containing type.</returns>
private static bool IsCommandDefinitionUnique(IMethodSymbol methodSymbol, in ImmutableArrayBuilder<DiagnosticInfo> diagnostics)
{
// If a duplicate is present in any of the base types, always emit a diagnostic for the current method.
// That is, there is no need to check the order: we assume the priority is top-down in the type hierarchy.
// This check has to be done first, as otherwise there would always be a false positive for the current type.
foreach (ISymbol symbol in methodSymbol.ContainingType.BaseType?.GetAllMembers(methodSymbol.Name) ?? Enumerable.Empty<ISymbol>())
{
if (symbol is IMethodSymbol otherSymbol &&
otherSymbol.HasAttributeWithFullyQualifiedMetadataName("CommunityToolkit.Mvvm.Input.RelayCommandAttribute"))
{
diagnostics.Add(
MultipleRelayCommandMethodOverloadsError,
methodSymbol,
methodSymbol.ContainingType,
methodSymbol);
return false;
}
}
// Check for duplicates in the containing type for the annotated method
foreach (ISymbol symbol in methodSymbol.ContainingType.GetMembers(methodSymbol.Name))
{
if (symbol is IMethodSymbol otherSymbol &&
otherSymbol.HasAttributeWithFullyQualifiedMetadataName("CommunityToolkit.Mvvm.Input.RelayCommandAttribute"))
{
// If the first [RelayCommand] overload is the current symbol, return immediately. This makes it so
// that if multiple overloads are present, only the ones after the first declared one will have
// diagnostics generated for them, while the first one will remain valid and will keep working.
if (SymbolEqualityComparer.Default.Equals(methodSymbol, otherSymbol))
{
return true;
}
// If the two method symbols are partial and either is the implementation of the other one, this is allowed
if ((methodSymbol is { IsPartialDefinition: true, PartialImplementationPart: { } partialImplementation } &&
SymbolEqualityComparer.Default.Equals(otherSymbol, partialImplementation)) ||
(otherSymbol is { IsPartialDefinition: true, PartialImplementationPart: { } otherPartialImplementation } &&
SymbolEqualityComparer.Default.Equals(methodSymbol, otherPartialImplementation)))
{
continue;
}
diagnostics.Add(
MultipleRelayCommandMethodOverloadsError,
methodSymbol,
methodSymbol.ContainingType,
methodSymbol);
return false;
}
}
return true;
}
/// <summary>
/// Get the generated field and property names for the input method.
/// </summary>
/// <param name="methodSymbol">The input <see cref="IMethodSymbol"/> instance to process.</param>
/// <returns>The generated field and property names for <paramref name="methodSymbol"/>.</returns>
public static (string FieldName, string PropertyName) GetGeneratedFieldAndPropertyNames(IMethodSymbol methodSymbol)
{
string propertyName = methodSymbol.Name;
// Strip the "On" prefix, if present. Only do this if the name is longer than 2 characters,
// and if the 3rd character is not a lowercase letter. This is needed to avoid accidentally
// stripping fales positives for "On" prefixes in names such as "Onboard".
if (propertyName.Length > 2 &&
propertyName.StartsWith("On") &&
!char.IsLower(propertyName[2]))
{
propertyName = propertyName.Substring(2);
}
// Strip the "Async" suffix for methods returning a Task type
if (methodSymbol.Name.EndsWith("Async") &&
methodSymbol.ReturnType.HasOrInheritsFromFullyQualifiedMetadataName("System.Threading.Tasks.Task"))
{
propertyName = propertyName.Substring(0, propertyName.Length - "Async".Length);
}
propertyName += "Command";
char firstCharacter = propertyName[0];
char loweredFirstCharacter = char.ToLower(firstCharacter, CultureInfo.InvariantCulture);
// The field name is generated depending on whether the first character can be lowered:
// - If it can, then the field name is just the property name starting in lowercase.
// - If it can't (eg. starts with '中'), then the '_' prefix is added to the property name.
string fieldName = (firstCharacter == loweredFirstCharacter) switch
{
true => $"_{propertyName}",
false => $"{loweredFirstCharacter}{propertyName.Substring(1)}"
};
return (fieldName, propertyName);
}
/// <summary>
/// Gets the type symbols for the input method, if supported.
/// </summary>
/// <param name="methodSymbol">The input <see cref="IMethodSymbol"/> instance to process.</param>
/// <param name="diagnostics">The current collection of gathered diagnostics.</param>
/// <param name="commandInterfaceType">The command interface type name.</param>
/// <param name="commandClassType">The command class type name.</param>
/// <param name="delegateType">The delegate type name for the wrapped method.</param>
/// <param name="supportsCancellation">Indicates whether or not the resulting command supports cancellation.</param>
/// <param name="commandTypeArguments">The type arguments for <paramref name="commandInterfaceType"/> and <paramref name="commandClassType"/>, if any.</param>
/// <param name="commandTypeArgumentsWithNullabilityAnnotations">Same as <paramref name="commandTypeArguments"/>, but with nullability annotations.</param>
/// <param name="delegateTypeArgumentsWithNullabilityAnnotations">The type arguments for <paramref name="delegateType"/>, if any, with nullability annotations.</param>
/// <returns>Whether or not <paramref name="methodSymbol"/> was valid and the requested types have been set.</returns>
private static bool TryMapCommandTypesFromMethod(
IMethodSymbol methodSymbol,
in ImmutableArrayBuilder<DiagnosticInfo> diagnostics,
[NotNullWhen(true)] out string? commandInterfaceType,
[NotNullWhen(true)] out string? commandClassType,
[NotNullWhen(true)] out string? delegateType,
out bool supportsCancellation,
out ImmutableArray<string> commandTypeArguments,
out ImmutableArray<string> commandTypeArgumentsWithNullabilityAnnotations,
out ImmutableArray<string> delegateTypeArgumentsWithNullabilityAnnotations)
{
// Map <void, void> to IRelayCommand, RelayCommand, Action
if (methodSymbol.ReturnsVoid && methodSymbol.Parameters.Length == 0)
{
commandInterfaceType = "global::CommunityToolkit.Mvvm.Input.IRelayCommand";
commandClassType = "global::CommunityToolkit.Mvvm.Input.RelayCommand";
delegateType = "global::System.Action";
supportsCancellation = false;
commandTypeArguments = ImmutableArray<string>.Empty;
commandTypeArgumentsWithNullabilityAnnotations = ImmutableArray<string>.Empty;
delegateTypeArgumentsWithNullabilityAnnotations = ImmutableArray<string>.Empty;
return true;
}
// Map <T, void> to IRelayCommand<T>, RelayCommand<T>, Action<T>
if (methodSymbol.ReturnsVoid &&
methodSymbol.Parameters.Length == 1 &&
methodSymbol.Parameters[0] is IParameterSymbol { RefKind: RefKind.None, Type: { IsRefLikeType: false, TypeKind: not TypeKind.Pointer and not TypeKind.FunctionPointer } } parameter)
{
commandInterfaceType = "global::CommunityToolkit.Mvvm.Input.IRelayCommand";
commandClassType = "global::CommunityToolkit.Mvvm.Input.RelayCommand";
delegateType = "global::System.Action";
supportsCancellation = false;
commandTypeArguments = ImmutableArray.Create(parameter.Type.GetFullyQualifiedName());
commandTypeArgumentsWithNullabilityAnnotations = ImmutableArray.Create(parameter.Type.GetFullyQualifiedNameWithNullabilityAnnotations());
delegateTypeArgumentsWithNullabilityAnnotations = ImmutableArray.Create(parameter.Type.GetFullyQualifiedNameWithNullabilityAnnotations());
return true;
}
// Map all Task-returning methods
if (methodSymbol.ReturnType.HasOrInheritsFromFullyQualifiedMetadataName("System.Threading.Tasks.Task"))
{
// Map <void, Task> to IAsyncRelayCommand, AsyncRelayCommand, Func<Task>
if (methodSymbol.Parameters.Length == 0)
{
commandInterfaceType = "global::CommunityToolkit.Mvvm.Input.IAsyncRelayCommand";
commandClassType = "global::CommunityToolkit.Mvvm.Input.AsyncRelayCommand";
delegateType = "global::System.Func";
supportsCancellation = false;
commandTypeArguments = ImmutableArray<string>.Empty;
commandTypeArgumentsWithNullabilityAnnotations = ImmutableArray<string>.Empty;
delegateTypeArgumentsWithNullabilityAnnotations = ImmutableArray.Create("global::System.Threading.Tasks.Task");
return true;
}
if (methodSymbol.Parameters.Length == 1 &&
methodSymbol.Parameters[0] is IParameterSymbol { RefKind: RefKind.None, Type: { IsRefLikeType: false, TypeKind: not TypeKind.Pointer and not TypeKind.FunctionPointer } } singleParameter)
{
// Map <CancellationToken, Task> to IAsyncRelayCommand, AsyncRelayCommand, Func<CancellationToken, Task>
if (singleParameter.Type.HasFullyQualifiedMetadataName("System.Threading.CancellationToken"))
{
commandInterfaceType = "global::CommunityToolkit.Mvvm.Input.IAsyncRelayCommand";
commandClassType = "global::CommunityToolkit.Mvvm.Input.AsyncRelayCommand";
delegateType = "global::System.Func";
supportsCancellation = true;
commandTypeArguments = ImmutableArray<string>.Empty;
commandTypeArgumentsWithNullabilityAnnotations = ImmutableArray<string>.Empty;
delegateTypeArgumentsWithNullabilityAnnotations = ImmutableArray.Create("global::System.Threading.CancellationToken", "global::System.Threading.Tasks.Task");
return true;
}
// Map <T, Task> to IAsyncRelayCommand<T>, AsyncRelayCommand<T>, Func<T, Task>
commandInterfaceType = "global::CommunityToolkit.Mvvm.Input.IAsyncRelayCommand";
commandClassType = "global::CommunityToolkit.Mvvm.Input.AsyncRelayCommand";
delegateType = "global::System.Func";
supportsCancellation = false;
commandTypeArguments = ImmutableArray.Create(singleParameter.Type.GetFullyQualifiedName());
commandTypeArgumentsWithNullabilityAnnotations = ImmutableArray.Create(singleParameter.Type.GetFullyQualifiedNameWithNullabilityAnnotations());
delegateTypeArgumentsWithNullabilityAnnotations = ImmutableArray.Create(singleParameter.Type.GetFullyQualifiedNameWithNullabilityAnnotations(), "global::System.Threading.Tasks.Task");
return true;
}
// Map <T, CancellationToken, Task> to IAsyncRelayCommand<T>, AsyncRelayCommand<T>, Func<T, CancellationToken, Task>
if (methodSymbol.Parameters.Length == 2 &&
methodSymbol.Parameters[0] is IParameterSymbol { RefKind: RefKind.None, Type: { IsRefLikeType: false, TypeKind: not TypeKind.Pointer and not TypeKind.FunctionPointer } } firstParameter &&
methodSymbol.Parameters[1] is IParameterSymbol { RefKind: RefKind.None, Type: { IsRefLikeType: false, TypeKind: not TypeKind.Pointer and not TypeKind.FunctionPointer } } secondParameter &&
secondParameter.Type.HasFullyQualifiedMetadataName("System.Threading.CancellationToken"))
{
commandInterfaceType = "global::CommunityToolkit.Mvvm.Input.IAsyncRelayCommand";
commandClassType = "global::CommunityToolkit.Mvvm.Input.AsyncRelayCommand";
delegateType = "global::System.Func";
supportsCancellation = true;
commandTypeArguments = ImmutableArray.Create(firstParameter.Type.GetFullyQualifiedName());
commandTypeArgumentsWithNullabilityAnnotations = ImmutableArray.Create(firstParameter.Type.GetFullyQualifiedNameWithNullabilityAnnotations());
delegateTypeArgumentsWithNullabilityAnnotations = ImmutableArray.Create(firstParameter.Type.GetFullyQualifiedNameWithNullabilityAnnotations(), "global::System.Threading.CancellationToken", "global::System.Threading.Tasks.Task");
return true;
}
}
diagnostics.Add(InvalidRelayCommandMethodSignatureError, methodSymbol, methodSymbol.ContainingType, methodSymbol);
commandInterfaceType = null;
commandClassType = null;
delegateType = null;
supportsCancellation = false;
commandTypeArguments = ImmutableArray<string>.Empty;
commandTypeArgumentsWithNullabilityAnnotations = ImmutableArray<string>.Empty;
delegateTypeArgumentsWithNullabilityAnnotations = ImmutableArray<string>.Empty;
return false;
}
/// <summary>
/// Checks whether or not the user has requested to configure the handling of concurrent executions.
/// </summary>
/// <param name="methodSymbol">The input <see cref="IMethodSymbol"/> instance to process.</param>
/// <param name="attributeData">The <see cref="AttributeData"/> instance the method was annotated with.</param>
/// <param name="commandClassType">The command class type name.</param>
/// <param name="diagnostics">The current collection of gathered diagnostics.</param>
/// <param name="allowConcurrentExecutions">Whether or not concurrent executions have been enabled.</param>
/// <returns>Whether or not a value for <paramref name="allowConcurrentExecutions"/> could be retrieved successfully.</returns>
private static bool TryGetAllowConcurrentExecutionsSwitch(
IMethodSymbol methodSymbol,
AttributeData attributeData,
string commandClassType,
in ImmutableArrayBuilder<DiagnosticInfo> diagnostics,
out bool allowConcurrentExecutions)
{
// Try to get the custom switch for concurrent executions (the default is false)
if (!attributeData.TryGetNamedArgument("AllowConcurrentExecutions", out allowConcurrentExecutions))
{
allowConcurrentExecutions = false;
return true;
}
// If the current type is an async command type and concurrent execution is disabled, pass that value to the constructor.
// If concurrent executions are allowed, there is no need to add any additional argument, as that is the default value.
if (commandClassType is "global::CommunityToolkit.Mvvm.Input.AsyncRelayCommand")
{
return true;
}
else
{
diagnostics.Add(InvalidConcurrentExecutionsParameterError, methodSymbol, methodSymbol.ContainingType, methodSymbol);
return false;
}
}
/// <summary>
/// Checks whether or not the user has requested to configure the task scheduler exception flow option.
/// </summary>
/// <param name="methodSymbol">The input <see cref="IMethodSymbol"/> instance to process.</param>
/// <param name="attributeData">The <see cref="AttributeData"/> instance the method was annotated with.</param>
/// <param name="commandClassType">The command class type name.</param>
/// <param name="diagnostics">The current collection of gathered diagnostics.</param>
/// <param name="flowExceptionsToTaskScheduler">Whether or not task scheduler exception flow have been enabled.</param>
/// <returns>Whether or not a value for <paramref name="flowExceptionsToTaskScheduler"/> could be retrieved successfully.</returns>
private static bool TryGetFlowExceptionsToTaskSchedulerSwitch(
IMethodSymbol methodSymbol,
AttributeData attributeData,
string commandClassType,
in ImmutableArrayBuilder<DiagnosticInfo> diagnostics,
out bool flowExceptionsToTaskScheduler)
{
// Try to get the custom switch for task scheduler exception flow (the default is false)
if (!attributeData.TryGetNamedArgument("FlowExceptionsToTaskScheduler", out flowExceptionsToTaskScheduler))
{
flowExceptionsToTaskScheduler = false;
return true;
}
// Just like with the concurrency control option, check that the target command type is asynchronous
if (commandClassType is "global::CommunityToolkit.Mvvm.Input.AsyncRelayCommand")
{
return true;
}
else
{
diagnostics.Add(InvalidFlowExceptionsToTaskSchedulerParameterError, methodSymbol, methodSymbol.ContainingType, methodSymbol);
return false;
}
}
/// <summary>
/// Checks whether or not the user has requested to also generate a cancel command.
/// </summary>
/// <param name="methodSymbol">The input <see cref="IMethodSymbol"/> instance to process.</param>
/// <param name="attributeData">The <see cref="AttributeData"/> instance the method was annotated with.</param>
/// <param name="commandClassType">The command class type name.</param>
/// <param name="supportsCancellation">Indicates whether or not the command supports cancellation.</param>
/// <param name="diagnostics">The current collection of gathered diagnostics.</param>
/// <param name="generateCancelCommand">Whether or not concurrent executions have been enabled.</param>
/// <returns>Whether or not a value for <paramref name="generateCancelCommand"/> could be retrieved successfully.</returns>
private static bool TryGetIncludeCancelCommandSwitch(
IMethodSymbol methodSymbol,
AttributeData attributeData,
string commandClassType,
bool supportsCancellation,
in ImmutableArrayBuilder<DiagnosticInfo> diagnostics,
out bool generateCancelCommand)
{
// Try to get the custom switch for cancel command generation (the default is false)
if (!attributeData.TryGetNamedArgument("IncludeCancelCommand", out generateCancelCommand))
{
generateCancelCommand = false;
return true;
}
// If the current type is an async command type and cancellation is supported, pass that value to the constructor.
// Otherwise, the current attribute use is not valid, so a diagnostic message should be produced.
if (commandClassType is "global::CommunityToolkit.Mvvm.Input.AsyncRelayCommand" &&
supportsCancellation)
{
return true;
}
else
{
diagnostics.Add(InvalidIncludeCancelCommandParameterError, methodSymbol, methodSymbol.ContainingType, methodSymbol);
return false;
}
}
/// <summary>
/// Tries to get the expression type for the "CanExecute" property, if available.
/// </summary>
/// <param name="methodSymbol">The input <see cref="IMethodSymbol"/> instance to process.</param>
/// <param name="attributeData">The <see cref="AttributeData"/> instance for <paramref name="methodSymbol"/>.</param>
/// <param name="commandTypeArguments">The command type arguments, if any.</param>
/// <param name="token">The cancellation token for the current operation.</param>
/// <param name="diagnostics">The current collection of gathered diagnostics.</param>
/// <param name="canExecuteMemberName">The resulting can execute member name, if available.</param>
/// <param name="canExecuteExpressionType">The resulting expression type, if available.</param>
/// <returns>Whether or not a value for <paramref name="canExecuteMemberName"/> and <paramref name="canExecuteExpressionType"/> could be determined (may include <see langword="null"/>).</returns>
private static bool TryGetCanExecuteExpressionType(
IMethodSymbol methodSymbol,
AttributeData attributeData,
ImmutableArray<string> commandTypeArguments,
CancellationToken token,
in ImmutableArrayBuilder<DiagnosticInfo> diagnostics,
out string? canExecuteMemberName,
out CanExecuteExpressionType? canExecuteExpressionType)
{
// Get the can execute member, if any
if (!attributeData.TryGetNamedArgument("CanExecute", out string? memberName))
{
canExecuteMemberName = null;
canExecuteExpressionType = null;
return true;
}
if (memberName is null)
{
diagnostics.Add(InvalidCanExecuteMemberNameError, methodSymbol, memberName ?? string.Empty, methodSymbol.ContainingType);
goto Failure;
}
ImmutableArray<ISymbol> canExecuteSymbols = methodSymbol.ContainingType!.GetAllMembers(memberName).ToImmutableArray();
if (canExecuteSymbols.IsEmpty)
{
// Special case for when the target member is a generated property from [ObservableProperty]
if (TryGetCanExecuteMemberFromGeneratedProperty(memberName, methodSymbol.ContainingType, commandTypeArguments, token, out canExecuteExpressionType))
{
canExecuteMemberName = memberName;
return true;
}
diagnostics.Add(InvalidCanExecuteMemberNameError, methodSymbol, memberName, methodSymbol.ContainingType);
}
else if (canExecuteSymbols.Length > 1)
{
diagnostics.Add(MultipleCanExecuteMemberNameMatchesError, methodSymbol, memberName, methodSymbol.ContainingType);
}
else if (TryGetCanExecuteExpressionFromSymbol(canExecuteSymbols[0], commandTypeArguments, out canExecuteExpressionType))
{
canExecuteMemberName = memberName;
return true;
}
else
{
diagnostics.Add(InvalidCanExecuteMemberError, methodSymbol, memberName, methodSymbol.ContainingType);
}
Failure:
canExecuteMemberName = null;
canExecuteExpressionType = null;
return false;
}
/// <summary>
/// Gets the expression type for the can execute logic, if possible.
/// </summary>
/// <param name="canExecuteSymbol">The can execute member symbol (either a method or a property).</param>
/// <param name="commandTypeArguments">The type arguments for the command interface, if any.</param>
/// <param name="canExecuteExpressionType">The resulting can execute expression type, if available.</param>
/// <returns>Whether or not <paramref name="canExecuteExpressionType"/> was set and the input symbol was valid.</returns>
private static bool TryGetCanExecuteExpressionFromSymbol(
ISymbol canExecuteSymbol,
ImmutableArray<string> commandTypeArguments,
[NotNullWhen(true)] out CanExecuteExpressionType? canExecuteExpressionType)
{
if (canExecuteSymbol is IMethodSymbol canExecuteMethodSymbol)
{
// The return type must always be a bool
if (canExecuteMethodSymbol.ReturnType is not { SpecialType: SpecialType.System_Boolean })
{
goto Failure;
}
// Parameterless methods are always valid
if (canExecuteMethodSymbol.Parameters.IsEmpty)
{
// If the command is generic, the input value is ignored
if (commandTypeArguments.Length > 0)
{
canExecuteExpressionType = CanExecuteExpressionType.MethodInvocationLambdaWithDiscard;
}
else
{
canExecuteExpressionType = CanExecuteExpressionType.MethodGroup;
}
return true;
}
// If the method has parameters, it has to have a single one matching the command type
if (canExecuteMethodSymbol.Parameters.Length == 1 &&
commandTypeArguments.Length == 1 &&
canExecuteMethodSymbol.Parameters[0].Type.HasFullyQualifiedName(commandTypeArguments[0]))
{
// Create a method group expression again
canExecuteExpressionType = CanExecuteExpressionType.MethodGroup;
return true;
}
}
else if (canExecuteSymbol is IPropertySymbol { GetMethod: not null } canExecutePropertySymbol)
{
// The property type must always be a bool
if (canExecutePropertySymbol.Type is not { SpecialType: SpecialType.System_Boolean })
{
goto Failure;
}
if (commandTypeArguments.Length > 0)
{
canExecuteExpressionType = CanExecuteExpressionType.PropertyAccessLambdaWithDiscard;
}
else
{
canExecuteExpressionType = CanExecuteExpressionType.PropertyAccessLambda;
}
return true;
}
Failure:
canExecuteExpressionType = null;
return false;
}
/// <summary>
/// Gets the expression type for the can execute logic, if possible.
/// </summary>
/// <param name="memberName">The member name passed to <c>[RelayCommand(CanExecute = ...)]</c>.</param>
/// <param name="containingType">The containing type for the method annotated with <c>[RelayCommand]</c>.</param>
/// <param name="commandTypeArguments">The type arguments for the command interface, if any.</param>
/// <param name="token">The cancellation token for the current operation.</param>
/// <param name="canExecuteExpressionType">The resulting can execute expression type, if available.</param>
/// <returns>Whether or not <paramref name="canExecuteExpressionType"/> was set and the input symbol was valid.</returns>
private static bool TryGetCanExecuteMemberFromGeneratedProperty(
string memberName,
INamedTypeSymbol containingType,
ImmutableArray<string> commandTypeArguments,
CancellationToken token,
[NotNullWhen(true)] out CanExecuteExpressionType? canExecuteExpressionType)
{
foreach (ISymbol memberSymbol in containingType.GetAllMembers())
{
// Only look for instance fields of bool type
if (memberSymbol is not IFieldSymbol { IsStatic: false, Type.SpecialType: SpecialType.System_Boolean } fieldSymbol)
{
continue;
}
token.ThrowIfCancellationRequested();
ImmutableArray<AttributeData> attributes = memberSymbol.GetAttributes();
// Only filter fields with the [ObservableProperty] attribute
if (memberSymbol is IFieldSymbol &&
!attributes.Any(static a => a.AttributeClass?.HasFullyQualifiedMetadataName(
"CommunityToolkit.Mvvm.ComponentModel.ObservablePropertyAttribute") == true))
{
continue;
}
// Get the target property name either directly or matching the generated one
string propertyName = ObservablePropertyGenerator.Execute.GetGeneratedPropertyName(fieldSymbol);
// If the generated property name matches, get the right expression type
if (memberName == propertyName)
{
if (commandTypeArguments.Length > 0)
{
canExecuteExpressionType = CanExecuteExpressionType.PropertyAccessLambdaWithDiscard;
}
else
{
canExecuteExpressionType = CanExecuteExpressionType.PropertyAccessLambda;
}
return true;
}
}
canExecuteExpressionType = null;
return false;
}
/// <summary>
/// Gathers all forwarded attributes for the generated field and property.
/// </summary>
/// <param name="methodSymbol">The input <see cref="IMethodSymbol"/> instance to process.</param>
/// <param name="semanticModel">The <see cref="SemanticModel"/> instance for the current run.</param>
/// <param name="token">The cancellation token for the current operation.</param>
/// <param name="diagnostics">The current collection of gathered diagnostics.</param>
/// <param name="forwardedAttributes">The resulting attributes to forward.</param>
private static void GatherForwardedAttributes(
IMethodSymbol methodSymbol,
SemanticModel semanticModel,
CancellationToken token,
in ImmutableArrayBuilder<DiagnosticInfo> diagnostics,
out ImmutableArray<AttributeInfo> forwardedAttributes)
{
using ImmutableArrayBuilder<AttributeInfo> forwardedAttributesInfo = ImmutableArrayBuilder<AttributeInfo>.Rent();
static void GatherForwardedAttributes(
IMethodSymbol methodSymbol,
SemanticModel semanticModel,
CancellationToken token,
in ImmutableArrayBuilder<DiagnosticInfo> diagnostics,
in ImmutableArrayBuilder<AttributeInfo> forwardedAttributesInfo)
{
// Get the single syntax reference for the input method symbol (there should be only one)
if (methodSymbol.DeclaringSyntaxReferences is not [SyntaxReference syntaxReference])
{
return;
}
// Try to get the target method declaration syntax node
if (syntaxReference.GetSyntax(token) is not MethodDeclarationSyntax methodDeclaration)
{