Skip to content

Commit fdcf0c9

Browse files
Change BuildClassDeclaration extension to target CodeRenderingContext
Because `CodeRenderingContext` owns the `CodeWriter` instance used for code gen, and the `BuildClassDeclaration` extension method targets `CodeWriter` and also takes a `CodeRenderingContext`, we can change it can target `CodeRenderingContext`.
1 parent 1b1a705 commit fdcf0c9

File tree

8 files changed

+201
-183
lines changed

8 files changed

+201
-183
lines changed

src/Compiler/Microsoft.AspNetCore.Razor.Language/test/CodeGeneration/CSharpCodeWriterTest.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -398,10 +398,11 @@ public void CSharpCodeWriter_RespectTabSetting()
398398
.WithIndentSize(4)
399399
.WithFlags(indentWithTabs: true);
400400

401-
using var writer = new CodeWriter(options);
401+
using var context = TestCodeRenderingContext.Create(options);
402+
var writer = context.CodeWriter;
402403

403404
// Act
404-
writer.BuildClassDeclaration(modifiers: [], "C", null, interfaces: [], typeParameters: [], context: null);
405+
context.BuildClassDeclaration(modifiers: [], "C", null, interfaces: [], typeParameters: []);
405406
writer.WriteField(Array.Empty<string>(), Array.Empty<string>(), "int", "f");
406407

407408
// Assert
@@ -422,10 +423,11 @@ public void CSharpCodeWriter_RespectSpaceSetting()
422423
.WithIndentSize(4)
423424
.WithFlags(indentWithTabs: false);
424425

425-
using var writer = new CodeWriter(options);
426+
using var context = TestCodeRenderingContext.Create(options);
427+
var writer = context.CodeWriter;
426428

427429
// Act
428-
writer.BuildClassDeclaration(modifiers: [], "C", null, interfaces: [], typeParameters: [], context: null);
430+
context.BuildClassDeclaration(modifiers: [], "C", null, interfaces: [], typeParameters: []);
429431
writer.WriteField(Array.Empty<string>(), Array.Empty<string>(), "int", "f");
430432

431433
// Assert

src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/CodeGeneration/CodeRenderingContextExtensions.cs

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

44
using System;
5+
using System.Collections.Immutable;
6+
using System.Diagnostics;
7+
using Microsoft.AspNetCore.Razor.Language.Intermediate;
58
using static Microsoft.AspNetCore.Razor.Language.CodeGeneration.CodeWriterExtensions;
69

