Skip to content

Commit 3265dc6

Browse files
authored
Add support for indented text writer to RDG (#46845)
* Add support for indented text writer to RDG * Fix up baselines
1 parent efb237b commit 3265dc6

23 files changed

+557
-539
lines changed

src/Http/Http.Extensions/gen/Microsoft.AspNetCore.Http.Generators.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
<Compile Include="$(SharedSourceRoot)RoslynUtils\Parsability.cs" LinkBase="Shared"/>
2929
<Compile Include="$(SharedSourceRoot)RoslynUtils\ParsabilityMethod.cs" LinkBase="Shared"/>
3030
<Compile Include="$(SharedSourceRoot)RoslynUtils\ParsabilityHelper.cs" LinkBase="Shared"/>
31+
<Compile Include="$(SharedSourceRoot)RoslynUtils\CodeWriter.cs" LinkBase="Shared" />
3132
</ItemGroup>
3233

3334
</Project>

src/Http/Http.Extensions/gen/RequestDelegateGenerator.cs

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System;
5+
using System.Globalization;
6+
using System.IO;
57
using System.Linq;
68
using System.Text;
79
using Microsoft.AspNetCore.App.Analyzers.Infrastructure;
@@ -86,9 +88,8 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
8688
handler.Method);
8789
}
8890
89-
{{endpoint.EmitRequestHandler()}}
90-
{{endpoint.EmitFilteredRequestHandler()}}
91-
91+
{{endpoint.EmitRequestHandler(baseIndent: 5)}}
92+
{{endpoint.EmitFilteredRequestHandler(baseIndent: 5)}}
9293
RequestDelegate targetDelegate = filteredInvocation is null ? RequestHandler : RequestHandlerFiltered;
9394
var metadata = inferredMetadataResult?.EndpointMetadata ?? ReadOnlyCollection<object>.Empty;
9495
return new RequestDelegateResult(targetDelegate, metadata);
@@ -101,28 +102,32 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
101102
{
102103
var dedupedByDelegate = endpoints.Distinct(EndpointDelegateComparer.Instance);
103104
var code = new StringBuilder();
105+
using var stringWriter = new StringWriter(CultureInfo.InvariantCulture);
106+
using var codeWriter = new CodeWriter(stringWriter, baseIndent: 2);
104107
foreach (var endpoint in dedupedByDelegate)
105108
{
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();
126131
});
127132

128133
var thunksAndEndpoints = thunks.Collect().Combine(stronglyTypedEndpointDefinitions);

src/Http/Http.Extensions/gen/RequestDelegateGeneratorSources.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1570,7 +1570,7 @@ internal static class GenerateRouteBuilderEndpoints
15701570
private static readonly string[] DeleteVerb = new[] { global::Microsoft.AspNetCore.Http.HttpMethods.Delete };
15711571
private static readonly string[] PatchVerb = new[] { global::Microsoft.AspNetCore.Http.HttpMethods.Patch };
15721572
1573-
{{endpoints}}
1573+
{{endpoints}}
15741574
}
15751575
""" : string.Empty;
15761576
}

src/Http/Http.Extensions/gen/StaticRouteHandlerModel/Emitters/EndpointEmitter.cs

Lines changed: 26 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,54 +3,44 @@
33

44
using System;
55
using System.Linq;
6+
using System.Globalization;
7+
using System.IO;
68
using System.Text;
79

810
namespace Microsoft.AspNetCore.Http.Generators.StaticRouteHandlerModel.Emitters;
911
internal static class EndpointEmitter
1012
{
11-
internal static string EmitParameterPreparation(this Endpoint endpoint)
13+
internal static string EmitParameterPreparation(this Endpoint endpoint, int baseIndent = 0)
1214
{
13-
var parameterPreparationBuilder = new StringBuilder();
15+
using var stringWriter = new StringWriter(CultureInfo.InvariantCulture);
16+
using var parameterPreparationBuilder = new CodeWriter(stringWriter, baseIndent);
1417

15-
for (var parameterIndex = 0; parameterIndex < endpoint.Parameters.Length; parameterIndex++)
18+
foreach (var parameter in endpoint.Parameters)
1619
{
17-
var parameter = endpoint.Parameters[parameterIndex];
18-
19-
var parameterPreparationCode = parameter switch
20-
{
21-
{
22-
Source: EndpointParameterSource.SpecialType
23-
} => parameter.EmitSpecialParameterPreparation(),
24-
{
25-
Source: EndpointParameterSource.Query or EndpointParameterSource.Header,
26-
} => parameter.EmitQueryOrHeaderParameterPreparation(),
27-
{
28-
Source: EndpointParameterSource.Route,
29-
} => parameter.EmitRouteParameterPreparation(),
30-
{
31-
Source: EndpointParameterSource.RouteOrQuery
32-
} => parameter.EmitRouteOrQueryParameterPreparation(),
33-
{
34-
Source: EndpointParameterSource.JsonBody
35-
} => parameter.EmitJsonBodyParameterPreparationString(),
36-
{
37-
Source: EndpointParameterSource.Service
38-
} => parameter.EmitServiceParameterPreparation(),
39-
_ => throw new Exception("Unreachable!")
40-
};
41-
42-
// To avoid having two newlines after the block of parameter handling code.
43-
if (parameterIndex < endpoint.Parameters.Length - 1)
44-
{
45-
parameterPreparationBuilder.AppendLine(parameterPreparationCode);
46-
}
47-
else
20+
switch (parameter)
4821
{
49-
parameterPreparationBuilder.Append(parameterPreparationCode);
22+
case { Source: EndpointParameterSource.SpecialType }:
23+
parameter.EmitSpecialParameterPreparation(parameterPreparationBuilder);
24+
break;
25+
case { Source: EndpointParameterSource.Query or EndpointParameterSource.Header }:
26+
parameter.EmitQueryOrHeaderParameterPreparation(parameterPreparationBuilder);
27+
break;
28+
case { Source: EndpointParameterSource.Route }:
29+
parameter.EmitRouteParameterPreparation(parameterPreparationBuilder);
30+
break;
31+
case { Source: EndpointParameterSource.RouteOrQuery }:
32+
parameter.EmitRouteOrQueryParameterPreparation(parameterPreparationBuilder);
33+
break;
34+
case { Source: EndpointParameterSource.JsonBody }:
35+
parameter.EmitJsonBodyParameterPreparationString(parameterPreparationBuilder);
36+
break;
37+
case { Source: EndpointParameterSource.Service }:
38+
parameter.EmitServiceParameterPreparation(parameterPreparationBuilder);
39+
break;
5040
}
5141
}
5242

53-
return parameterPreparationBuilder.ToString();
43+
return stringWriter.ToString();
5444
}
5545

5646
public static string EmitArgumentList(this Endpoint endpoint) => string.Join(", ", endpoint.Parameters.Select(p => p.EmitArgument()));

0 commit comments

Comments
 (0)