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; _writePragmas = true;
// TODO: Should this just be '\n'? // TODO: Should this just be '\n'?
if (_writer.LastWrite.Last() != '\n') if (!_writer.LastWrite.EndsWith("\n"))
{ {
_writer.WriteLine(); _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. // 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 ... // 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. // Always write at least 1 empty line to potentially separate code from pragmas.
_writer.WriteLine(); _writer.WriteLine();
// Check if the previous empty line wasn't enough to separate code from pragmas. // Check if the previous empty line wasn't enough to separate code from pragmas.
if (writeExtraLine) if (!endsWithNewline)
{ {
_writer.WriteLine(); _writer.WriteLine();
} }

View File

@ -23,7 +23,7 @@ namespace Microsoft.AspNet.Razor.Generator.Compiler.CSharp
throw new ArgumentNullException("target"); 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: // We treat statement padding specially so for brace positioning, so that in the following example:
// @if (foo > 0) // @if (foo > 0)
@ -44,7 +44,12 @@ namespace Microsoft.AspNet.Razor.Generator.Compiler.CSharp
return generatedCode; 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); int padding = CalculatePadding(target, generatedStart);
@ -69,7 +74,8 @@ namespace Microsoft.AspNet.Razor.Generator.Compiler.CSharp
// In design time: __o = somecode(); // In design time: __o = somecode();
// In Run time: Write(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) if (padding < 0)
{ {
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)) using (Writer.BuildLambda(endLine: false, parameterNames: TemplateBlockCodeGenerator.TemplateWriterName))
{ {
Visit((ChunkBlock)chunk); Accept(chunk.Children);
} }
Context.TargetWriterName = currentTargetWriterName; Context.TargetWriterName = currentTargetWriterName;
@ -182,7 +182,7 @@ namespace Microsoft.AspNet.Razor.Generator.Compiler.CSharp
using (Writer.BuildLambda(endLine: false, parameterNames: ValueWriterName)) using (Writer.BuildLambda(endLine: false, parameterNames: ValueWriterName))
{ {
Visit((ChunkBlock)chunk); Accept(chunk.Children);
} }
Writer.WriteEndMethodInvocation(false) Writer.WriteEndMethodInvocation(false)
@ -217,7 +217,7 @@ namespace Microsoft.AspNet.Razor.Generator.Compiler.CSharp
ExpressionRenderingMode currentRenderingMode = Context.ExpressionRenderingMode; ExpressionRenderingMode currentRenderingMode = Context.ExpressionRenderingMode;
Context.ExpressionRenderingMode = ExpressionRenderingMode.InjectCode; Context.ExpressionRenderingMode = ExpressionRenderingMode.InjectCode;
Visit((ChunkBlock)chunk); Accept(chunk.Children);
Context.ExpressionRenderingMode = currentRenderingMode; Context.ExpressionRenderingMode = currentRenderingMode;
@ -262,7 +262,7 @@ namespace Microsoft.AspNet.Razor.Generator.Compiler.CSharp
.WriteParameterSeparator() .WriteParameterSeparator()
.WriteLocationTaggedString(chunk.Suffix); .WriteLocationTaggedString(chunk.Suffix);
Visit((ChunkBlock)chunk); Accept(chunk.Children);
Writer.WriteEndMethodInvocation(); Writer.WriteEndMethodInvocation();
} }
@ -275,7 +275,7 @@ namespace Microsoft.AspNet.Razor.Generator.Compiler.CSharp
using (Writer.BuildLambda(false)) using (Writer.BuildLambda(false))
{ {
Visit((ChunkBlock)chunk); Accept(chunk.Children);
} }
Writer.WriteEndMethodInvocation(); Writer.WriteEndMethodInvocation();
@ -285,38 +285,35 @@ namespace Microsoft.AspNet.Razor.Generator.Compiler.CSharp
{ {
// TODO: Handle instrumentation // 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(); var firstChild = (ExpressionChunk)chunk.Children.FirstOrDefault();
Writer.ResetIndent() if (firstChild != null)
.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
{ {
Start = chunk.Start, int currentIndent = Writer.CurrentIndent;
Association = chunk.Association, string designTimeAssignment = "__o = ";
Children = chunk.Children.Skip(1).ToList() Writer.ResetIndent();
};
// Render all children (except for the first one) // This is only here to enable accurate formatting by the C# editor.
Visit(subBlock); Writer.WriteLineNumberDirective(1, "------------------------------------------");
Writer.WriteLine(";") // We build the padding with an offset of the design time assignment statement.
.WriteLine() Writer.Write(_paddingBuilder.BuildExpressionPadding((Span)firstChild.Association, designTimeAssignment.Length))
.WriteLineDefaultDirective() .Write(designTimeAssignment);
.WriteLineHiddenDirective()
.SetIndent(currentIndent); // 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) public void RenderRuntimeExpressionBlockChunk(ExpressionBlockChunk chunk)
@ -325,7 +322,7 @@ namespace Microsoft.AspNet.Razor.Generator.Compiler.CSharp
if (Context.ExpressionRenderingMode == ExpressionRenderingMode.InjectCode) if (Context.ExpressionRenderingMode == ExpressionRenderingMode.InjectCode)
{ {
Visit((ChunkBlock)chunk); Accept(chunk.Children);
} }
else if (Context.ExpressionRenderingMode == ExpressionRenderingMode.WriteToOutput) else if (Context.ExpressionRenderingMode == ExpressionRenderingMode.WriteToOutput)
{ {
@ -340,7 +337,7 @@ namespace Microsoft.AspNet.Razor.Generator.Compiler.CSharp
Writer.WriteStartMethodInvocation(Context.Host.GeneratedClassContext.WriteMethodName); Writer.WriteStartMethodInvocation(Context.Host.GeneratedClassContext.WriteMethodName);
} }
Visit((ChunkBlock)chunk); Accept(chunk.Children);
Writer.WriteEndMethodInvocation() Writer.WriteEndMethodInvocation()
.WriteLine(); .WriteLine();

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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