710
namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration;
@@ -33,4 +36,162 @@ public static CSharpCodeWritingScope BuildNamespace(this CodeRenderingContext co
3336
}
3437
return new CSharpCodeWritingScope(writer);
3538
}
39+
40+
public static CSharpCodeWritingScope BuildClassDeclaration(
41+
this CodeRenderingContext context,
42+
ImmutableArray<string> modifiers,
43+
string name,
44+
BaseTypeWithModel? baseType,
45+
ImmutableArray<IntermediateToken> interfaces,
46+
ImmutableArray<TypeParameter> typeParameters,
47+
bool useNullableContext = false)
48+
{
49+
var writer = context.CodeWriter;
50+
51+
if (useNullableContext)
52+
{
53+
writer.WriteLine("#nullable restore");
54+
}
55+
56+
foreach (var modifier in modifiers)
57+
{
58+
writer.Write(modifier);
59+
writer.Write(" ");
60+
}
61+
62+
writer.Write("class ");
63+
writer.Write(name);
64+
65+
if (!typeParameters.IsDefaultOrEmpty)
66+
{
67+
writer.Write("<");
68+
69+
var first = true;
70+
71+
foreach (var typeParameter in typeParameters)
72+
{
73+
if (first)
74+
{
75+
first = false;
76+
}
77+
else
78+
{
79+
writer.Write(",");
80+
}
81+
82+
if (typeParameter.ParameterNameSource is { } source)
83+
{
84+
WriteWithPragma(writer, typeParameter.ParameterName, context, source);
85+
}
86+
else
87+
{
88+
writer.Write((string)typeParameter.ParameterName);
89+
}
90+
}
91+
92+
writer.Write(">");
93+
}
94+
95+
var hasBaseType = !string.IsNullOrWhiteSpace(baseType?.BaseType.Content);
96+
var hasInterfaces = !interfaces.IsDefaultOrEmpty;
97+
98+
if (hasBaseType || hasInterfaces)
99+
{
100+
writer.Write(" : ");
101+
102+
if (hasBaseType)
103+
{
104+
Debug.Assert(baseType != null);
105+
106+
WriteToken(baseType.BaseType);
107+
WriteOptionalToken(baseType.GreaterThan);
108+
WriteOptionalToken(baseType.ModelType);
109+
WriteOptionalToken(baseType.LessThan);
110+
111+
if (hasInterfaces)
112+
{
113+
writer.WriteParameterSeparator();
114+
}
115+
}
116+
117+
if (hasInterfaces)
118+
{
119+
WriteToken(interfaces[0]);
120+
121+
for (var i = 1; i < interfaces.Length; i++)
122+
{
123+
writer.Write(", ");
124+
WriteToken(interfaces[i]);
125+
}
126+
}
127+
}
128+
129+
writer.WriteLine();
130+
if (typeParameters != null)
131+
{
132+
foreach (var typeParameter in typeParameters)
133+
{
134+
var constraint = typeParameter.Constraints;
135+
if (constraint != null)
136+
{
137+
if (typeParameter.ConstraintsSource is { } source)
138+
{
139+
Debug.Assert(context != null);
140+
WriteWithPragma(writer, constraint, context, source);
141+
}
142+
else
143+
{
144+
writer.Write(constraint);
145+
writer.WriteLine();
146+
}
147+
}
148+
}
149+
}
150+
151+
if (useNullableContext)
152+
{
153+
writer.WriteLine("#nullable disable");
154+
}
155+
156+
return new CSharpCodeWritingScope(writer);
157+
158+
void WriteOptionalToken(IntermediateToken? token)
159+
{
160+
if (token is not null)
161+
{
162+
WriteToken(token);
163+
}
164+
}
165+
166+
void WriteToken(IntermediateToken token)
167+
{
168+
if (token.Source is { } source)
169+
{
170+
WriteWithPragma(writer, token.Content, context, source);
171+
}
172+
else
173+
{
174+
writer.Write(token.Content);
175+
}
176+
}
177+
178+
static void WriteWithPragma(CodeWriter writer, string content, CodeRenderingContext context, SourceSpan source)
179+
{
180+
if (context.Options.DesignTime)
181+
{
182+
using (writer.BuildLinePragma(source, context))
183+
{
184+
context.AddSourceMappingFor(source);
185+
writer.Write(content);
186+
}
187+
}
188+
else
189+
{
190+
using (writer.BuildEnhancedLinePragma(source, context))
191+
{
192+
writer.Write(content);
193+
}
194+
}
195+
}
196+
}
36197
}

src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/CodeGeneration/CodeWriterExtensions.cs

Lines changed: 0 additions & 157 deletions
Original file line numberDiff line numberDiff line change
@@ -474,163 +474,6 @@ private static CSharpCodeWritingScope BuildLambda(CodeWriter writer, bool async,
474474
return scope;
475475
}
476476

