Re-applied code review changes for formatting fix.

The previous fix was accidentally overridden.  Also changed how we
render chunk block's children.  New way avoids casts and removes logic
from base.
This commit is contained in:
N. Taylor Mullen 2014-03-05 11:24:06 -08:00
parent 549e36b803
commit 6ea8d7721b
19 changed files with 65 additions and 63 deletions

View File

@ -31,7 +31,7 @@ namespace Microsoft.AspNet.Razor.Generator.Compiler.CSharp
_writePragmas = true;
// TODO: Should this just be '\n'?
if (_writer.LastWrite.Last() != '\n')
if (!_writer.LastWrite.EndsWith("\n"))
{
_writer.WriteLine();
}
@ -73,13 +73,13 @@ namespace Microsoft.AspNet.Razor.Generator.Compiler.CSharp
{
// Need to add an additional line at the end IF there wasn't one already written.
// This is needed to work with the C# editor's handling of #line ...
bool writeExtraLine = _writer.ToString().Last() != '\n';
bool endsWithNewline = _writer.ToString().EndsWith("\n");
// Always write at least 1 empty line to potentially separate code from pragmas.
_writer.WriteLine();
// Check if the previous empty line wasn't enough to separate code from pragmas.
if (writeExtraLine)
if (!endsWithNewline)
{
_writer.WriteLine();
}

View File

@ -23,7 +23,7 @@ namespace Microsoft.AspNet.Razor.Generator.Compiler.CSharp
throw new ArgumentNullException("target");
}
int padding = CalculatePadding(target, 0);
int padding = CalculatePadding(target, generatedStart: 0);
// We treat statement padding specially so for brace positioning, so that in the following example:
// @if (foo > 0)
@ -44,7 +44,12 @@ namespace Microsoft.AspNet.Razor.Generator.Compiler.CSharp
return generatedCode;
}
public string BuildExpressionPadding(Span target, int generatedStart = 0)
public string BuildExpressionPadding(Span target)
{
return BuildExpressionPadding(target, generatedStart: 0);
}
public string BuildExpressionPadding(Span target, int generatedStart)
{
int padding = CalculatePadding(target, generatedStart);
@ -69,7 +74,8 @@ namespace Microsoft.AspNet.Razor.Generator.Compiler.CSharp
// In design time: __o = somecode();
// In Run time: Write(somecode());
//
// In both cases the padding would have been 1 space to remote the space the @ symbol takes, which will be smaller than the 6 chars the hidden generated code takes.
// In both cases the padding would have been 1 space to remote the space the @ symbol takes, which will be smaller than the 6
// chars the hidden generated code takes.
if (padding < 0)
{
padding = 0;

View File

@ -40,7 +40,7 @@ namespace Microsoft.AspNet.Razor.Generator.Compiler.CSharp
using (Writer.BuildLambda(endLine: false, parameterNames: TemplateBlockCodeGenerator.TemplateWriterName))
{
Visit((ChunkBlock)chunk);
Accept(chunk.Children);
}
Context.TargetWriterName = currentTargetWriterName;
@ -182,7 +182,7 @@ namespace Microsoft.AspNet.Razor.Generator.Compiler.CSharp
using (Writer.BuildLambda(endLine: false, parameterNames: ValueWriterName))
{
Visit((ChunkBlock)chunk);
Accept(chunk.Children);
}
Writer.WriteEndMethodInvocation(false)
@ -217,7 +217,7 @@ namespace Microsoft.AspNet.Razor.Generator.Compiler.CSharp
ExpressionRenderingMode currentRenderingMode = Context.ExpressionRenderingMode;
Context.ExpressionRenderingMode = ExpressionRenderingMode.InjectCode;
Visit((ChunkBlock)chunk);
Accept(chunk.Children);
Context.ExpressionRenderingMode = currentRenderingMode;
@ -262,7 +262,7 @@ namespace Microsoft.AspNet.Razor.Generator.Compiler.CSharp
.WriteParameterSeparator()
.WriteLocationTaggedString(chunk.Suffix);
Visit((ChunkBlock)chunk);
Accept(chunk.Children);
Writer.WriteEndMethodInvocation();
}
@ -275,7 +275,7 @@ namespace Microsoft.AspNet.Razor.Generator.Compiler.CSharp
using (Writer.BuildLambda(false))
{
Visit((ChunkBlock)chunk);
Accept(chunk.Children);
}
Writer.WriteEndMethodInvocation();
@ -285,38 +285,35 @@ namespace Microsoft.AspNet.Razor.Generator.Compiler.CSharp
{
// TODO: Handle instrumentation
int currentIndent = Writer.CurrentIndent;
string designTimeAssignment = "__o = ";
// The first child should never be null, it should always be a transition span, that's what
// defines an expression block chunk.
var firstChild = (ExpressionChunk)chunk.Children.FirstOrDefault();
Writer.ResetIndent()
.WriteLineNumberDirective(1, "This is here only for document formatting.")
// We build the padding with an offset of the design time assignment statement.
.Write(_paddingBuilder.BuildExpressionPadding((Span)firstChild.Association, designTimeAssignment.Length))
.Write(designTimeAssignment);
// We map the first line of code but do not write the line pragmas associated with it.
CreateRawCodeMapping(firstChild.Code, firstChild.Association.Start);
// This is a temporary block that is indentical to the current one with the exception of its children.
var subBlock = new ChunkBlock
if (firstChild != null)
{
Start = chunk.Start,
Association = chunk.Association,
Children = chunk.Children.Skip(1).ToList()
};
int currentIndent = Writer.CurrentIndent;
string designTimeAssignment = "__o = ";
Writer.ResetIndent();
// Render all children (except for the first one)
Visit(subBlock);
// This is only here to enable accurate formatting by the C# editor.
Writer.WriteLineNumberDirective(1, "------------------------------------------");
Writer.WriteLine(";")
.WriteLine()
.WriteLineDefaultDirective()
.WriteLineHiddenDirective()
.SetIndent(currentIndent);
// We build the padding with an offset of the design time assignment statement.
Writer.Write(_paddingBuilder.BuildExpressionPadding((Span)firstChild.Association, designTimeAssignment.Length))
.Write(designTimeAssignment);
// We map the first line of code but do not write the line pragmas associated with it.
CreateRawCodeMapping(firstChild.Code, firstChild.Association.Start);
// Render all but the first child.
// The reason why we render the other children differently is because when formatting the C# code
// the formatter expects the start line to have the assignment statement on it.
Accept(chunk.Children.Skip(1).ToList());
Writer.WriteLine(";")
.WriteLine()
.WriteLineDefaultDirective()
.WriteLineHiddenDirective()
.SetIndent(currentIndent);
}
}
public void RenderRuntimeExpressionBlockChunk(ExpressionBlockChunk chunk)
@ -325,7 +322,7 @@ namespace Microsoft.AspNet.Razor.Generator.Compiler.CSharp
if (Context.ExpressionRenderingMode == ExpressionRenderingMode.InjectCode)
{
Visit((ChunkBlock)chunk);
Accept(chunk.Children);
}
else if (Context.ExpressionRenderingMode == ExpressionRenderingMode.WriteToOutput)
{
@ -340,7 +337,7 @@ namespace Microsoft.AspNet.Razor.Generator.Compiler.CSharp
Writer.WriteStartMethodInvocation(Context.Host.GeneratedClassContext.WriteMethodName);
}
Visit((ChunkBlock)chunk);
Accept(chunk.Children);
Writer.WriteEndMethodInvocation()
.WriteLine();

