@@ -178,31 +178,61 @@ private static string GenerateSource(
178178 sb . AppendLine ( ) ;
179179 }
180180
181- sb . Append ( "partial " ) . Append ( GetKind ( type ) ) . Append ( ' ' ) . Append ( type . Name ) . AppendLine ( ) ;
182- sb . AppendLine ( "{" ) ;
183- sb . Append ( " public static global::PatternKit.Messaging.Message<" ) . Append ( payload ) . Append ( "> " ) . Append ( config . FactoryName ) . Append ( '(' ) . Append ( payload ) . Append ( " payload" ) ;
181+ var containingTypes = GetContainingTypes ( type ) ;
182+ var indentLevel = 0 ;
183+ foreach ( var containingType in containingTypes )
184+ {
185+ AppendTypeDeclaration ( sb , containingType , indentLevel ) ;
186+ sb . AppendLine ( ) ;
187+ sb . AppendLine ( new string ( ' ' , indentLevel * 4 ) + "{" ) ;
188+ indentLevel ++ ;
189+ }
190+
191+ AppendTypeDeclaration ( sb , type , indentLevel ) ;
192+ sb . AppendLine ( ) ;
193+ var indent = new string ( ' ' , indentLevel * 4 ) ;
194+ sb . AppendLine ( indent + "{" ) ;
195+ var memberIndent = indent + " " ;
196+ var bodyIndent = memberIndent + " " ;
197+ sb . Append ( memberIndent ) . Append ( "public static global::PatternKit.Messaging.Message<" ) . Append ( payload ) . Append ( "> " ) . Append ( config . FactoryName ) . Append ( '(' ) . Append ( payload ) . Append ( " payload" ) ;
184198 foreach ( var header in headers )
185199 sb . Append ( ", " ) . Append ( header . ValueType ) . Append ( ' ' ) . Append ( header . ParameterName ) ;
186200 sb . AppendLine ( ")" ) ;
187- sb . Append ( " => global::PatternKit.Messaging.Message<") . Append ( payload ) . AppendLine ( ">.Create(payload)" ) ;
201+ sb . Append ( bodyIndent ) . Append ( " => global::PatternKit.Messaging.Message<") . Append ( payload ) . AppendLine ( ">.Create(payload)" ) ;
188202 foreach ( var header in headers )
189- sb . Append ( " .WithHeader(\" ") . Append ( Escape ( header . Name ) ) . Append ( "\" , " ) . Append ( header . ParameterName ) . AppendLine ( ")" ) ;
190- sb . AppendLine ( " ;" ) ;
203+ sb . Append ( bodyIndent ) . Append ( " .WithHeader(\" ") . Append ( Escape ( header . Name ) ) . Append ( "\" , " ) . Append ( header . ParameterName ) . AppendLine ( ")" ) ;
204+ sb . Append ( bodyIndent ) . AppendLine ( " ;" ) ;
191205 sb . AppendLine ( ) ;
192206
193- sb . Append ( " public static global::PatternKit.Messaging.MessageContext ") . Append ( config . ContextFactoryName )
207+ sb . Append ( memberIndent ) . Append ( " public static global::PatternKit.Messaging.MessageContext ") . Append ( config . ContextFactoryName )
194208 . Append ( "(global::PatternKit.Messaging.Message<" ) . Append ( payload ) . Append ( "> message, global::System.Threading.CancellationToken cancellationToken = default)" ) ;
195209 sb . AppendLine ( ) ;
196- sb . AppendLine ( " {" ) ;
197- sb . AppendLine ( " if (message is null)" ) ;
198- sb . AppendLine ( " throw new global::System.ArgumentNullException(nameof(message));" ) ;
210+ sb . Append ( memberIndent ) . AppendLine ( "{" ) ;
211+ sb . Append ( bodyIndent ) . AppendLine ( "if (message is null)" ) ;
212+ sb . Append ( bodyIndent ) . AppendLine ( " throw new global::System.ArgumentNullException(nameof(message));" ) ;
199213 sb . AppendLine ( ) ;
200- sb . AppendLine ( " return global::PatternKit.Messaging.MessageContext.From(message, cancellationToken);" ) ;
201- sb . AppendLine ( " }" ) ;
202- sb . AppendLine ( "}" ) ;
214+ sb . Append ( bodyIndent ) . AppendLine ( "return global::PatternKit.Messaging.MessageContext.From(message, cancellationToken);" ) ;
215+ sb . AppendLine ( memberIndent + "}" ) ;
216+ sb . AppendLine ( indent + "}" ) ;
217+ for ( var i = containingTypes . Length - 1 ; i >= 0 ; i -- )
218+ {
219+ sb . AppendLine ( new string ( ' ' , i * 4 ) + "}" ) ;
220+ }
221+
203222 return sb . ToString ( ) ;
204223 }
205224
225+ private static INamedTypeSymbol [ ] GetContainingTypes ( INamedTypeSymbol type )
226+ {
227+ var containingTypes = new Stack < INamedTypeSymbol > ( ) ;
228+ for ( var current = type . ContainingType ; current is not null ; current = current . ContainingType )
229+ {
230+ containingTypes . Push ( current ) ;
231+ }
232+
233+ return containingTypes . ToArray ( ) ;
234+ }
235+
206236 private static string ToParameterName ( string headerName )
207237 {
208238 var sb = new StringBuilder ( ) ;
@@ -231,8 +261,30 @@ private static bool IsValidIdentifier(string value)
231261 && SyntaxFacts . IsValidIdentifier ( value )
232262 && SyntaxFacts . GetKeywordKind ( value ) == SyntaxKind . None ;
233263
234- private static string GetKind ( INamedTypeSymbol type )
235- => type . TypeKind == TypeKind . Struct ? "struct" : "class" ;
264+ private static void AppendTypeDeclaration ( StringBuilder sb , INamedTypeSymbol type , int indentLevel )
265+ {
266+ sb . Append ( new string ( ' ' , indentLevel * 4 ) ) ;
267+ sb . Append ( GetAccessibility ( type . DeclaredAccessibility ) ) . Append ( ' ' ) ;
268+ if ( type . IsStatic )
269+ sb . Append ( "static " ) ;
270+ else if ( type . IsAbstract && type . TypeKind == TypeKind . Class )
271+ sb . Append ( "abstract " ) ;
272+ else if ( type . IsSealed && type . TypeKind == TypeKind . Class )
273+ sb . Append ( "sealed " ) ;
274+ sb . Append ( "partial " ) . Append ( type . TypeKind == TypeKind . Struct ? "struct" : "class" ) . Append ( ' ' ) . Append ( type . Name ) ;
275+ }
276+
277+ private static string GetAccessibility ( Accessibility accessibility )
278+ => accessibility switch
279+ {
280+ Accessibility . Public => "public" ,
281+ Accessibility . Internal => "internal" ,
282+ Accessibility . Private => "private" ,
283+ Accessibility . Protected => "protected" ,
284+ Accessibility . ProtectedAndInternal => "private protected" ,
285+ Accessibility . ProtectedOrInternal => "protected internal" ,
286+ _ => "internal"
287+ } ;
236288
237289 private static string Escape ( string value ) => value . Replace ( "\\ " , "\\ \\ " ) . Replace ( "\" " , "\\ \" " ) ;
238290
0 commit comments