Skip to content
This repository was archived by the owner on Dec 19, 2018. It is now read-only.

Commit c431d3f

Browse files
author
N. Taylor Mullen
committed
Refactored CodeGeneratorContext to CodeBuilderContext.
- Needed to separate the context's of "generation" and "building" to enable the communication of the TagHelperProvider.
1 parent 79f8610 commit c431d3f

23 files changed

+146
-68
lines changed

src/Microsoft.AspNet.Razor/CSharpRazorCodeLanguage.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public override RazorCodeGenerator CreateCodeGenerator(string className, string
4242
return new CSharpRazorCodeGenerator(className, rootNamespaceName, sourceFileName, host);
4343
}
4444

45-
public override CodeBuilder CreateCodeBuilder(CodeGeneratorContext context)
45+
public override CodeBuilder CreateCodeBuilder(CodeBuilderContext context)
4646
{
4747
return new CSharpCodeBuilder(context);
4848
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
namespace Microsoft.AspNet.Razor.Generator
5+
{
6+
/// <summary>
7+
/// Context object with information used to generate a Razor page.
8+
/// </summary>
9+
public class CodeBuilderContext : CodeGeneratorContext
10+
{
11+
/// <summary>
12+
/// Instantiates a new instance of the <see cref="CodeBuilderContext"/> object.
13+
/// </summary>
14+
/// <param name="generatorContext">A <see cref="CodeGeneratorContext"/> to copy information from.</param>
15+
public CodeBuilderContext(CodeGeneratorContext generatorContext)
16+
: base(generatorContext)
17+
{
18+
ExpressionRenderingMode = ExpressionRenderingMode.WriteToOutput;
19+
}
20+
21+
// Internal for testing.
22+
internal CodeBuilderContext(RazorEngineHost host,
23+
string className,
24+
string rootNamespace,
25+
string sourceFile,
26+
bool shouldGenerateLinePragmas)
27+
: base(host, className, rootNamespace, sourceFile, shouldGenerateLinePragmas)
28+
{
29+
ExpressionRenderingMode = ExpressionRenderingMode.WriteToOutput;
30+
}
31+
32+
/// <summary>
33+
/// The current C# rendering mode.
34+
/// </summary>
35+
/// <remarks>
36+
/// <see cref="ExpressionRenderingMode.WriteToOutput"/> forces C# generation to write
37+
/// <see cref="Compiler.Chunk"/>s to the output page, i.e. WriteLiteral("Hello World").
38+
/// <see cref="ExpressionRenderingMode.InjectCode"/> writes <see cref="Compiler.Chunk"/> values in their
39+
/// rawest form, i.g. "Hello World".
40+
/// </remarks>
41+
public ExpressionRenderingMode ExpressionRenderingMode { get; set; }
42+
43+
/// <summary>
44+
/// The C# writer to write <see cref="Compiler.Chunk"/> information to.
45+
/// </summary>
46+
/// <remarks>
47+
/// If <see cref="TargetWriterName"/> is <c>null</c> values will be written using a default write method
48+
/// i.e. WriteLiteral("Hello World").
49+
/// If <see cref="TargetWriterName"/> is not <c>null</c> values will be written to the given
50+
/// <see cref="TargetWriterName"/>, i.e. WriteLiteralTo(myWriter, "Hello World").
51+
/// </remarks>
52+
public string TargetWriterName { get; set; }
53+
}
54+
}

src/Microsoft.AspNet.Razor/Generator/CodeGeneratorContext.cs

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,38 @@ namespace Microsoft.AspNet.Razor.Generator
77
{
88
public class CodeGeneratorContext
99
{
10-
private CodeGeneratorContext()
10+
protected CodeGeneratorContext(CodeGeneratorContext context)
11+
: this(context.Host,
12+
context.ClassName,
13+
context.RootNamespace,
14+
context.SourceFile,
15+
// True because we're pulling from the provided context's source file.
16+
shouldGenerateLinePragmas: true)
1117
{
12-
ExpressionRenderingMode = ExpressionRenderingMode.WriteToOutput;
18+
CodeTreeBuilder = context.CodeTreeBuilder;
19+
}
20+
21+
public CodeGeneratorContext(RazorEngineHost host,
22+
string className,
23+
string rootNamespace,
24+
string sourceFile,
25+
bool shouldGenerateLinePragmas)
26+
{
27+
CodeTreeBuilder = new CodeTreeBuilder();
28+
Host = host;
29+
SourceFile = shouldGenerateLinePragmas ? sourceFile : null;
30+
RootNamespace = rootNamespace;
31+
ClassName = className;
1332
}
1433

15-
// Internal/Private state. Technically consumers might want to use some of these but they can implement them independently if necessary.
16-
// It's way safer to make them internal for now, especially with the code generator stuff in a bit of flux.
17-
internal ExpressionRenderingMode ExpressionRenderingMode { get; set; }
1834
public string SourceFile { get; internal set; }
35+
1936
public string RootNamespace { get; private set; }
37+
2038
public string ClassName { get; private set; }
39+
2140
public RazorEngineHost Host { get; private set; }
22-
public string TargetWriterName { get; set; }
2341

2442
public CodeTreeBuilder CodeTreeBuilder { get; set; }
25-
26-
public static CodeGeneratorContext Create(RazorEngineHost host, string className, string rootNamespace, string sourceFile, bool shouldGenerateLinePragmas)
27-
{
28-
return new CodeGeneratorContext()
29-
{
30-
CodeTreeBuilder = new CodeTreeBuilder(),
31-
Host = host,
32-
SourceFile = shouldGenerateLinePragmas ? sourceFile : null,
33-
RootNamespace = rootNamespace,
34-
ClassName = className
35-
};
36-
}
3743
}
3844
}

src/Microsoft.AspNet.Razor/Generator/Compiler/CodeBuilder/CSharp/CSharpCodeBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class CSharpCodeBuilder : CodeBuilder
1212
{
1313
private const int DisableAsyncWarning = 1998;
1414

15-
public CSharpCodeBuilder(CodeGeneratorContext context)
15+
public CSharpCodeBuilder(CodeBuilderContext context)
1616
: base(context)
1717
{
1818
}

src/Microsoft.AspNet.Razor/Generator/Compiler/CodeBuilder/CSharp/Visitors/CSharpBaseTypeVisitor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace Microsoft.AspNet.Razor.Generator.Compiler.CSharp
55
{
66
public class CSharpBaseTypeVisitor : CodeVisitor<CSharpCodeWriter>
77
{
8-
public CSharpBaseTypeVisitor(CSharpCodeWriter writer, CodeGeneratorContext context)
8+
public CSharpBaseTypeVisitor(CSharpCodeWriter writer, CodeBuilderContext context)
99
: base(writer, context) { }
1010

1111
public string CurrentBaseType { get; set; }

src/Microsoft.AspNet.Razor/Generator/Compiler/CodeBuilder/CSharp/Visitors/CSharpClassAttributeVisitor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace Microsoft.AspNet.Razor.Generator.Compiler.CSharp
77
{
88
public class CSharpClassAttributeVisitor : CodeVisitor<CSharpCodeWriter>
99
{
10-
public CSharpClassAttributeVisitor(CSharpCodeWriter writer, CodeGeneratorContext context)
10+
public CSharpClassAttributeVisitor(CSharpCodeWriter writer, CodeBuilderContext context)
1111
: base(writer, context) { }
1212

1313
protected override void Visit(SessionStateChunk chunk)

src/Microsoft.AspNet.Razor/Generator/Compiler/CodeBuilder/CSharp/Visitors/CSharpCodeVisitor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class CSharpCodeVisitor : CodeVisitor<CSharpCodeWriter>
1717

1818
private CSharpPaddingBuilder _paddingBuilder;
1919

20-
public CSharpCodeVisitor(CSharpCodeWriter writer, CodeGeneratorContext context)
20+
public CSharpCodeVisitor(CSharpCodeWriter writer, CodeBuilderContext context)
2121
: base(writer, context)
2222
{
2323
_paddingBuilder = new CSharpPaddingBuilder(context.Host);

src/Microsoft.AspNet.Razor/Generator/Compiler/CodeBuilder/CSharp/Visitors/CSharpDesignTimeHelpersVisitor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public class CSharpDesignTimeHelpersVisitor : CodeVisitor<CSharpCodeWriter>
1010

1111
private const int DisableVariableNamingWarnings = 219;
1212

13-
public CSharpDesignTimeHelpersVisitor(CSharpCodeWriter writer, CodeGeneratorContext context)
13+
public CSharpDesignTimeHelpersVisitor(CSharpCodeWriter writer, CodeBuilderContext context)
1414
: base(writer, context) { }
1515

1616
public void AcceptTree(CodeTree tree)

src/Microsoft.AspNet.Razor/Generator/Compiler/CodeBuilder/CSharp/Visitors/CSharpHelperVisitor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class CSharpHelperVisitor : CodeVisitor<CSharpCodeWriter>
1111

1212
private CSharpCodeVisitor _codeVisitor;
1313

14-
public CSharpHelperVisitor(CSharpCodeWriter writer, CodeGeneratorContext context)
14+
public CSharpHelperVisitor(CSharpCodeWriter writer, CodeBuilderContext context)
1515
: base(writer, context)
1616
{
1717
_codeVisitor = new CSharpCodeVisitor(writer, context);

src/Microsoft.AspNet.Razor/Generator/Compiler/CodeBuilder/CSharp/Visitors/CSharpTypeMemberVisitor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public class CSharpTypeMemberVisitor : CodeVisitor<CSharpCodeWriter>
99
{
1010
private CSharpCodeVisitor _csharpCodeVisitor;
1111

12-
public CSharpTypeMemberVisitor(CSharpCodeWriter writer, CodeGeneratorContext context)
12+
public CSharpTypeMemberVisitor(CSharpCodeWriter writer, CodeBuilderContext context)
1313
: base(writer, context)
1414
{
1515
_csharpCodeVisitor = new CSharpCodeVisitor(writer, context);

0 commit comments

Comments
 (0)