Skip to content

Commit 1b1a705

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

File tree

3 files changed

+41
-30
lines changed

3 files changed

+41
-30
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
using static Microsoft.AspNetCore.Razor.Language.CodeGeneration.CodeWriterExtensions;
6+
7+
namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration;
8+
9+
internal static class CodeRenderingContextExtensions
10+
{
11+
public static CSharpCodeWritingScope BuildNamespace(this CodeRenderingContext context, string? name, SourceSpan? span)
12+
{
13+
var writer = context.CodeWriter;
14+
15+
if (name.IsNullOrEmpty())
16+
{
17+
return new CSharpCodeWritingScope(writer, writeBraces: false);
18+
}
19+
20+
writer.Write("namespace ");
21+
22+
if (context.Options.DesignTime || span is null)
23+
{
24+
writer.WriteLine(name);
25+
}
26+
else
27+
{
28+
writer.WriteLine();
29+
using (writer.BuildEnhancedLinePragma(span, context))
30+
{
31+
writer.WriteLine(name);
32+
}
33+
}
34+
return new CSharpCodeWritingScope(writer);
35+
}
36+
}

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

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

477-
#nullable enable
478-
public static CSharpCodeWritingScope BuildNamespace(this CodeWriter writer, string? name, SourceSpan? span, CodeRenderingContext context)
479-
{
480-
if (name.IsNullOrEmpty())
481-
{
482-
return new CSharpCodeWritingScope(writer, writeBraces: false);
483-
}
484-
485-
writer.Write("namespace ");
486-
if (context.Options.DesignTime || span is null)
487-
{
488-
writer.WriteLine(name);
489-
}
490-
else
491-
{
492-
writer.WriteLine();
493-
using (writer.BuildEnhancedLinePragma(span, context))
494-
{
495-
writer.WriteLine(name);
496-
}
497-
}
498-
return new CSharpCodeWritingScope(writer);
499-
}
500-
#nullable disable
501-
502477
public static CSharpCodeWritingScope BuildClassDeclaration(
503478
this CodeWriter writer,
504479
ImmutableArray<string> modifiers,

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,21 +109,21 @@ public override void VisitUsingDirective(UsingDirectiveIntermediateNode node)
109109

110110
public override void VisitNamespaceDeclaration(NamespaceDeclarationIntermediateNode node)
111111
{
112-
var codeWriter = CodeWriter;
113-
114-
using (codeWriter.BuildNamespace(node.Content, node.Source, _context))
112+
using (_context.BuildNamespace(node.Content, node.Source))
115113
{
114+
var writer = CodeWriter;
115+
116116
if (node.Children.OfType<UsingDirectiveIntermediateNode>().Any())
117117
{
118118
// Tooling needs at least one line directive before using directives, otherwise Roslyn will
119119
// not offer to create a new one. The last using in the group will output a hidden line
120120
// directive after itself.
121-
codeWriter.WriteLine("#line default");
121+
writer.WriteLine("#line default");
122122
}
123123
else
124124
{
125125
// If there are no using directives, we output the hidden directive here.
126-
codeWriter.WriteLine("#line hidden");
126+
writer.WriteLine("#line hidden");
127127
}
128128

129129
VisitDefault(node);

0 commit comments

Comments
 (0)