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:
parent
a9d4cd9089
commit
bc3d32ec74
|
|
@ -29,7 +29,6 @@ namespace Microsoft.AspNet.Razor.Generator
|
|||
ns = ns.Substring(1);
|
||||
}
|
||||
|
||||
// TODO: Verify namespace hasn't already been added.
|
||||
codeTreeBuilder.AddUsingChunk(ns, target, context);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -25,11 +25,11 @@ namespace Microsoft.AspNet.Razor.Generator.Compiler.CSharp
|
|||
// Separate the usings and the class
|
||||
writer.WriteLine();
|
||||
|
||||
var baseTypeVisitor = new CSharpBaseTypeVisitor(writer);
|
||||
var baseTypeVisitor = new CSharpBaseTypeVisitor(writer, Context);
|
||||
baseTypeVisitor.Accept(Tree.Chunks);
|
||||
|
||||
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>() :
|
||||
new string[] { baseType };
|
||||
|
|
@ -44,8 +44,6 @@ namespace Microsoft.AspNet.Razor.Generator.Compiler.CSharp
|
|||
new CSharpTypeMemberVisitor(writer, Context).Accept(Tree.Chunks);
|
||||
new CSharpDesignTimeHelpersVisitor(writer, Context).AcceptTree(Tree);
|
||||
|
||||
// TODO: resolve variable declarations
|
||||
|
||||
writer.WriteLineHiddenDirective();
|
||||
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)
|
||||
{
|
||||
// Write out using directives
|
||||
var usingVisitor = new CSharpUsingVisitor(writer, Context.SourceFile);
|
||||
var usingVisitor = new CSharpUsingVisitor(writer, Context);
|
||||
foreach (Chunk chunk in Tree.Chunks)
|
||||
{
|
||||
usingVisitor.Accept(chunk);
|
||||
|
|
|
|||
|
|
@ -1,14 +1,10 @@
|
|||
|
||||
namespace Microsoft.AspNet.Razor.Generator.Compiler.CSharp
|
||||
{
|
||||
public class CSharpBaseTypeVisitor : CodeVisitor
|
||||
public class CSharpBaseTypeVisitor : CodeVisitor<CSharpCodeWriter>
|
||||
{
|
||||
private CSharpCodeWriter _writer;
|
||||
|
||||
public CSharpBaseTypeVisitor(CSharpCodeWriter writer)
|
||||
{
|
||||
_writer = writer;
|
||||
}
|
||||
public CSharpBaseTypeVisitor(CSharpCodeWriter writer, CodeGeneratorContext context)
|
||||
: base(writer, context) { }
|
||||
|
||||
public string CurrentBaseType { get; set; }
|
||||
|
||||
|
|
|
|||
|
|
@ -2,24 +2,20 @@
|
|||
|
||||
namespace Microsoft.AspNet.Razor.Generator.Compiler.CSharp
|
||||
{
|
||||
public class CSharpClassAttributeVisitor : CodeVisitor
|
||||
public class CSharpClassAttributeVisitor : CodeVisitor<CSharpCodeWriter>
|
||||
{
|
||||
private CSharpCodeWriter _writer;
|
||||
|
||||
public CSharpClassAttributeVisitor(CSharpCodeWriter writer)
|
||||
{
|
||||
_writer = writer;
|
||||
}
|
||||
public CSharpClassAttributeVisitor(CSharpCodeWriter writer, CodeGeneratorContext context)
|
||||
: base(writer, context) { }
|
||||
|
||||
protected override void Visit(SessionStateChunk chunk)
|
||||
{
|
||||
_writer.Write("[")
|
||||
.Write(typeof(RazorDirectiveAttribute).FullName)
|
||||
.Write("(")
|
||||
.WriteStringLiteral(SyntaxConstants.CSharp.SessionStateKeyword)
|
||||
.WriteParameterSeparator()
|
||||
.WriteStringLiteral(chunk.Value)
|
||||
.WriteLine(")]");
|
||||
Writer.Write("[")
|
||||
.Write(typeof(RazorDirectiveAttribute).FullName)
|
||||
.Write("(")
|
||||
.WriteStringLiteral(SyntaxConstants.CSharp.SessionStateKeyword)
|
||||
.WriteParameterSeparator()
|
||||
.WriteStringLiteral(chunk.Value)
|
||||
.WriteLine(")]");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,24 +4,20 @@ using System.Linq;
|
|||
|
||||
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 readonly CSharpCodeWriter _writer;
|
||||
private readonly CodeGeneratorContext _context;
|
||||
public CSharpCodeVisitor(CSharpCodeWriter writer, CodeGeneratorContext context)
|
||||
: base(writer, context) { }
|
||||
|
||||
|
||||
public CSharpCodeVisitor(CSharpCodeWriter writer, CodeGeneratorContext context)
|
||||
{
|
||||
_writer = writer;
|
||||
_context = context;
|
||||
}
|
||||
|
||||
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(" = ")
|
||||
.WriteStringLiteral(chunk.Layout)
|
||||
.WriteLine(";");
|
||||
|
|
@ -30,76 +26,76 @@ namespace Microsoft.AspNet.Razor.Generator.Compiler.CSharp
|
|||
|
||||
protected override void Visit(TemplateChunk chunk)
|
||||
{
|
||||
_writer.Write(TemplateBlockCodeGenerator.ItemParameterName).Write(" => ")
|
||||
.WriteStartNewObject(_context.Host.GeneratedClassContext.TemplateTypeName);
|
||||
Writer.Write(TemplateBlockCodeGenerator.ItemParameterName).Write(" => ")
|
||||
.WriteStartNewObject(Context.Host.GeneratedClassContext.TemplateTypeName);
|
||||
|
||||
using (_writer.BuildLambda(endLine: false, parameterNames: TemplateBlockCodeGenerator.TemplateWriterName))
|
||||
using (Writer.BuildLambda(endLine: false, parameterNames: TemplateBlockCodeGenerator.TemplateWriterName))
|
||||
{
|
||||
Visit((ChunkBlock)chunk);
|
||||
}
|
||||
|
||||
_writer.WriteEndMethodInvocation(false).WriteLine();
|
||||
Writer.WriteEndMethodInvocation(false).WriteLine();
|
||||
}
|
||||
|
||||
protected override void Visit(ResolveUrlChunk chunk)
|
||||
{
|
||||
if (!_context.Host.DesignTimeMode && String.IsNullOrEmpty(chunk.Url))
|
||||
if (!Context.Host.DesignTimeMode && String.IsNullOrEmpty(chunk.Url))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// 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 (!String.IsNullOrEmpty(chunk.WriterName))
|
||||
{
|
||||
_writer.WriteStartMethodInvocation(_context.Host.GeneratedClassContext.WriteLiteralToMethodName)
|
||||
Writer.WriteStartMethodInvocation(Context.Host.GeneratedClassContext.WriteLiteralToMethodName)
|
||||
.Write(chunk.WriterName)
|
||||
.WriteParameterSeparator();
|
||||
}
|
||||
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)
|
||||
.WriteEndMethodInvocation(endLine: false);
|
||||
|
||||
if (chunk.RenderingMode == ExpressionRenderingMode.WriteToOutput)
|
||||
{
|
||||
_writer.WriteEndMethodInvocation();
|
||||
Writer.WriteEndMethodInvocation();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected override void Visit(LiteralChunk chunk)
|
||||
{
|
||||
if (!_context.Host.DesignTimeMode && String.IsNullOrEmpty(chunk.Text))
|
||||
if (!Context.Host.DesignTimeMode && String.IsNullOrEmpty(chunk.Text))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// TODO: Add instrumentation
|
||||
|
||||
if (!String.IsNullOrEmpty(chunk.Text) && !_context.Host.DesignTimeMode)
|
||||
if (!String.IsNullOrEmpty(chunk.Text) && !Context.Host.DesignTimeMode)
|
||||
{
|
||||
if (!String.IsNullOrEmpty(chunk.WriterName))
|
||||
{
|
||||
_writer.WriteStartMethodInvocation(_context.Host.GeneratedClassContext.WriteLiteralToMethodName)
|
||||
Writer.WriteStartMethodInvocation(Context.Host.GeneratedClassContext.WriteLiteralToMethodName)
|
||||
.Write(chunk.WriterName)
|
||||
.WriteParameterSeparator();
|
||||
}
|
||||
else
|
||||
{
|
||||
_writer.WriteStartMethodInvocation(_context.Host.GeneratedClassContext.WriteLiteralMethodName);
|
||||
Writer.WriteStartMethodInvocation(Context.Host.GeneratedClassContext.WriteLiteralMethodName);
|
||||
}
|
||||
|
||||
_writer.WriteStringLiteral(chunk.Text)
|
||||
Writer.WriteStringLiteral(chunk.Text)
|
||||
.WriteEndMethodInvocation();
|
||||
}
|
||||
|
||||
|
|
@ -111,88 +107,83 @@ namespace Microsoft.AspNet.Razor.Generator.Compiler.CSharp
|
|||
// TODO: Handle instrumentation
|
||||
// TODO: Refactor
|
||||
|
||||
if (!_context.Host.DesignTimeMode && chunk.RenderingMode == ExpressionRenderingMode.InjectCode)
|
||||
if (!Context.Host.DesignTimeMode && chunk.RenderingMode == ExpressionRenderingMode.InjectCode)
|
||||
{
|
||||
Visit((ChunkBlock)chunk);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (_context.Host.DesignTimeMode)
|
||||
if (Context.Host.DesignTimeMode)
|
||||
{
|
||||
_writer.WriteStartAssignment("__o");
|
||||
Writer.WriteStartAssignment("__o");
|
||||
}
|
||||
else if (chunk.RenderingMode == ExpressionRenderingMode.WriteToOutput)
|
||||
{
|
||||
// TODO: Abstract padding out?
|
||||
|
||||
if (!String.IsNullOrEmpty(chunk.WriterName))
|
||||
{
|
||||
_writer.WriteStartMethodInvocation(_context.Host.GeneratedClassContext.WriteToMethodName)
|
||||
Writer.WriteStartMethodInvocation(Context.Host.GeneratedClassContext.WriteToMethodName)
|
||||
.Write(chunk.WriterName)
|
||||
.WriteParameterSeparator();
|
||||
}
|
||||
else
|
||||
{
|
||||
_writer.WriteStartMethodInvocation(_context.Host.GeneratedClassContext.WriteMethodName);
|
||||
Writer.WriteStartMethodInvocation(Context.Host.GeneratedClassContext.WriteMethodName);
|
||||
}
|
||||
}
|
||||
|
||||
Visit((ChunkBlock)chunk);
|
||||
|
||||
if (_context.Host.DesignTimeMode)
|
||||
if (Context.Host.DesignTimeMode)
|
||||
{
|
||||
_writer.WriteLine(";");
|
||||
Writer.WriteLine(";");
|
||||
}
|
||||
else if (chunk.RenderingMode == ExpressionRenderingMode.WriteToOutput)
|
||||
{
|
||||
_writer.WriteEndMethodInvocation();
|
||||
Writer.WriteEndMethodInvocation();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
.Write(chunk.Code.Value);
|
||||
Writer.Indent(chunk.Start.CharacterIndex)
|
||||
.Write(chunk.Code);
|
||||
}
|
||||
}
|
||||
|
||||
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(snippet.Value);
|
||||
}
|
||||
Writer.Indent(chunk.Start.CharacterIndex);
|
||||
Writer.WriteLine(chunk.Code);
|
||||
}
|
||||
}
|
||||
|
||||
protected override void Visit(DynamicCodeAttributeChunk chunk)
|
||||
{
|
||||
if (_context.Host.DesignTimeMode)
|
||||
if (Context.Host.DesignTimeMode)
|
||||
{
|
||||
return; // Don't generate anything!
|
||||
}
|
||||
|
||||
Chunk code = chunk.Children.FirstOrDefault();
|
||||
|
||||
_writer.WriteParameterSeparator()
|
||||
Writer.WriteParameterSeparator()
|
||||
.WriteLine();
|
||||
|
||||
if (code is ExpressionChunk || code is ExpressionBlockChunk)
|
||||
{
|
||||
_writer.WriteStartMethodInvocation("Tuple.Create")
|
||||
Writer.WriteStartMethodInvocation("Tuple.Create")
|
||||
.WriteLocationTaggedString(chunk.Prefix)
|
||||
.WriteParameterSeparator()
|
||||
.WriteStartMethodInvocation("Tuple.Create", new string[] { "System.Object", "System.Int32" });
|
||||
|
||||
Accept(code);
|
||||
|
||||
_writer.WriteParameterSeparator()
|
||||
Writer.WriteParameterSeparator()
|
||||
.Write(chunk.Start.AbsoluteIndex.ToString(CultureInfo.CurrentCulture))
|
||||
.WriteEndMethodInvocation(false)
|
||||
.WriteParameterSeparator()
|
||||
|
|
@ -201,18 +192,18 @@ namespace Microsoft.AspNet.Razor.Generator.Compiler.CSharp
|
|||
}
|
||||
else
|
||||
{
|
||||
_writer.WriteStartMethodInvocation("Tuple.Create")
|
||||
Writer.WriteStartMethodInvocation("Tuple.Create")
|
||||
.WriteLocationTaggedString(chunk.Prefix)
|
||||
.WriteParameterSeparator()
|
||||
.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);
|
||||
}
|
||||
|
||||
_writer.WriteEndMethodInvocation(false)
|
||||
Writer.WriteEndMethodInvocation(false)
|
||||
.WriteParameterSeparator()
|
||||
.Write(chunk.Start.AbsoluteIndex.ToString(CultureInfo.CurrentCulture))
|
||||
.WriteEndMethodInvocation(endLine: false)
|
||||
|
|
@ -224,23 +215,23 @@ namespace Microsoft.AspNet.Razor.Generator.Compiler.CSharp
|
|||
|
||||
protected override void Visit(LiteralCodeAttributeChunk chunk)
|
||||
{
|
||||
if (_context.Host.DesignTimeMode)
|
||||
if (Context.Host.DesignTimeMode)
|
||||
{
|
||||
return; // Don't generate anything!
|
||||
}
|
||||
|
||||
_writer.WriteParameterSeparator()
|
||||
Writer.WriteParameterSeparator()
|
||||
.WriteStartMethodInvocation("Tuple.Create")
|
||||
.WriteLocationTaggedString(chunk.Prefix)
|
||||
.WriteParameterSeparator();
|
||||
|
||||
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);
|
||||
|
||||
_writer.WriteParameterSeparator()
|
||||
Writer.WriteParameterSeparator()
|
||||
.Write(chunk.ValueLocation.AbsoluteIndex.ToString(CultureInfo.CurrentCulture))
|
||||
.WriteEndMethodInvocation(false)
|
||||
.WriteParameterSeparator()
|
||||
|
|
@ -250,7 +241,7 @@ namespace Microsoft.AspNet.Razor.Generator.Compiler.CSharp
|
|||
}
|
||||
else
|
||||
{
|
||||
_writer.WriteLocationTaggedString(chunk.Value)
|
||||
Writer.WriteLocationTaggedString(chunk.Value)
|
||||
.WriteParameterSeparator()
|
||||
.WriteBooleanLiteral(true)
|
||||
.WriteEndMethodInvocation(false);
|
||||
|
|
@ -259,23 +250,23 @@ namespace Microsoft.AspNet.Razor.Generator.Compiler.CSharp
|
|||
|
||||
protected override void Visit(CodeAttributeChunk chunk)
|
||||
{
|
||||
if (_context.Host.DesignTimeMode)
|
||||
if (Context.Host.DesignTimeMode)
|
||||
{
|
||||
return; // Don't generate anything!
|
||||
}
|
||||
|
||||
if (!String.IsNullOrEmpty(chunk.WriterName))
|
||||
{
|
||||
_writer.WriteStartMethodInvocation(_context.Host.GeneratedClassContext.WriteAttributeToMethodName)
|
||||
Writer.WriteStartMethodInvocation(Context.Host.GeneratedClassContext.WriteAttributeToMethodName)
|
||||
.Write(chunk.WriterName)
|
||||
.WriteParameterSeparator();
|
||||
}
|
||||
else
|
||||
{
|
||||
_writer.WriteStartMethodInvocation(_context.Host.GeneratedClassContext.WriteAttributeMethodName);
|
||||
Writer.WriteStartMethodInvocation(Context.Host.GeneratedClassContext.WriteAttributeMethodName);
|
||||
}
|
||||
|
||||
_writer.WriteStringLiteral(chunk.Attribute)
|
||||
Writer.WriteStringLiteral(chunk.Attribute)
|
||||
.WriteParameterSeparator()
|
||||
.WriteLocationTaggedString(chunk.Prefix)
|
||||
.WriteParameterSeparator()
|
||||
|
|
@ -283,21 +274,21 @@ namespace Microsoft.AspNet.Razor.Generator.Compiler.CSharp
|
|||
|
||||
Visit((ChunkBlock)chunk);
|
||||
|
||||
_writer.WriteEndMethodInvocation();
|
||||
Writer.WriteEndMethodInvocation();
|
||||
}
|
||||
|
||||
protected override void Visit(SectionChunk chunk)
|
||||
{
|
||||
_writer.WriteStartMethodInvocation(_context.Host.GeneratedClassContext.DefineSectionMethodName)
|
||||
Writer.WriteStartMethodInvocation(Context.Host.GeneratedClassContext.DefineSectionMethodName)
|
||||
.WriteStringLiteral(chunk.Name)
|
||||
.WriteParameterSeparator();
|
||||
|
||||
using (_writer.BuildLambda(false))
|
||||
using (Writer.BuildLambda(false))
|
||||
{
|
||||
Visit((ChunkBlock)chunk);
|
||||
}
|
||||
|
||||
_writer.WriteEndMethodInvocation();
|
||||
Writer.WriteEndMethodInvocation();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,26 +1,20 @@
|
|||
|
||||
namespace Microsoft.AspNet.Razor.Generator.Compiler.CSharp
|
||||
{
|
||||
public class CSharpDesignTimeHelpersVisitor : CodeVisitor
|
||||
public class CSharpDesignTimeHelpersVisitor : CodeVisitor<CSharpCodeWriter>
|
||||
{
|
||||
internal const string InheritsHelper = "__inheritsHelper";
|
||||
|
||||
private readonly CSharpCodeWriter _writer;
|
||||
private readonly CodeGeneratorContext _context;
|
||||
|
||||
public CSharpDesignTimeHelpersVisitor(CSharpCodeWriter writer, CodeGeneratorContext context)
|
||||
{
|
||||
_writer = writer;
|
||||
_context = context;
|
||||
}
|
||||
: base(writer, context) { }
|
||||
|
||||
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);
|
||||
}
|
||||
|
|
@ -30,17 +24,17 @@ namespace Microsoft.AspNet.Razor.Generator.Compiler.CSharp
|
|||
|
||||
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();
|
||||
_writer.Write(chunk.TypeName);
|
||||
Writer.Write(chunk.TypeName);
|
||||
lineMappingWriter.MarkLineMappingEnd();
|
||||
|
||||
_writer.Write(" ").Write(InheritsHelper).Write(" = null;");
|
||||
Writer.Write(" ").Write(InheritsHelper).Write(" = null;");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,18 +2,15 @@
|
|||
|
||||
namespace Microsoft.AspNet.Razor.Generator.Compiler.CSharp
|
||||
{
|
||||
public class CSharpHelperVisitor : CodeVisitor
|
||||
public class CSharpHelperVisitor : CodeVisitor<CSharpCodeWriter>
|
||||
{
|
||||
private const string HelperWriterName = "__razor_helper_writer";
|
||||
|
||||
private readonly CSharpCodeWriter _writer;
|
||||
private readonly CodeGeneratorContext _context;
|
||||
private CSharpCodeVisitor _codeVisitor;
|
||||
|
||||
public CSharpHelperVisitor(CSharpCodeWriter writer, CodeGeneratorContext context)
|
||||
: base(writer, context)
|
||||
{
|
||||
_writer = writer;
|
||||
_context = context;
|
||||
_codeVisitor = new CSharpCodeVisitor(writer, context);
|
||||
}
|
||||
|
||||
|
|
@ -21,42 +18,42 @@ namespace Microsoft.AspNet.Razor.Generator.Compiler.CSharp
|
|||
{
|
||||
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();
|
||||
_writer.Write(chunk.Signature);
|
||||
Writer.Write(chunk.Signature);
|
||||
mappingWriter.MarkLineMappingEnd();
|
||||
}
|
||||
|
||||
if(chunk.HeaderComplete)
|
||||
if (chunk.HeaderComplete)
|
||||
{
|
||||
_writer.WriteStartReturn()
|
||||
.WriteStartNewObject(_context.Host.GeneratedClassContext.TemplateTypeName);
|
||||
Writer.WriteStartReturn()
|
||||
.WriteStartNewObject(Context.Host.GeneratedClassContext.TemplateTypeName);
|
||||
|
||||
lambdaScope = _writer.BuildLambda(endLine: false, parameterNames: HelperWriterName);
|
||||
lambdaScope = Writer.BuildLambda(endLine: false, parameterNames: HelperWriterName);
|
||||
}
|
||||
|
||||
|
||||
// Generate children code
|
||||
_codeVisitor.Accept(chunk.Children);
|
||||
|
||||
if (chunk.HeaderComplete)
|
||||
{
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,27 +1,19 @@
|
|||
using System.Linq;
|
||||
using System;
|
||||
|
||||
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)
|
||||
{
|
||||
_writer = writer;
|
||||
_context = context;
|
||||
}
|
||||
: base(writer, context) { }
|
||||
|
||||
protected override void Visit(TypeMemberChunk chunk)
|
||||
{
|
||||
Snippet code = chunk.Code.FirstOrDefault();
|
||||
|
||||
if (code != null)
|
||||
if (!String.IsNullOrEmpty(chunk.Code))
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,16 +2,11 @@
|
|||
|
||||
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
|
||||
public class CSharpUsingVisitor : CodeVisitor<CSharpCodeWriter>
|
||||
{
|
||||
private CSharpCodeWriter _writer;
|
||||
private string _sourceFile;
|
||||
|
||||
public CSharpUsingVisitor(CSharpCodeWriter writer, string sourceFile)
|
||||
public CSharpUsingVisitor(CSharpCodeWriter writer, CodeGeneratorContext context)
|
||||
: base(writer, context)
|
||||
{
|
||||
_writer = writer;
|
||||
_sourceFile = sourceFile;
|
||||
ImportedUsings = new List<string>();
|
||||
}
|
||||
|
||||
|
|
@ -19,10 +14,10 @@ namespace Microsoft.AspNet.Razor.Generator.Compiler.CSharp
|
|||
|
||||
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);
|
||||
_writer.WriteUsing(chunk.Namespace);
|
||||
Writer.WriteUsing(chunk.Namespace);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,8 +3,17 @@ using System.Collections.Generic;
|
|||
|
||||
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)
|
||||
{
|
||||
if (chunks == null)
|
||||
|
|
|
|||
|
|
@ -1,8 +1,11 @@
|
|||
|
||||
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)
|
||||
{
|
||||
}
|
||||
|
|
@ -55,5 +58,5 @@ namespace Microsoft.AspNet.Razor.Generator.Compiler
|
|||
protected override void Visit(SessionStateChunk chunk)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue