11using System . Text ;
22using CSharpFunctionalExtensions . HttpResults . Generators . Builders ;
3+ using CSharpFunctionalExtensions . HttpResults . Generators . Utils ;
34using Microsoft . CodeAnalysis ;
45using Microsoft . CodeAnalysis . CSharp . Syntax ;
56using Microsoft . CodeAnalysis . Text ;
@@ -43,33 +44,28 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
4344 var ( compilation , classDeclarations ) = source ;
4445
4546 var mapperClasses = new List < ClassDeclarationSyntax > ( ) ;
46- var requiredNamespaces = new HashSet < string > ( ) ;
4747
4848 Parallel . ForEach (
4949 classDeclarations ,
5050 classDeclaration =>
5151 {
52- var semanticModel = compilation . GetSemanticModel ( classDeclaration . SyntaxTree ) ;
53- var namespaceName = GetNamespace ( classDeclaration , semanticModel ) ;
54-
5552 lock ( mapperClasses )
5653 {
5754 mapperClasses . Add ( classDeclaration ) ;
58- requiredNamespaces . Add ( namespaceName ) ;
5955 }
6056 }
6157 ) ;
6258
6359 if ( ! ResultExtensionsGeneratorValidator . CheckRules ( mapperClasses , context ) )
6460 return ;
6561
66- var ( fileName , sourceText ) = CreateErrorMapperInstancesClass ( mapperClasses , requiredNamespaces ) ;
62+ var ( fileName , sourceText ) = CreateErrorMapperInstancesClass ( mapperClasses , compilation ) ;
6763 context . AddSource ( fileName , SourceText . From ( sourceText , Encoding . UTF8 ) ) ;
6864
6965 var classBuilders = new List < ClassBuilder >
7066 {
71- new ResultExtensionsClassBuilder ( requiredNamespaces , mapperClasses , compilation ) ,
72- new UnitResultExtensionsClassBuilder ( requiredNamespaces , mapperClasses , compilation ) ,
67+ new ResultExtensionsClassBuilder ( mapperClasses , compilation ) ,
68+ new UnitResultExtensionsClassBuilder ( mapperClasses , compilation ) ,
7369 } ;
7470
7571 foreach ( var classBuilder in classBuilders )
@@ -83,7 +79,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
8379 /// </summary>
8480 private static ( string FileName , string SourceText ) CreateErrorMapperInstancesClass (
8581 List < ClassDeclarationSyntax > mapperClasses ,
86- HashSet < string > requiredNamespaces
82+ Compilation compilation
8783 )
8884 {
8985 var sourceBuilder = new StringBuilder ( ) ;
@@ -92,20 +88,20 @@ HashSet<string> requiredNamespaces
9288 sourceBuilder . AppendLine ( ) ;
9389 sourceBuilder . AppendLine ( "#nullable enable" ) ;
9490 sourceBuilder . AppendLine ( ) ;
95- requiredNamespaces
96- . Where ( @namespace => ! @namespace . StartsWith ( "global" ) )
97- . Distinct ( )
98- . Select ( @namespace => $ "using { @namespace } ;")
99- . ToList ( )
100- . ForEach ( @using => sourceBuilder . AppendLine ( @using ) ) ;
10191 sourceBuilder . AppendLine ( ) ;
10292
10393 sourceBuilder . AppendLine ( "public static class ErrorMapperInstances {" ) ;
10494
105- foreach ( var mapperName in mapperClasses )
106- sourceBuilder . AppendLine (
107- $ " public static { mapperName . Identifier . Text } { mapperName . Identifier . Text } {{ get; }} = new();"
108- ) ;
95+ foreach ( var mapper in mapperClasses )
96+ {
97+ var semanticModel = compilation . GetSemanticModel ( mapper . SyntaxTree ) ;
98+
99+ if ( semanticModel . GetDeclaredSymbol ( mapper ) is not ITypeSymbol mapperSymbol )
100+ continue ;
101+
102+ var mapperType = TypeNameResolver . GetFullyQualifiedTypeName ( mapperSymbol ) ;
103+ sourceBuilder . AppendLine ( $ " public static { mapperType } { mapper . Identifier . Text } {{ get; }} = new();") ;
104+ }
109105
110106 sourceBuilder . AppendLine ( "}" ) ;
111107
@@ -127,16 +123,4 @@ private static bool ImplementsResultErrorMapper(ITypeSymbol? classSymbol)
127123 interfaceSymbol . Name . StartsWith ( ResultErrorMapperInterface )
128124 ) ;
129125 }
130-
131- /// <summary>
132- /// Retrieves the namespace of a class declaration.
133- /// </summary>
134- /// <param name="classDeclaration">The class declaration syntax node.</param>
135- /// <param name="semanticModel">The semantic model for the syntax tree.</param>
136- /// <returns>The namespace of the class, or an empty string if the namespace cannot be determined.</returns>
137- private static string GetNamespace ( ClassDeclarationSyntax classDeclaration , SemanticModel semanticModel )
138- {
139- var symbol = semanticModel . GetDeclaredSymbol ( classDeclaration ) ;
140- return symbol ? . ContainingNamespace ? . ToString ( ) ?? string . Empty ;
141- }
142126}
0 commit comments