Modify CSharpCodeWriter to generate async lambdas for section

Fixes #85
This commit is contained in:
Pranav K 2014-10-03 16:00:24 -07:00
parent 4f9a6fed93
commit a2a05e7153
7 changed files with 28 additions and 21 deletions

View File

@ -284,13 +284,23 @@ namespace Microsoft.AspNet.Razor.Generator.Compiler.CSharp
return new CSharpCodeWritingScope(this); return new CSharpCodeWritingScope(this);
} }
public CSharpCodeWritingScope BuildLambda(params string[] parameterNames)
{
return BuildLambda(true, parameterNames);
}
public CSharpCodeWritingScope BuildLambda(bool endLine, params string[] parameterNames) public CSharpCodeWritingScope BuildLambda(bool endLine, params string[] parameterNames)
{ {
return BuildLambda(endLine, async: false, parameterNames: parameterNames);
}
public CSharpCodeWritingScope BuildAsyncLambda(bool endLine, params string[] parameterNames)
{
return BuildLambda(endLine, async: true, parameterNames: parameterNames);
}
private CSharpCodeWritingScope BuildLambda(bool endLine, bool async, string[] parameterNames)
{
if (async)
{
Write("async");
}
Write("(").Write(string.Join(", ", parameterNames)).Write(") => "); Write("(").Write(string.Join(", ", parameterNames)).Write(") => ");
var scope = new CSharpCodeWritingScope(this); var scope = new CSharpCodeWritingScope(this);

View File

@ -320,19 +320,16 @@ namespace Microsoft.AspNet.Razor.Generator.Compiler.CSharp
{ {
Writer.WriteStartMethodInvocation(Context.Host.GeneratedClassContext.DefineSectionMethodName) Writer.WriteStartMethodInvocation(Context.Host.GeneratedClassContext.DefineSectionMethodName)
.WriteStringLiteral(chunk.Name) .WriteStringLiteral(chunk.Name)
.WriteParameterSeparator() .WriteParameterSeparator();
.WriteStartNewObject(Context.Host.GeneratedClassContext.TemplateTypeName);
var currentTargetWriterName = Context.TargetWriterName; var currentTargetWriterName = Context.TargetWriterName;
Context.TargetWriterName = TemplateWriterName; Context.TargetWriterName = TemplateWriterName;
using (Writer.BuildLambda(endLine: false, parameterNames: TemplateWriterName)) using (Writer.BuildAsyncLambda(endLine: false, parameterNames: TemplateWriterName))
{ {
Accept(chunk.Children); Accept(chunk.Children);
} }
Context.TargetWriterName = currentTargetWriterName; Context.TargetWriterName = currentTargetWriterName;
Writer.WriteEndMethodInvocation(endLine: false);
Writer.WriteEndMethodInvocation(); Writer.WriteEndMethodInvocation();
} }

View File

@ -294,7 +294,7 @@ namespace Microsoft.AspNet.Razor.Test.Generator
BuildLineMapping(113, 7, 2, 1262, 71, 6, 12), BuildLineMapping(113, 7, 2, 1262, 71, 6, 12),
BuildLineMapping(129, 8, 1, 1343, 76, 6, 4), BuildLineMapping(129, 8, 1, 1343, 76, 6, 4),
BuildLineMapping(142, 8, 1443, 78, 14, 3), BuildLineMapping(142, 8, 1443, 78, 14, 3),
BuildLineMapping(204, 13, 5, 1638, 90, 6, 3) BuildLineMapping(204, 13, 5, 1630, 90, 6, 3)
}); });
} }

View File

@ -86,14 +86,14 @@ __o = Foo(item => new Template((__razor_template_writer) => {
#line default #line default
#line hidden #line hidden
DefineSection("Footer", new Template((__razor_template_writer) => { DefineSection("Footer", async(__razor_template_writer) => {
#line 14 "DesignTime.cshtml" #line 14 "DesignTime.cshtml"
__o = bar; __o = bar;
#line default #line default
#line hidden #line hidden
} }
)); );
} }
#pragma warning restore 1998 #pragma warning restore 1998
} }

View File

