From 2b3315edce500ac0f03e24831df6e7a58c722927 Mon Sep 17 00:00:00 2001 From: Pranav K Date: Thu, 19 Nov 2015 12:08:00 -0800 Subject: [PATCH 1/2] Use the new line character to check if the CodeWriter buffer ends in a line feed. Fixes #577 --- .../CodeGenerators/CSharpLineMappingWriter.cs | 2 +- src/Microsoft.AspNet.Razor/CodeGenerators/CodeWriter.cs | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.AspNet.Razor/CodeGenerators/CSharpLineMappingWriter.cs b/src/Microsoft.AspNet.Razor/CodeGenerators/CSharpLineMappingWriter.cs index e4f003e02..3e6859f37 100644 --- a/src/Microsoft.AspNet.Razor/CodeGenerators/CSharpLineMappingWriter.cs +++ b/src/Microsoft.AspNet.Razor/CodeGenerators/CSharpLineMappingWriter.cs @@ -114,7 +114,7 @@ public void Dispose() { // 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 ... - var endsWithNewline = _writer.GenerateCode().EndsWith("\n"); + var endsWithNewline = _writer.Builder[_writer.Builder.Length - 1] == '\n'; // Always write at least 1 empty line to potentially separate code from pragmas. _writer.WriteLine(); diff --git a/src/Microsoft.AspNet.Razor/CodeGenerators/CodeWriter.cs b/src/Microsoft.AspNet.Razor/CodeGenerators/CodeWriter.cs index 8190243d0..171883c22 100644 --- a/src/Microsoft.AspNet.Razor/CodeGenerators/CodeWriter.cs +++ b/src/Microsoft.AspNet.Razor/CodeGenerators/CodeWriter.cs @@ -3,6 +3,7 @@ using System; using System.IO; +using System.Text; namespace Microsoft.AspNet.Razor.CodeGenerators { @@ -18,6 +19,8 @@ public class CodeWriter : IDisposable private int _currentLineIndex; private int _currentLineCharacterIndex; + public StringBuilder Builder => _writer.GetStringBuilder(); + public string LastWrite { get; private set; } public int CurrentIndent { get; private set; } From cbf5e7883e4fcad5de1ef4a9fc1e20d2e517505c Mon Sep 17 00:00:00 2001 From: Pranav K Date: Thu, 19 Nov 2015 14:42:24 -0800 Subject: [PATCH 2/2] Changes per PR comments --- .../CodeGenerators/CSharpLineMappingWriter.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.AspNet.Razor/CodeGenerators/CSharpLineMappingWriter.cs b/src/Microsoft.AspNet.Razor/CodeGenerators/CSharpLineMappingWriter.cs index 3e6859f37..f8ca7ca1e 100644 --- a/src/Microsoft.AspNet.Razor/CodeGenerators/CSharpLineMappingWriter.cs +++ b/src/Microsoft.AspNet.Razor/CodeGenerators/CSharpLineMappingWriter.cs @@ -114,7 +114,8 @@ public void Dispose() { // 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 ... - var endsWithNewline = _writer.Builder[_writer.Builder.Length - 1] == '\n'; + var builder = _writer.Builder; + var endsWithNewline = builder.Length > 0 && builder[builder.Length - 1] == '\n'; // Always write at least 1 empty line to potentially separate code from pragmas. _writer.WriteLine();