33using Microsoft . CodeAnalysis . Text ;
44using System . Text ;
55
6- namespace SourceGen ;
7-
8- [ Generator ]
9- public class MySourceGenerator : IIncrementalGenerator
6+ namespace SourceGen
107{
11- public void Initialize ( IncrementalGeneratorInitializationContext context )
8+ [ Generator ]
9+ public class MySourceGenerator : IIncrementalGenerator
1210 {
13- // Define the attribute
14- context . RegisterPostInitializationOutput ( ctx =>
11+ public void Initialize ( IncrementalGeneratorInitializationContext context )
1512 {
16- var attributeSource = @"// <auto-generated/>
13+ // Define the attribute
14+ context . RegisterPostInitializationOutput ( ctx =>
15+ {
16+ var attributeSource = @"// <auto-generated/>
1717namespace SourceGenAttribute
1818{
1919 [System.AttributeUsage(System.AttributeTargets.Class)]
2020 public class GenStuffAttribute : System.Attribute
2121 {
2222 }
2323}" ;
24- ctx . AddSource ( "GenStuffAttribute.g.cs" , SourceText . From ( attributeSource , Encoding . UTF8 ) ) ;
25- } ) ;
24+ ctx . AddSource ( "GenStuffAttribute.g.cs" , SourceText . From ( attributeSource , Encoding . UTF8 ) ) ;
25+ } ) ;
2626
27- // Find classes with the attribute
28- IncrementalValuesProvider < INamedTypeSymbol > classDeclarations = context . SyntaxProvider
29- . ForAttributeWithMetadataName (
30- "SourceGenAttribute.GenStuffAttribute" ,
31- ( node , _ ) => node is ClassDeclarationSyntax ,
32- ( generatorAttributeSyntaxContext , _ ) => ( INamedTypeSymbol ) generatorAttributeSyntaxContext . TargetSymbol ) ;
27+ // Find classes with the attribute
28+ IncrementalValuesProvider < INamedTypeSymbol > classDeclarations = context . SyntaxProvider
29+ . ForAttributeWithMetadataName (
30+ "SourceGenAttribute.GenStuffAttribute" ,
31+ ( node , _ ) => node is ClassDeclarationSyntax ,
32+ ( generatorAttributeSyntaxContext , _ ) => ( INamedTypeSymbol ) generatorAttributeSyntaxContext . TargetSymbol ) ;
3333
34- // Generate extension methods
35- context . RegisterSourceOutput ( classDeclarations , ( spc , classSymbol ) =>
36- {
37- if ( classSymbol . ContainingNamespace . IsGlobalNamespace )
34+ // Generate extension methods
35+ context . RegisterSourceOutput ( classDeclarations , ( spc , classSymbol ) =>
3836 {
39- return ;
40- }
41- var className = classSymbol . Name ;
42- var namespaceName = classSymbol . ContainingNamespace . ToDisplayString ( ) ;
37+ if ( classSymbol . ContainingNamespace . IsGlobalNamespace )
38+ {
39+ return ;
40+ }
41+ var className = classSymbol . Name ;
42+ var namespaceName = classSymbol . ContainingNamespace . ToDisplayString ( ) ;
4343
44- var source = $@ "// <auto-generated/>
44+ var source = $@ "// <auto-generated/>
4545namespace { namespaceName }
4646{{
4747 public static class { className } Extensions
@@ -53,7 +53,8 @@ public static string GeneratedMethod(this {className} obj)
5353 }}
5454}}" ;
5555
56- spc . AddSource ( $ "{ className } Extensions.g.cs", SourceText . From ( source , Encoding . UTF8 ) ) ;
57- } ) ;
56+ spc . AddSource ( $ "{ className } Extensions.g.cs", SourceText . From ( source , Encoding . UTF8 ) ) ;
57+ } ) ;
58+ }
5859 }
59- }
60+ }
0 commit comments