@@ -23,28 +23,32 @@ public sealed partial class IMessengerRegisterAllGenerator : IIncrementalGenerat
23
23
/// <inheritdoc/>
24
24
public void Initialize ( IncrementalGeneratorInitializationContext context )
25
25
{
26
- // Get all class declarations
26
+ // Get all class declarations. This pipeline step also needs to filter out duplicate recipient
27
+ // definitions (it might happen if a recipient has partial declarations). To do this, all pairs
28
+ // of class declarations and associated symbols are gathered, and then only the pair where the
29
+ // class declaration is the first syntax reference for the associated symbol is kept.
27
30
IncrementalValuesProvider < INamedTypeSymbol > typeSymbols =
28
31
context . SyntaxProvider
29
32
. CreateSyntaxProvider (
30
- static ( node , _ ) => node is ClassDeclarationSyntax { BaseList . Types . Count : > 0 } ,
31
- static ( context , _ ) => ( INamedTypeSymbol ) context . SemanticModel . GetDeclaredSymbol ( context . Node ) ! ) ;
33
+ static ( node , _ ) => node is ClassDeclarationSyntax ,
34
+ static ( context , _ ) => ( context . Node , Symbol : ( INamedTypeSymbol ) context . SemanticModel . GetDeclaredSymbol ( context . Node ) ! ) )
35
+ . Where ( static item =>
36
+ item . Symbol . DeclaringSyntaxReferences . Length > 0 &&
37
+ item . Symbol . DeclaringSyntaxReferences [ 0 ] is SyntaxReference syntaxReference &&
38
+ syntaxReference . SyntaxTree == item . Node . SyntaxTree &&
39
+ syntaxReference . Span == item . Node . Span )
40
+ . Select ( static ( item , _ ) => item . Symbol ) ;
32
41
33
42
// Get the target IRecipient<TMessage> interfaces and filter out other types
34
43
IncrementalValuesProvider < ( INamedTypeSymbol Type , ImmutableArray < INamedTypeSymbol > Interfaces ) > typeAndInterfaceSymbols =
35
44
typeSymbols
36
45
. Select ( static ( item , _ ) => ( item , Interfaces : Execute . GetInterfaces ( item ) ) )
37
46
. Where ( static item => ! item . Interfaces . IsEmpty ) ;
38
47
39
- // Get the recipient info for all target types. This pipeline step also needs to filter out
40
- // duplicate recipient definitions (it might happen if a recipient has partial declarations)
48
+ // Get the recipient info for all target types
41
49
IncrementalValuesProvider < RecipientInfo > recipientInfo =
42
50
typeAndInterfaceSymbols
43
51
. Select ( static ( item , _ ) => Execute . GetInfo ( item . Type , item . Interfaces ) )
44
- . WithComparer ( RecipientInfo . Comparer . Default )
45
- . Collect ( )
46
- . Select ( static ( item , _ ) => item . Distinct ( RecipientInfo . EqualityComparerByFilenameHint ) )
47
- . SelectMany ( static ( item , _ ) => item )
48
52
. WithComparer ( RecipientInfo . Comparer . Default ) ;
49
53
50
54
// Check whether the header file is needed
0 commit comments