View File

@ -23,7 +23,6 @@ namespace Microsoft.AspNet.Razor.Generator.Compiler
}
protected override void Visit(ChunkBlock chunk)
{
Accept(chunk.Children);
}
protected override void Visit(DynamicCodeAttributeChunk chunk)
{

View File

@ -54,7 +54,7 @@ Foo() {
#line default
#line hidden
#line 1 "This is here only for document formatting."
#line 1 "------------------------------------------"
__o = i;
#line default
@ -66,14 +66,14 @@ Foo() {
#line default
#line hidden
#line 1 "This is here only for document formatting."
#line 1 "------------------------------------------"
__o = Foo(Bar.Baz);
#line default
#line hidden
#line 1 "This is here only for document formatting."
#line 1 "------------------------------------------"
__o = Foo(item => new Template((__razor_template_writer) => {
#line 1 "This is here only for document formatting."
#line 1 "------------------------------------------"
__o = baz;
#line default
@ -90,7 +90,7 @@ __o = Foo(item => new Template((__razor_template_writer) => {
#line default
#line hidden
DefineSection("Footer", () => {
#line 1 "This is here only for document formatting."
#line 1 "------------------------------------------"
__o = bar;
#line default

View File

@ -17,7 +17,7 @@ namespace TestOutput
public override void Execute()
{
#line 1 "This is here only for document formatting."
#line 1 "------------------------------------------"
__o = ;
#line default

View File

@ -17,7 +17,7 @@ namespace TestOutput
public override void Execute()
{
#line 1 "This is here only for document formatting."
#line 1 "------------------------------------------"
__o = ;
#line default

View File

@ -24,7 +24,7 @@ namespace TestOutput
#line default
#line hidden
#line 1 "This is here only for document formatting."
#line 1 "------------------------------------------"
__o = ;
#line default

View File

@ -24,7 +24,7 @@ namespace TestOutput
#line default
#line hidden
#line 1 "This is here only for document formatting."
#line 1 "------------------------------------------"
__o = ;
#line default

View File

@ -17,7 +17,7 @@ namespace TestOutput
public override void Execute()
{
#line 1 "This is here only for document formatting."
#line 1 "------------------------------------------"
__o = ;
#line default

View File

@ -32,7 +32,7 @@ namespace TestOutput
public override void Execute()
{
#line 1 "This is here only for document formatting."
#line 1 "------------------------------------------"
__o = RandomInt();
#line default

View File

@ -32,7 +32,7 @@ namespace TestOutput
public override void Execute()
{
#line 1 "This is here only for document formatting."
#line 1 "------------------------------------------"
__o = RandomInt();
#line default

View File

@ -17,7 +17,7 @@ namespace TestOutput
public override void Execute()
{
#line 1 "This is here only for document formatting."
#line 1 "------------------------------------------"
__o = ;
#line default

View File

@ -34,12 +34,12 @@ using System
public override void Execute()
{
#line 1 "This is here only for document formatting."
#line 1 "------------------------------------------"
__o = typeof(Path).FullName;
#line default
#line hidden
#line 1 "This is here only for document formatting."
#line 1 "------------------------------------------"
__o = typeof(Foo).FullName;
#line default

View File

@ -22,7 +22,7 @@ namespace TestOutput
public override void Execute()
{
#line 1 "This is here only for document formatting."
#line 1 "------------------------------------------"
__o = foo();
#line default

View File

@ -46,12 +46,12 @@ namespace TestOutput
#line default
#line hidden
#line 1 "This is here only for document formatting."
#line 1 "------------------------------------------"
__o = bar;
#line default
#line hidden
#line 1 "This is here only for document formatting."
#line 1 "------------------------------------------"
__o = a
#line 15 "RazorComments.cshtml"
b

View File

@ -23,7 +23,7 @@ namespace TestOutput
#line default
#line hidden
#line 1 "This is here only for document formatting."
#line 1 "------------------------------------------"
__o = DateTime.;
#line default

View File

@ -23,7 +23,7 @@ namespace TestOutput
#line default
#line hidden
#line 1 "This is here only for document formatting."
#line 1 "------------------------------------------"
__o = DateTime.;
#line default

View File

@ -23,7 +23,7 @@
#line default
#line hidden
#line 1 "This is here only for document formatting."
#line 1 "------------------------------------------"
__o = hello;
#line default
@ -34,7 +34,7 @@
#line default
#line hidden
#line 1 "This is here only for document formatting."
#line 1 "------------------------------------------"
__o = c;
#line default