@ -92,7 +92,7 @@ namespace TestOutput
Instrumentation.BeginContext(474, 4, true); Instrumentation.BeginContext(474, 4, true);
WriteLiteral("\r\n\r\n"); WriteLiteral("\r\n\r\n");
Instrumentation.EndContext(); Instrumentation.EndContext();
DefineSection("Foo", new Template((__razor_template_writer) => { DefineSection("Foo", async(__razor_template_writer) => {
Instrumentation.BeginContext(492, 8, true); Instrumentation.BeginContext(492, 8, true);
WriteLiteralTo(__razor_template_writer, "\r\n <a"); WriteLiteralTo(__razor_template_writer, "\r\n <a");
Instrumentation.EndContext(); Instrumentation.EndContext();
@ -126,7 +126,7 @@ namespace TestOutput
WriteLiteralTo(__razor_template_writer, ">Crazy Url!</a>\r\n"); WriteLiteralTo(__razor_template_writer, ">Crazy Url!</a>\r\n");
Instrumentation.EndContext(); Instrumentation.EndContext();
} }
)); );
} }
#pragma warning restore 1998 #pragma warning restore 1998
} }

View File

@ -24,21 +24,21 @@ namespace TestOutput
Instrumentation.BeginContext(47, 33, true); Instrumentation.BeginContext(47, 33, true);
WriteLiteral("\r\n\r\n<div>This is in the Body>\r\n\r\n"); WriteLiteral("\r\n\r\n<div>This is in the Body>\r\n\r\n");
Instrumentation.EndContext(); Instrumentation.EndContext();
DefineSection("Section2", new Template((__razor_template_writer) => { DefineSection("Section2", async(__razor_template_writer) => {
Instrumentation.BeginContext(99, 39, true); Instrumentation.BeginContext(99, 39, true);
WriteLiteralTo(__razor_template_writer, "\r\n <div>This is in Section 2</div>\r\n"); WriteLiteralTo(__razor_template_writer, "\r\n <div>This is in Section 2</div>\r\n");
Instrumentation.EndContext(); Instrumentation.EndContext();
} }
)); );
Instrumentation.BeginContext(141, 2, true); Instrumentation.BeginContext(141, 2, true);
WriteLiteral("\r\n"); WriteLiteral("\r\n");
Instrumentation.EndContext(); Instrumentation.EndContext();
DefineSection("Section1", new Template((__razor_template_writer) => { DefineSection("Section1", async(__razor_template_writer) => {
Instrumentation.BeginContext(162, 39, true); Instrumentation.BeginContext(162, 39, true);
WriteLiteralTo(__razor_template_writer, "\r\n <div>This is in Section 1</div>\r\n"); WriteLiteralTo(__razor_template_writer, "\r\n <div>This is in Section 1</div>\r\n");
Instrumentation.EndContext(); Instrumentation.EndContext();
} }
)); );
} }
#pragma warning restore 1998 #pragma warning restore 1998
} }

View File

@ -34,7 +34,7 @@ namespace TestOutput
Instrumentation.BeginContext(63, 4, true); Instrumentation.BeginContext(63, 4, true);
WriteLiteral("\r\n\r\n"); WriteLiteral("\r\n\r\n");
Instrumentation.EndContext(); Instrumentation.EndContext();
DefineSection("MySection", new Template((__razor_template_writer) => { DefineSection("MySection", async(__razor_template_writer) => {
Instrumentation.BeginContext(87, 21, true); Instrumentation.BeginContext(87, 21, true);
WriteLiteralTo(__razor_template_writer, "\r\n <div>\r\n "); WriteLiteralTo(__razor_template_writer, "\r\n <div>\r\n ");
Instrumentation.EndContext(); Instrumentation.EndContext();
@ -90,7 +90,7 @@ Write(DateTime.Now);
WriteLiteralTo(__razor_template_writer, "\r\n </div>\r\n"); WriteLiteralTo(__razor_template_writer, "\r\n </div>\r\n");
Instrumentation.EndContext(); Instrumentation.EndContext();
} }
)); );
} }
#pragma warning restore 1998 #pragma warning restore 1998
} }