477-
public static CSharpCodeWritingScope BuildClassDeclaration(
478-
this CodeWriter writer,
479-
ImmutableArray<string> modifiers,
480-
string name,
481-
BaseTypeWithModel baseType,
482-
ImmutableArray<IntermediateToken> interfaces,
483-
ImmutableArray<TypeParameter> typeParameters,
484-
CodeRenderingContext context,
485-
bool useNullableContext = false)
486-
{
487-
Debug.Assert(context == null || context.CodeWriter == writer);
488-
489-
if (useNullableContext)
490-
{
491-
writer.WriteLine("#nullable restore");
492-
}
493-
494-
foreach (var modifier in modifiers)
495-
{
496-
writer.Write(modifier);
497-
writer.Write(" ");
498-
}
499-
500-
writer.Write("class ");
501-
writer.Write(name);
502-
503-
if (!typeParameters.IsDefaultOrEmpty)
504-
{
505-
writer.Write("<");
506-
507-
var first = true;
508-
509-
foreach (var typeParameter in typeParameters)
510-
{
511-
if (first)
512-
{
513-
first = false;
514-
}
515-
else
516-
{
517-
writer.Write(",");
518-
}
519-
520-
if (typeParameter.ParameterNameSource is { } source)
521-
{
522-
WriteWithPragma(writer, typeParameter.ParameterName, context, source);
523-
}
524-
else
525-
{
526-
writer.Write((string)typeParameter.ParameterName);
527-
}
528-
}
529-
530-
writer.Write(">");
531-
}
532-
533-
var hasBaseType = !string.IsNullOrWhiteSpace(baseType?.BaseType.Content);
534-
var hasInterfaces = !interfaces.IsDefaultOrEmpty;
535-
536-
if (hasBaseType || hasInterfaces)
537-
{
538-
writer.Write(" : ");
539-
540-
if (hasBaseType)
541-
{
542-
WriteToken(baseType.BaseType);
543-
WriteOptionalToken(baseType.GreaterThan);
544-
WriteOptionalToken(baseType.ModelType);
545-
WriteOptionalToken(baseType.LessThan);
546-
547-
if (hasInterfaces)
548-
{
549-
WriteParameterSeparator(writer);
550-
}
551-
}
552-
553-
if (hasInterfaces)
554-
{
555-
WriteToken(interfaces[0]);
556-
557-
for (var i = 1; i < interfaces.Length; i++)
558-
{
559-
writer.Write(", ");
560-
WriteToken(interfaces[i]);
561-
}
562-
}
563-
}
564-
565-
writer.WriteLine();
566-
if (typeParameters != null)
567-
{
568-
foreach (var typeParameter in typeParameters)
569-
{
570-
var constraint = typeParameter.Constraints;
571-
if (constraint != null)
572-
{
573-
if (typeParameter.ConstraintsSource is { } source)
574-
{
575-
Debug.Assert(context != null);
576-
WriteWithPragma(writer, constraint, context, source);
577-
}
578-
else
579-
{
580-
writer.Write(constraint);
581-
writer.WriteLine();
582-
}
583-
}
584-
}
585-
}
586-
587-
if (useNullableContext)
588-
{
589-
writer.WriteLine("#nullable disable");
590-
}
591-
592-
return new CSharpCodeWritingScope(writer);
593-
594-
void WriteOptionalToken(IntermediateToken token)
595-
{
596-
if (token is not null)
597-
{
598-
WriteToken(token);
599-
}
600-
}
601-
602-
void WriteToken(IntermediateToken token)
603-
{
604-
if (token.Source is { } source)
605-
{
606-
WriteWithPragma(writer, token.Content, context, source);
607-
}
608-
else
609-
{
610-
writer.Write(token.Content);
611-
}
612-
}
613-
614-
static void WriteWithPragma(CodeWriter writer, string content, CodeRenderingContext context, SourceSpan source)
615-
{
616-
if (context.Options.DesignTime)
617-
{
618-
using (writer.BuildLinePragma(source, context))
619-
{
620-
context.AddSourceMappingFor(source);
621-
writer.Write(content);
622-
}
623-
}
624-
else
625-
{
626-
using (writer.BuildEnhancedLinePragma(source, context))
627-
{
628-
writer.Write(content);
629-
}
630-
}
631-
}
632-
}
633-
634477
public static CSharpCodeWritingScope BuildMethodDeclaration(
635478
this CodeWriter writer,
636479
string accessibility,

src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/CodeGeneration/DefaultDocumentWriter.cs

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

44
using System;
5+
using System.Diagnostics;
56
using System.Linq;
67
using System.Security.Cryptography;
78
using Microsoft.AspNetCore.Razor.Language.Intermediate;
@@ -132,13 +133,14 @@ public override void VisitNamespaceDeclaration(NamespaceDeclarationIntermediateN
132133

133134
public override void VisitClassDeclaration(ClassDeclarationIntermediateNode node)
134135
{
135-
using (CodeWriter.BuildClassDeclaration(
136+
Debug.Assert(node.ClassName != null);
137+
138+
using (_context.BuildClassDeclaration(
136139
node.Modifiers,
137140
node.ClassName,
138141
node.BaseType,
139142
node.Interfaces,
140143
node.TypeParameters,
141-
_context,
142144
useNullableContext: !Options.SuppressNullabilityEnforcement && node.NullableContext))
143145
{
144146
VisitDefault(node);

0 commit comments

Comments
 (0)