2
2
// The .NET Foundation licenses this file to you under the MIT license.
3
3
4
4
using System ;
5
+ using System . Globalization ;
6
+ using System . IO ;
5
7
using System . Linq ;
6
8
using System . Text ;
7
9
using Microsoft . AspNetCore . App . Analyzers . Infrastructure ;
@@ -86,9 +88,8 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
86
88
handler.Method);
87
89
}
88
90
89
- {{ endpoint . EmitRequestHandler ( ) }}
90
- {{ endpoint . EmitFilteredRequestHandler ( ) }}
91
-
91
+ {{ endpoint . EmitRequestHandler ( baseIndent : 5 ) }}
92
+ {{ endpoint . EmitFilteredRequestHandler ( baseIndent : 5 ) }}
92
93
RequestDelegate targetDelegate = filteredInvocation is null ? RequestHandler : RequestHandlerFiltered;
93
94
var metadata = inferredMetadataResult?.EndpointMetadata ?? ReadOnlyCollection<object>.Empty;
94
95
return new RequestDelegateResult(targetDelegate, metadata);
@@ -101,28 +102,32 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
101
102
{
102
103
var dedupedByDelegate = endpoints . Distinct ( EndpointDelegateComparer . Instance ) ;
103
104
var code = new StringBuilder ( ) ;
105
+ using var stringWriter = new StringWriter ( CultureInfo . InvariantCulture ) ;
106
+ using var codeWriter = new CodeWriter ( stringWriter , baseIndent : 2 ) ;
104
107
foreach ( var endpoint in dedupedByDelegate )
105
108
{
106
- code . AppendLine ( $$ """
107
- internal static global::Microsoft.AspNetCore.Builder.RouteHandlerBuilder {{ endpoint . HttpMethod }} (
108
- this global::Microsoft.AspNetCore.Routing.IEndpointRouteBuilder endpoints,
109
- [global::System.Diagnostics.CodeAnalysis.StringSyntax("Route")] string pattern,
110
- global::{{ endpoint . EmitHandlerDelegateType ( ) }} handler,
111
- [global::System.Runtime.CompilerServices.CallerFilePath] string filePath = "",
112
- [global::System.Runtime.CompilerServices.CallerLineNumber]int lineNumber = 0)
113
- {
114
- return global::Microsoft.AspNetCore.Http.Generated.GeneratedRouteBuilderExtensionsCore.MapCore(
115
- endpoints,
116
- pattern,
117
- handler,
118
- {{ endpoint . EmitVerb ( ) }} ,
119
- filePath,
120
- lineNumber);
121
- }
122
- """ ) ;
123
- }
124
-
125
- return code . ToString ( ) ;
109
+ codeWriter . WriteLine ( $ "internal static global::Microsoft.AspNetCore.Builder.RouteHandlerBuilder { endpoint . HttpMethod } (") ;
110
+ codeWriter . Indent ++ ;
111
+ codeWriter . WriteLine ( "this global::Microsoft.AspNetCore.Routing.IEndpointRouteBuilder endpoints," ) ;
112
+ codeWriter . WriteLine ( @"[global::System.Diagnostics.CodeAnalysis.StringSyntax(""Route"")] string pattern," ) ;
113
+ codeWriter . WriteLine ( $ "global::{ endpoint . EmitHandlerDelegateType ( ) } handler,") ;
114
+ codeWriter . WriteLine ( @"[global::System.Runtime.CompilerServices.CallerFilePath] string filePath = """"," ) ;
115
+ codeWriter . WriteLine ( "[global::System.Runtime.CompilerServices.CallerLineNumber]int lineNumber = 0)" ) ;
116
+ codeWriter . Indent -- ;
117
+ codeWriter . StartBlock ( ) ;
118
+ codeWriter . WriteLine ( "return global::Microsoft.AspNetCore.Http.Generated.GeneratedRouteBuilderExtensionsCore.MapCore(" ) ;
119
+ codeWriter . Indent ++ ;
120
+ codeWriter . WriteLine ( "endpoints," ) ;
121
+ codeWriter . WriteLine ( "pattern," ) ;
122
+ codeWriter . WriteLine ( "handler," ) ;
123
+ codeWriter . WriteLine ( $ "{ endpoint . EmitVerb ( ) } ,") ;
124
+ codeWriter . WriteLine ( "filePath," ) ;
125
+ codeWriter . WriteLine ( "lineNumber);" ) ;
126
+ codeWriter . Indent -- ;
127
+ codeWriter . EndBlock ( ) ;
128
+ }
129
+
130
+ return stringWriter . ToString ( ) ;
126
131
} ) ;
127
132
128
133
var thunksAndEndpoints = thunks . Collect ( ) . Combine ( stronglyTypedEndpointDefinitions ) ;
0 commit comments