Modify base Visitor class to be more generic.

This involved adding more boiler plate to the base of the visitor classes such as a Writer and a Context.  By following this contract for new visitors we'll ensure that visitors have enough information to add new features to Razor.
This commit is contained in:
N. Taylor Mullen 2014-02-04 14:56:00 -08:00
parent a9d4cd9089
commit bc3d32ec74
11 changed files with 124 additions and 154 deletions

View File

@ -29,7 +29,6 @@ namespace Microsoft.AspNet.Razor.Generator
ns = ns.Substring(1); ns = ns.Substring(1);
} }
// TODO: Verify namespace hasn't already been added.
codeTreeBuilder.AddUsingChunk(ns, target, context); codeTreeBuilder.AddUsingChunk(ns, target, context);
} }

View File

@ -25,11 +25,11 @@ namespace Microsoft.AspNet.Razor.Generator.Compiler.CSharp
// Separate the usings and the class // Separate the usings and the class
writer.WriteLine(); writer.WriteLine();
var baseTypeVisitor = new CSharpBaseTypeVisitor(writer); var baseTypeVisitor = new CSharpBaseTypeVisitor(writer, Context);
baseTypeVisitor.Accept(Tree.Chunks); baseTypeVisitor.Accept(Tree.Chunks);
string baseType = baseTypeVisitor.CurrentBaseType ?? Host.DefaultBaseClass; string baseType = baseTypeVisitor.CurrentBaseType ?? Host.DefaultBaseClass;
new CSharpClassAttributeVisitor(writer).Accept(Tree.Chunks); new CSharpClassAttributeVisitor(writer, Context).Accept(Tree.Chunks);
IEnumerable<string> baseTypes = String.IsNullOrEmpty(baseType) ? Enumerable.Empty<string>() : IEnumerable<string> baseTypes = String.IsNullOrEmpty(baseType) ? Enumerable.Empty<string>() :
new string[] { baseType }; new string[] { baseType };
@ -44,8 +44,6 @@ namespace Microsoft.AspNet.Razor.Generator.Compiler.CSharp
new CSharpTypeMemberVisitor(writer, Context).Accept(Tree.Chunks); new CSharpTypeMemberVisitor(writer, Context).Accept(Tree.Chunks);
new CSharpDesignTimeHelpersVisitor(writer, Context).AcceptTree(Tree); new CSharpDesignTimeHelpersVisitor(writer, Context).AcceptTree(Tree);
// TODO: resolve variable declarations
writer.WriteLineHiddenDirective(); writer.WriteLineHiddenDirective();
using (writer.BuildConstructor(Context.ClassName)) using (writer.BuildConstructor(Context.ClassName))
{ {
@ -68,7 +66,7 @@ namespace Microsoft.AspNet.Razor.Generator.Compiler.CSharp
private void AddImports(CodeTree codeTree, CSharpCodeWriter writer, IEnumerable<string> defaultImports) private void AddImports(CodeTree codeTree, CSharpCodeWriter writer, IEnumerable<string> defaultImports)
{ {
// Write out using directives // Write out using directives
var usingVisitor = new CSharpUsingVisitor(writer, Context.SourceFile); var usingVisitor = new CSharpUsingVisitor(writer, Context);
foreach (Chunk chunk in Tree.Chunks) foreach (Chunk chunk in Tree.Chunks)
{ {
usingVisitor.Accept(chunk); usingVisitor.Accept(chunk);

View File

@ -1,14 +1,10 @@
 
namespace Microsoft.AspNet.Razor.Generator.Compiler.CSharp namespace Microsoft.AspNet.Razor.Generator.Compiler.CSharp
{ {
public class CSharpBaseTypeVisitor : CodeVisitor public class CSharpBaseTypeVisitor : CodeVisitor<CSharpCodeWriter>
{ {
private CSharpCodeWriter _writer; public CSharpBaseTypeVisitor(CSharpCodeWriter writer, CodeGeneratorContext context)
: base(writer, context) { }
public CSharpBaseTypeVisitor(CSharpCodeWriter writer)
{
_writer = writer;
}
public string CurrentBaseType { get; set; } public string CurrentBaseType { get; set; }

View File

@ -2,24 +2,20 @@
namespace Microsoft.AspNet.Razor.Generator.Compiler.CSharp namespace Microsoft.AspNet.Razor.Generator.Compiler.CSharp
{ {
public class CSharpClassAttributeVisitor : CodeVisitor public class CSharpClassAttributeVisitor : CodeVisitor<CSharpCodeWriter>
{ {
private CSharpCodeWriter _writer; public CSharpClassAttributeVisitor(CSharpCodeWriter writer, CodeGeneratorContext context)
: base(writer, context) { }
public CSharpClassAttributeVisitor(CSharpCodeWriter writer)
{
_writer = writer;
}
protected override void Visit(SessionStateChunk chunk) protected override void Visit(SessionStateChunk chunk)
{ {
_writer.Write("[") Writer.Write("[")
.Write(typeof(RazorDirectiveAttribute).FullName) .Write(typeof(RazorDirectiveAttribute).FullName)
.Write("(") .Write("(")
.WriteStringLiteral(SyntaxConstants.CSharp.SessionStateKeyword) .WriteStringLiteral(SyntaxConstants.CSharp.SessionStateKeyword)
.WriteParameterSeparator() .WriteParameterSeparator()
.WriteStringLiteral(chunk.Value) .WriteStringLiteral(chunk.Value)
.WriteLine(")]"); .WriteLine(")]");
} }
} }
} }

View File

@ -4,24 +4,20 @@ using System.Linq;
namespace Microsoft.AspNet.Razor.Generator.Compiler.CSharp namespace Microsoft.AspNet.Razor.Generator.Compiler.CSharp
{ {
public class CSharpCodeVisitor : CodeVisitor public class CSharpCodeVisitor : CodeVisitor<CSharpCodeWriter>
{ {
private const string ValueWriterName = "__razor_attribute_value_writer"; private const string ValueWriterName = "__razor_attribute_value_writer";
private readonly CSharpCodeWriter _writer;
private readonly CodeGeneratorContext _context;
public CSharpCodeVisitor(CSharpCodeWriter writer, CodeGeneratorContext context) public CSharpCodeVisitor(CSharpCodeWriter writer, CodeGeneratorContext context)
{ : base(writer, context) { }
_writer = writer;
_context = context;
}
protected override void Visit(SetLayoutChunk chunk) protected override void Visit(SetLayoutChunk chunk)
{ {
if (!_context.Host.DesignTimeMode && !String.IsNullOrEmpty(_context.Host.GeneratedClassContext.LayoutPropertyName)) if (!Context.Host.DesignTimeMode && !String.IsNullOrEmpty(Context.Host.GeneratedClassContext.LayoutPropertyName))
{ {
_writer.Write(_context.Host.GeneratedClassContext.LayoutPropertyName) Writer.Write(Context.Host.GeneratedClassContext.LayoutPropertyName)
.Write(" = ") .Write(" = ")
.WriteStringLiteral(chunk.Layout) .WriteStringLiteral(chunk.Layout)
.WriteLine(";"); .WriteLine(";");
@ -30,76 +26,76 @@ namespace Microsoft.AspNet.Razor.Generator.Compiler.CSharp
protected override void Visit(TemplateChunk chunk) protected override void Visit(TemplateChunk chunk)
{ {
_writer.Write(TemplateBlockCodeGenerator.ItemParameterName).Write(" => ") Writer.Write(TemplateBlockCodeGenerator.ItemParameterName).Write(" => ")
.WriteStartNewObject(_context.Host.GeneratedClassContext.TemplateTypeName); .WriteStartNewObject(Context.Host.GeneratedClassContext.TemplateTypeName);
using (_writer.BuildLambda(endLine: false, parameterNames: TemplateBlockCodeGenerator.TemplateWriterName)) using (Writer.BuildLambda(endLine: false, parameterNames: TemplateBlockCodeGenerator.TemplateWriterName))
{ {
Visit((ChunkBlock)chunk); Visit((ChunkBlock)chunk);
} }
_writer.WriteEndMethodInvocation(false).WriteLine(); Writer.WriteEndMethodInvocation(false).WriteLine();
} }
protected override void Visit(ResolveUrlChunk chunk) protected override void Visit(ResolveUrlChunk chunk)
{ {
if (!_context.Host.DesignTimeMode && String.IsNullOrEmpty(chunk.Url)) if (!Context.Host.DesignTimeMode && String.IsNullOrEmpty(chunk.Url))
{ {
return; return;
} }
// TODO: Add instrumentation // TODO: Add instrumentation
if (!String.IsNullOrEmpty(chunk.Url) && !_context.Host.DesignTimeMode) if (!String.IsNullOrEmpty(chunk.Url) && !Context.Host.DesignTimeMode)
{ {
if (chunk.RenderingMode == ExpressionRenderingMode.WriteToOutput) if (chunk.RenderingMode == ExpressionRenderingMode.WriteToOutput)
{ {
if (!String.IsNullOrEmpty(chunk.WriterName)) if (!String.IsNullOrEmpty(chunk.WriterName))
{ {
_writer.WriteStartMethodInvocation(_context.Host.GeneratedClassContext.WriteLiteralToMethodName) Writer.WriteStartMethodInvocation(Context.Host.GeneratedClassContext.WriteLiteralToMethodName)
.Write(chunk.WriterName) .Write(chunk.WriterName)
.WriteParameterSeparator(); .WriteParameterSeparator();
} }
else else
{ {
_writer.WriteStartMethodInvocation(_context.Host.GeneratedClassContext.WriteLiteralMethodName); Writer.WriteStartMethodInvocation(Context.Host.GeneratedClassContext.WriteLiteralMethodName);
} }
} }
_writer.WriteStartMethodInvocation(_context.Host.GeneratedClassContext.ResolveUrlMethodName) Writer.WriteStartMethodInvocation(Context.Host.GeneratedClassContext.ResolveUrlMethodName)
.WriteStringLiteral(chunk.Url) .WriteStringLiteral(chunk.Url)
.WriteEndMethodInvocation(endLine: false); .WriteEndMethodInvocation(endLine: false);
if (chunk.RenderingMode == ExpressionRenderingMode.WriteToOutput) if (chunk.RenderingMode == ExpressionRenderingMode.WriteToOutput)
{ {
_writer.WriteEndMethodInvocation(); Writer.WriteEndMethodInvocation();
} }
} }
} }
protected override void Visit(LiteralChunk chunk) protected override void Visit(LiteralChunk chunk)
{ {
if (!_context.Host.DesignTimeMode && String.IsNullOrEmpty(chunk.Text)) if (!Context.Host.DesignTimeMode && String.IsNullOrEmpty(chunk.Text))
{ {
return; return;
} }
// TODO: Add instrumentation // TODO: Add instrumentation
if (!String.IsNullOrEmpty(chunk.Text) && !_context.Host.DesignTimeMode) if (!String.IsNullOrEmpty(chunk.Text) && !Context.Host.DesignTimeMode)
{ {
if (!String.IsNullOrEmpty(chunk.WriterName)) if (!String.IsNullOrEmpty(chunk.WriterName))
{ {
_writer.WriteStartMethodInvocation(_context.Host.GeneratedClassContext.WriteLiteralToMethodName) Writer.WriteStartMethodInvocation(Context.Host.GeneratedClassContext.WriteLiteralToMethodName)
.Write(chunk.WriterName) .Write(chunk.WriterName)
.WriteParameterSeparator(); .WriteParameterSeparator();
} }
else else
{ {
_writer.WriteStartMethodInvocation(_context.Host.GeneratedClassContext.WriteLiteralMethodName); Writer.WriteStartMethodInvocation(Context.Host.GeneratedClassContext.WriteLiteralMethodName);
} }
_writer.WriteStringLiteral(chunk.Text) Writer.WriteStringLiteral(chunk.Text)
.WriteEndMethodInvocation(); .WriteEndMethodInvocation();
} }
@ -111,88 +107,83 @@ namespace Microsoft.AspNet.Razor.Generator.Compiler.CSharp
// TODO: Handle instrumentation // TODO: Handle instrumentation
// TODO: Refactor // TODO: Refactor
if (!_context.Host.DesignTimeMode && chunk.RenderingMode == ExpressionRenderingMode.InjectCode) if (!Context.Host.DesignTimeMode && chunk.RenderingMode == ExpressionRenderingMode.InjectCode)
{ {
Visit((ChunkBlock)chunk); Visit((ChunkBlock)chunk);
} }
else else
{ {
if (_context.Host.DesignTimeMode) if (Context.Host.DesignTimeMode)
{ {
_writer.WriteStartAssignment("__o"); Writer.WriteStartAssignment("__o");
} }
else if (chunk.RenderingMode == ExpressionRenderingMode.WriteToOutput) else if (chunk.RenderingMode == ExpressionRenderingMode.WriteToOutput)
{ {
// TODO: Abstract padding out?
if (!String.IsNullOrEmpty(chunk.WriterName)) if (!String.IsNullOrEmpty(chunk.WriterName))
{ {
_writer.WriteStartMethodInvocation(_context.Host.GeneratedClassContext.WriteToMethodName) Writer.WriteStartMethodInvocation(Context.Host.GeneratedClassContext.WriteToMethodName)
.Write(chunk.WriterName) .Write(chunk.WriterName)
.WriteParameterSeparator(); .WriteParameterSeparator();
} }
else else
{ {
_writer.WriteStartMethodInvocation(_context.Host.GeneratedClassContext.WriteMethodName); Writer.WriteStartMethodInvocation(Context.Host.GeneratedClassContext.WriteMethodName);
} }
} }
Visit((ChunkBlock)chunk); Visit((ChunkBlock)chunk);
if (_context.Host.DesignTimeMode) if (Context.Host.DesignTimeMode)
{ {
_writer.WriteLine(";"); Writer.WriteLine(";");
} }
else if (chunk.RenderingMode == ExpressionRenderingMode.WriteToOutput) else if (chunk.RenderingMode == ExpressionRenderingMode.WriteToOutput)
{ {
_writer.WriteEndMethodInvocation(); Writer.WriteEndMethodInvocation();
} }
} }
} }
protected override void Visit(ExpressionChunk chunk) protected override void Visit(ExpressionChunk chunk)
{ {
using (_writer.BuildLineMapping(chunk.Start, chunk.Code.Value.Length, _context.SourceFile)) using (Writer.BuildLineMapping(chunk.Start, chunk.Code.Length, Context.SourceFile))
{ {
_writer.Indent(chunk.Start.CharacterIndex) Writer.Indent(chunk.Start.CharacterIndex)
.Write(chunk.Code.Value); .Write(chunk.Code);
} }
} }
protected override void Visit(StatementChunk chunk) protected override void Visit(StatementChunk chunk)
{ {
foreach (Snippet snippet in chunk.Code) using (Writer.BuildLineMapping(chunk.Start, chunk.Code.Length, Context.SourceFile))
{ {
using (_writer.BuildLineMapping(chunk.Start, snippet.Value.Length, _context.SourceFile)) Writer.Indent(chunk.Start.CharacterIndex);
{ Writer.WriteLine(chunk.Code);
_writer.Indent(chunk.Start.CharacterIndex);
_writer.WriteLine(snippet.Value);
}
} }
} }
protected override void Visit(DynamicCodeAttributeChunk chunk) protected override void Visit(DynamicCodeAttributeChunk chunk)
{ {
if (_context.Host.DesignTimeMode) if (Context.Host.DesignTimeMode)
{ {
return; // Don't generate anything! return; // Don't generate anything!
} }
Chunk code = chunk.Children.FirstOrDefault(); Chunk code = chunk.Children.FirstOrDefault();
_writer.WriteParameterSeparator() Writer.WriteParameterSeparator()
.WriteLine(); .WriteLine();
if (code is ExpressionChunk || code is ExpressionBlockChunk) if (code is ExpressionChunk || code is ExpressionBlockChunk)
{ {
_writer.WriteStartMethodInvocation("Tuple.Create") Writer.WriteStartMethodInvocation("Tuple.Create")
.WriteLocationTaggedString(chunk.Prefix) .WriteLocationTaggedString(chunk.Prefix)
.WriteParameterSeparator() .WriteParameterSeparator()
.WriteStartMethodInvocation("Tuple.Create", new string[] { "System.Object", "System.Int32" }); .WriteStartMethodInvocation("Tuple.Create", new string[] { "System.Object", "System.Int32" });
Accept(code); Accept(code);
_writer.WriteParameterSeparator() Writer.WriteParameterSeparator()
.Write(chunk.Start.AbsoluteIndex.ToString(CultureInfo.CurrentCulture)) .Write(chunk.Start.AbsoluteIndex.ToString(CultureInfo.CurrentCulture))
.WriteEndMethodInvocation(false) .WriteEndMethodInvocation(false)
.WriteParameterSeparator() .WriteParameterSeparator()
@ -201,18 +192,18 @@ namespace Microsoft.AspNet.Razor.Generator.Compiler.CSharp
} }
else else
{ {
_writer.WriteStartMethodInvocation("Tuple.Create") Writer.WriteStartMethodInvocation("Tuple.Create")
.WriteLocationTaggedString(chunk.Prefix) .WriteLocationTaggedString(chunk.Prefix)
.WriteParameterSeparator() .WriteParameterSeparator()
.WriteStartMethodInvocation("Tuple.Create", new string[] { "System.Object", "System.Int32" }) .WriteStartMethodInvocation("Tuple.Create", new string[] { "System.Object", "System.Int32" })
.WriteStartNewObject(_context.Host.GeneratedClassContext.TemplateTypeName); .WriteStartNewObject(Context.Host.GeneratedClassContext.TemplateTypeName);
using (_writer.BuildLambda(endLine: false, parameterNames: ValueWriterName)) using (Writer.BuildLambda(endLine: false, parameterNames: ValueWriterName))
{ {
Visit((ChunkBlock)chunk); Visit((ChunkBlock)chunk);
} }
_writer.WriteEndMethodInvocation(false) Writer.WriteEndMethodInvocation(false)
.WriteParameterSeparator() .WriteParameterSeparator()
.Write(chunk.Start.AbsoluteIndex.ToString(CultureInfo.CurrentCulture)) .Write(chunk.Start.AbsoluteIndex.ToString(CultureInfo.CurrentCulture))
.WriteEndMethodInvocation(endLine: false) .WriteEndMethodInvocation(endLine: false)
@ -224,23 +215,23 @@ namespace Microsoft.AspNet.Razor.Generator.Compiler.CSharp
protected override void Visit(LiteralCodeAttributeChunk chunk) protected override void Visit(LiteralCodeAttributeChunk chunk)
{ {
if (_context.Host.DesignTimeMode) if (Context.Host.DesignTimeMode)
{ {
return; // Don't generate anything! return; // Don't generate anything!
} }
_writer.WriteParameterSeparator() Writer.WriteParameterSeparator()
.WriteStartMethodInvocation("Tuple.Create") .WriteStartMethodInvocation("Tuple.Create")
.WriteLocationTaggedString(chunk.Prefix) .WriteLocationTaggedString(chunk.Prefix)
.WriteParameterSeparator(); .WriteParameterSeparator();
if (chunk.Children.Count > 0 || chunk.Value == null) if (chunk.Children.Count > 0 || chunk.Value == null)
{ {
_writer.WriteStartMethodInvocation("Tuple.Create", new string[] { "System.Object", "System.Int32" }); Writer.WriteStartMethodInvocation("Tuple.Create", new string[] { "System.Object", "System.Int32" });
Visit((ChunkBlock)chunk); Visit((ChunkBlock)chunk);
_writer.WriteParameterSeparator() Writer.WriteParameterSeparator()
.Write(chunk.ValueLocation.AbsoluteIndex.ToString(CultureInfo.CurrentCulture)) .Write(chunk.ValueLocation.AbsoluteIndex.ToString(CultureInfo.CurrentCulture))
.WriteEndMethodInvocation(false) .WriteEndMethodInvocation(false)
.WriteParameterSeparator() .WriteParameterSeparator()
@ -250,7 +241,7 @@ namespace Microsoft.AspNet.Razor.Generator.Compiler.CSharp
} }
else else
{ {
_writer.WriteLocationTaggedString(chunk.Value) Writer.WriteLocationTaggedString(chunk.Value)
.WriteParameterSeparator() .WriteParameterSeparator()
.WriteBooleanLiteral(true) .WriteBooleanLiteral(true)
.WriteEndMethodInvocation(false); .WriteEndMethodInvocation(false);
@ -259,23 +250,23 @@ namespace Microsoft.AspNet.Razor.Generator.Compiler.CSharp
protected override void Visit(CodeAttributeChunk chunk) protected override void Visit(CodeAttributeChunk chunk)
{ {
if (_context.Host.DesignTimeMode) if (Context.Host.DesignTimeMode)
{ {
return; // Don't generate anything! return; // Don't generate anything!
} }
if (!String.IsNullOrEmpty(chunk.WriterName)) if (!String.IsNullOrEmpty(chunk.WriterName))
{ {
_writer.WriteStartMethodInvocation(_context.Host.GeneratedClassContext.WriteAttributeToMethodName) Writer.WriteStartMethodInvocation(Context.Host.GeneratedClassContext.WriteAttributeToMethodName)
.Write(chunk.WriterName) .Write(chunk.WriterName)
.WriteParameterSeparator(); .WriteParameterSeparator();
} }
else else
{ {
_writer.WriteStartMethodInvocation(_context.Host.GeneratedClassContext.WriteAttributeMethodName); Writer.WriteStartMethodInvocation(Context.Host.GeneratedClassContext.WriteAttributeMethodName);
} }
_writer.WriteStringLiteral(chunk.Attribute) Writer.WriteStringLiteral(chunk.Attribute)
.WriteParameterSeparator() .WriteParameterSeparator()
.WriteLocationTaggedString(chunk.Prefix) .WriteLocationTaggedString(chunk.Prefix)
.WriteParameterSeparator() .WriteParameterSeparator()
@ -283,21 +274,21 @@ namespace Microsoft.AspNet.Razor.Generator.Compiler.CSharp
Visit((ChunkBlock)chunk); Visit((ChunkBlock)chunk);
_writer.WriteEndMethodInvocation(); Writer.WriteEndMethodInvocation();
} }
protected override void Visit(SectionChunk chunk) protected override void Visit(SectionChunk chunk)
{ {
_writer.WriteStartMethodInvocation(_context.Host.GeneratedClassContext.DefineSectionMethodName) Writer.WriteStartMethodInvocation(Context.Host.GeneratedClassContext.DefineSectionMethodName)
.WriteStringLiteral(chunk.Name) .WriteStringLiteral(chunk.Name)
.WriteParameterSeparator(); .WriteParameterSeparator();
using (_writer.BuildLambda(false)) using (Writer.BuildLambda(false))
{ {
Visit((ChunkBlock)chunk); Visit((ChunkBlock)chunk);
} }
_writer.WriteEndMethodInvocation(); Writer.WriteEndMethodInvocation();
} }
} }
} }

View File

@ -1,26 +1,20 @@
 
namespace Microsoft.AspNet.Razor.Generator.Compiler.CSharp namespace Microsoft.AspNet.Razor.Generator.Compiler.CSharp
{ {
public class CSharpDesignTimeHelpersVisitor : CodeVisitor public class CSharpDesignTimeHelpersVisitor : CodeVisitor<CSharpCodeWriter>
{ {
internal const string InheritsHelper = "__inheritsHelper"; internal const string InheritsHelper = "__inheritsHelper";
private readonly CSharpCodeWriter _writer;
private readonly CodeGeneratorContext _context;
public CSharpDesignTimeHelpersVisitor(CSharpCodeWriter writer, CodeGeneratorContext context) public CSharpDesignTimeHelpersVisitor(CSharpCodeWriter writer, CodeGeneratorContext context)
{ : base(writer, context) { }
_writer = writer;
_context = context;
}
public void AcceptTree(CodeTree tree) public void AcceptTree(CodeTree tree)
{ {
if (_context.Host.DesignTimeMode) if (Context.Host.DesignTimeMode)
{ {
using (_writer.BuildMethodDeclaration("private", "void", "@" + CodeGeneratorContext.DesignTimeHelperMethodName)) using (Writer.BuildMethodDeclaration("private", "void", "@" + CodeGeneratorContext.DesignTimeHelperMethodName))
{ {
using (_writer.BuildDisableWarningScope()) using (Writer.BuildDisableWarningScope())
{ {
Accept(tree.Chunks); Accept(tree.Chunks);
} }
@ -30,17 +24,17 @@ namespace Microsoft.AspNet.Razor.Generator.Compiler.CSharp
protected override void Visit(SetBaseTypeChunk chunk) protected override void Visit(SetBaseTypeChunk chunk)
{ {
if (_context.Host.DesignTimeMode) if (Context.Host.DesignTimeMode)
{ {
using (CSharpLineMappingWriter lineMappingWriter = _writer.BuildLineMapping(chunk.Start, chunk.TypeName.Length, _context.SourceFile)) using (CSharpLineMappingWriter lineMappingWriter = Writer.BuildLineMapping(chunk.Start, chunk.TypeName.Length, Context.SourceFile))
{ {
_writer.Indent(chunk.Start.CharacterIndex); Writer.Indent(chunk.Start.CharacterIndex);
lineMappingWriter.MarkLineMappingStart(); lineMappingWriter.MarkLineMappingStart();
_writer.Write(chunk.TypeName); Writer.Write(chunk.TypeName);
lineMappingWriter.MarkLineMappingEnd(); lineMappingWriter.MarkLineMappingEnd();
_writer.Write(" ").Write(InheritsHelper).Write(" = null;"); Writer.Write(" ").Write(InheritsHelper).Write(" = null;");
} }
} }
} }

View File

@ -2,18 +2,15 @@
namespace Microsoft.AspNet.Razor.Generator.Compiler.CSharp namespace Microsoft.AspNet.Razor.Generator.Compiler.CSharp
{ {
public class CSharpHelperVisitor : CodeVisitor public class CSharpHelperVisitor : CodeVisitor<CSharpCodeWriter>
{ {
private const string HelperWriterName = "__razor_helper_writer"; private const string HelperWriterName = "__razor_helper_writer";
private readonly CSharpCodeWriter _writer;
private readonly CodeGeneratorContext _context;
private CSharpCodeVisitor _codeVisitor; private CSharpCodeVisitor _codeVisitor;
public CSharpHelperVisitor(CSharpCodeWriter writer, CodeGeneratorContext context) public CSharpHelperVisitor(CSharpCodeWriter writer, CodeGeneratorContext context)
: base(writer, context)
{ {
_writer = writer;
_context = context;
_codeVisitor = new CSharpCodeVisitor(writer, context); _codeVisitor = new CSharpCodeVisitor(writer, context);
} }
@ -21,22 +18,22 @@ namespace Microsoft.AspNet.Razor.Generator.Compiler.CSharp
{ {
IDisposable lambdaScope = null; IDisposable lambdaScope = null;
using (CSharpLineMappingWriter mappingWriter = _writer.BuildLineMapping(chunk.Signature.Location, chunk.Signature.Value.Length, _context.SourceFile)) using (CSharpLineMappingWriter mappingWriter = Writer.BuildLineMapping(chunk.Signature.Location, chunk.Signature.Value.Length, Context.SourceFile))
{ {
string accessibility = "public " + (_context.Host.StaticHelpers ? "static" : String.Empty); string accessibility = "public " + (Context.Host.StaticHelpers ? "static" : String.Empty);
_writer.Write(accessibility).Write(" ").Write(_context.Host.GeneratedClassContext.TemplateTypeName).Write(" "); Writer.Write(accessibility).Write(" ").Write(Context.Host.GeneratedClassContext.TemplateTypeName).Write(" ");
mappingWriter.MarkLineMappingStart(); mappingWriter.MarkLineMappingStart();
_writer.Write(chunk.Signature); Writer.Write(chunk.Signature);
mappingWriter.MarkLineMappingEnd(); mappingWriter.MarkLineMappingEnd();
} }
if(chunk.HeaderComplete) if (chunk.HeaderComplete)
{ {
_writer.WriteStartReturn() Writer.WriteStartReturn()
.WriteStartNewObject(_context.Host.GeneratedClassContext.TemplateTypeName); .WriteStartNewObject(Context.Host.GeneratedClassContext.TemplateTypeName);
lambdaScope = _writer.BuildLambda(endLine: false, parameterNames: HelperWriterName); lambdaScope = Writer.BuildLambda(endLine: false, parameterNames: HelperWriterName);
} }
// Generate children code // Generate children code
@ -45,18 +42,18 @@ namespace Microsoft.AspNet.Razor.Generator.Compiler.CSharp
if (chunk.HeaderComplete) if (chunk.HeaderComplete)
{ {
lambdaScope.Dispose(); lambdaScope.Dispose();
_writer.WriteEndMethodInvocation(); Writer.WriteEndMethodInvocation();
} }
if(chunk.Footer != null && !String.IsNullOrEmpty(chunk.Footer.Value)) if (chunk.Footer != null && !String.IsNullOrEmpty(chunk.Footer.Value))
{ {
using(_writer.BuildLineMapping(chunk.Footer.Location, chunk.Footer.Value.Length, _context.SourceFile)) using (Writer.BuildLineMapping(chunk.Footer.Location, chunk.Footer.Value.Length, Context.SourceFile))
{ {
_writer.Write(chunk.Footer); Writer.Write(chunk.Footer);
} }
} }
_writer.WriteLine(); Writer.WriteLine();
} }
} }
} }

View File

@ -1,27 +1,19 @@
using System.Linq; using System;
namespace Microsoft.AspNet.Razor.Generator.Compiler.CSharp namespace Microsoft.AspNet.Razor.Generator.Compiler.CSharp
{ {
public class CSharpTypeMemberVisitor : CodeVisitor public class CSharpTypeMemberVisitor : CodeVisitor<CSharpCodeWriter>
{ {
private CSharpCodeWriter _writer;
private CodeGeneratorContext _context;
public CSharpTypeMemberVisitor(CSharpCodeWriter writer, CodeGeneratorContext context) public CSharpTypeMemberVisitor(CSharpCodeWriter writer, CodeGeneratorContext context)
{ : base(writer, context) { }
_writer = writer;
_context = context;
}
protected override void Visit(TypeMemberChunk chunk) protected override void Visit(TypeMemberChunk chunk)
{ {
Snippet code = chunk.Code.FirstOrDefault(); if (!String.IsNullOrEmpty(chunk.Code))
if (code != null)
{ {
using (_writer.BuildLineMapping(chunk.Start, code.Value.Length, _context.SourceFile)) using (Writer.BuildLineMapping(chunk.Start, chunk.Code.Length, Context.SourceFile))
{ {
_writer.WriteLine(code.Value); Writer.WriteLine(chunk.Code);
} }
} }
} }

View File

@ -2,16 +2,11 @@
namespace Microsoft.AspNet.Razor.Generator.Compiler.CSharp namespace Microsoft.AspNet.Razor.Generator.Compiler.CSharp
{ {
// TODO: This class shares a lot of the same properties as the other CSharpCodeVisitor, make common base? public class CSharpUsingVisitor : CodeVisitor<CSharpCodeWriter>
public class CSharpUsingVisitor : CodeVisitor
{ {
private CSharpCodeWriter _writer; public CSharpUsingVisitor(CSharpCodeWriter writer, CodeGeneratorContext context)
private string _sourceFile; : base(writer, context)
public CSharpUsingVisitor(CSharpCodeWriter writer, string sourceFile)
{ {
_writer = writer;
_sourceFile = sourceFile;
ImportedUsings = new List<string>(); ImportedUsings = new List<string>();
} }
@ -19,10 +14,10 @@ namespace Microsoft.AspNet.Razor.Generator.Compiler.CSharp
protected override void Visit(UsingChunk chunk) protected override void Visit(UsingChunk chunk)
{ {
using (_writer.BuildLineMapping(chunk.Start, chunk.Association.Length, _sourceFile)) using (Writer.BuildLineMapping(chunk.Start, chunk.Association.Length, Context.SourceFile))
{ {
ImportedUsings.Add(chunk.Namespace); ImportedUsings.Add(chunk.Namespace);
_writer.WriteUsing(chunk.Namespace); Writer.WriteUsing(chunk.Namespace);
} }
} }
} }

View File

@ -3,8 +3,17 @@ using System.Collections.Generic;
namespace Microsoft.AspNet.Razor.Generator.Compiler namespace Microsoft.AspNet.Razor.Generator.Compiler
{ {
public abstract class ChunkVisitor : IChunkVisitor public abstract class ChunkVisitor<T> : IChunkVisitor where T : CodeWriter
{ {
public ChunkVisitor(T writer, CodeGeneratorContext context)
{
Writer = writer;
Context = context;
}
protected T Writer { get; private set; }
protected CodeGeneratorContext Context { get; private set; }
public void Accept(IList<Chunk> chunks) public void Accept(IList<Chunk> chunks)
{ {
if (chunks == null) if (chunks == null)

View File

@ -1,8 +1,11 @@
 
namespace Microsoft.AspNet.Razor.Generator.Compiler namespace Microsoft.AspNet.Razor.Generator.Compiler
{ {
public class CodeVisitor : ChunkVisitor public class CodeVisitor<T> : ChunkVisitor<T> where T : CodeWriter
{ {
public CodeVisitor(T writer, CodeGeneratorContext context)
: base(writer, context) { }
protected override void Visit(LiteralChunk chunk) protected override void Visit(LiteralChunk chunk)
{ {
} }
@ -55,5 +58,5 @@ namespace Microsoft.AspNet.Razor.Generator.Compiler
protected override void Visit(SessionStateChunk chunk) protected override void Visit(SessionStateChunk chunk)
{ {
} }
} }
} }