@@ -23,40 +23,33 @@ public sealed partial class RelayCommandGenerator : IIncrementalGenerator
23
23
/// <inheritdoc/>
24
24
public void Initialize ( IncrementalGeneratorInitializationContext context )
25
25
{
26
- // Get all method declarations with at least one attribute
27
- IncrementalValuesProvider < IMethodSymbol > methodSymbols =
26
+ // Gather info for all annotated command methods (starting from method declarations with at least one attribute)
27
+ IncrementalValuesProvider < ( HierarchyInfo Hierarchy , Result < CommandInfo ? > Info ) > commandInfoWithErrors =
28
28
context . SyntaxProvider
29
29
. CreateSyntaxProvider (
30
30
static ( node , _ ) => node is MethodDeclarationSyntax { Parent : ClassDeclarationSyntax , AttributeLists . Count : > 0 } ,
31
- static ( context , _ ) =>
31
+ static ( context , token ) =>
32
32
{
33
33
if ( ! context . SemanticModel . Compilation . HasLanguageVersionAtLeastEqualTo ( LanguageVersion . CSharp8 ) )
34
34
{
35
35
return default ;
36
36
}
37
37
38
- return ( IMethodSymbol ) context . SemanticModel . GetDeclaredSymbol ( context . Node ) ! ;
39
- } )
40
- . Where ( static item => item is not null ) ! ;
38
+ IMethodSymbol methodSymbol = ( IMethodSymbol ) context . SemanticModel . GetDeclaredSymbol ( context . Node , token ) ! ;
41
39
42
- // Filter the methods using [RelayCommand]
43
- IncrementalValuesProvider < ( IMethodSymbol Symbol , AttributeData Attribute ) > methodSymbolsWithAttributeData =
44
- methodSymbols
45
- . Select ( static ( item , _ ) => (
46
- item ,
47
- Attribute : item . GetAttributes ( ) . FirstOrDefault ( a => a . AttributeClass ? . HasFullyQualifiedName ( "global::CommunityToolkit.Mvvm.Input.RelayCommandAttribute" ) == true ) ) )
48
- . Where ( static item => item . Attribute is not null ) ! ;
40
+ // Filter the methods using [RelayCommand]
41
+ if ( ! methodSymbol . TryGetAttributeWithFullyQualifiedName ( "global::CommunityToolkit.Mvvm.Input.RelayCommandAttribute" , out AttributeData ? attribute ) )
42
+ {
43
+ return default ;
44
+ }
49
45
50
- // Gather info for all annotated command methods
51
- IncrementalValuesProvider < ( HierarchyInfo Hierarchy , Result < CommandInfo ? > Info ) > commandInfoWithErrors =
52
- methodSymbolsWithAttributeData
53
- . Select ( static ( item , _ ) =>
54
- {
55
- HierarchyInfo hierarchy = HierarchyInfo . From ( item . Symbol . ContainingType ) ;
56
- CommandInfo ? commandInfo = Execute . GetInfo ( item . Symbol , item . Attribute , out ImmutableArray < Diagnostic > diagnostics ) ;
46
+ // Produce the incremental models
47
+ HierarchyInfo hierarchy = HierarchyInfo . From ( methodSymbol . ContainingType ) ;
48
+ CommandInfo ? commandInfo = Execute . GetInfo ( methodSymbol , attribute , out ImmutableArray < Diagnostic > diagnostics ) ;
57
49
58
- return ( hierarchy , new Result < CommandInfo ? > ( commandInfo , diagnostics ) ) ;
59
- } ) ;
50
+ return ( Hierarchy : hierarchy , new Result < CommandInfo ? > ( commandInfo , diagnostics ) ) ;
51
+ } )
52
+ . Where ( static item => item . Hierarchy is not null ) ;
60
53
61
54
// Output the diagnostics
62
55
context . ReportDiagnostics ( commandInfoWithErrors . Select ( static ( item , _ ) => item . Info . Errors ) ) ;
@@ -66,7 +59,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
66
59
commandInfoWithErrors
67
60
. Where ( static item => item . Info . Value is not null )
68
61
. Select ( static ( item , _ ) => ( item . Hierarchy , item . Info . Value ! ) )
69
- . WithComparers ( HierarchyInfo . Comparer . Default , CommandInfo . Comparer . Default ) ;
62
+ . WithComparers ( HierarchyInfo . Comparer . Default , CommandInfo . Comparer . Default ) ;
70
63
71
64
// Generate the commands
72
65
context . RegisterSourceOutput ( commandInfo , static ( context , item ) =>
0 commit comments