Cleaned up some code writer code.
Modified the ToString override to be GenerateCode and fixed how it cached.
This commit is contained in:
parent
9c4a6e901d
commit
9abd67da4b
|
|
@ -61,7 +61,7 @@ namespace Microsoft.AspNet.Razor.Generator.Compiler.CSharp
|
|||
}
|
||||
}
|
||||
|
||||
return new CodeBuilderResult(writer.ToString(), writer.LineMappingManager.Mappings);
|
||||
return new CodeBuilderResult(writer.GenerateCode(), writer.LineMappingManager.Mappings);
|
||||
}
|
||||
|
||||
private void AddImports(CodeTree codeTree, CSharpCodeWriter writer, IEnumerable<string> defaultImports)
|
||||
|
|
|
|||
|
|
@ -48,7 +48,7 @@ namespace Microsoft.AspNet.Razor.Generator.Compiler.CSharp
|
|||
|
||||
public void MarkLineMappingEnd()
|
||||
{
|
||||
_generatedContentLength = _writer.ToString().Length - _generatedLocation.AbsoluteIndex;
|
||||
_generatedContentLength = _writer.GenerateCode().Length - _generatedLocation.AbsoluteIndex;
|
||||
}
|
||||
|
||||
protected virtual void Dispose(bool disposing)
|
||||
|
|
@ -58,7 +58,7 @@ namespace Microsoft.AspNet.Razor.Generator.Compiler.CSharp
|
|||
// Verify that the generated length has not already been calculated
|
||||
if (_generatedContentLength == 0)
|
||||
{
|
||||
_generatedContentLength = _writer.ToString().Length - _generatedLocation.AbsoluteIndex;
|
||||
_generatedContentLength = _writer.GenerateCode().Length - _generatedLocation.AbsoluteIndex;
|
||||
}
|
||||
|
||||
var generatedLocation = new MappingLocation(_generatedLocation, _generatedContentLength);
|
||||
|
|
@ -75,7 +75,7 @@ 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 endsWithNewline = _writer.ToString().EndsWith("\n");
|
||||
bool endsWithNewline = _writer.GenerateCode().EndsWith("\n");
|
||||
|
||||
// Always write at least 1 empty line to potentially separate code from pragmas.
|
||||
_writer.WriteLine();
|
||||
|
|
|
|||
|
|
@ -6,17 +6,10 @@ namespace Microsoft.AspNet.Razor.Generator.Compiler
|
|||
{
|
||||
public class CodeWriter : IDisposable
|
||||
{
|
||||
protected StringWriter Writer;
|
||||
|
||||
private StringWriter _writer = new StringWriter();
|
||||
private bool _newLine;
|
||||
private string _cache;
|
||||
private bool _dirty;
|
||||
|
||||
public CodeWriter()
|
||||
{
|
||||
Writer = new StringWriter();
|
||||
_dirty = true;
|
||||
}
|
||||
private string _cache = string.Empty;
|
||||
private bool _dirty = false;
|
||||
|
||||
public string LastWrite { get; private set; }
|
||||
public int CurrentIndent { get; private set; }
|
||||
|
|
@ -51,7 +44,7 @@ namespace Microsoft.AspNet.Razor.Generator.Compiler
|
|||
{
|
||||
if (_newLine)
|
||||
{
|
||||
Writer.Write(new string(' ', size));
|
||||
_writer.Write(new string(' ', size));
|
||||
Flush();
|
||||
_dirty = true;
|
||||
_newLine = false;
|
||||
|
|
@ -64,7 +57,7 @@ namespace Microsoft.AspNet.Razor.Generator.Compiler
|
|||
{
|
||||
Indent(CurrentIndent);
|
||||
|
||||
Writer.Write(data);
|
||||
_writer.Write(data);
|
||||
|
||||
Flush();
|
||||
|
||||
|
|
@ -79,7 +72,7 @@ namespace Microsoft.AspNet.Razor.Generator.Compiler
|
|||
{
|
||||
LastWrite = Environment.NewLine;
|
||||
|
||||
Writer.WriteLine();
|
||||
_writer.WriteLine();
|
||||
|
||||
Flush();
|
||||
|
||||
|
|
@ -96,16 +89,17 @@ namespace Microsoft.AspNet.Razor.Generator.Compiler
|
|||
|
||||
public CodeWriter Flush()
|
||||
{
|
||||
Writer.Flush();
|
||||
_writer.Flush();
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
public string GenerateCode()
|
||||
{
|
||||
if (_dirty)
|
||||
{
|
||||
_cache = Writer.ToString();
|
||||
_cache = _writer.ToString();
|
||||
_dirty = false;
|
||||
}
|
||||
|
||||
return _cache;
|
||||
|
|
@ -113,7 +107,7 @@ namespace Microsoft.AspNet.Razor.Generator.Compiler
|
|||
|
||||
public SourceLocation GetCurrentSourceLocation()
|
||||
{
|
||||
string output = ToString();
|
||||
string output = GenerateCode();
|
||||
string unescapedOutput = output.Replace("\\r", String.Empty).Replace("\\n", String.Empty);
|
||||
|
||||
return new SourceLocation(
|
||||
|
|
@ -126,7 +120,7 @@ namespace Microsoft.AspNet.Razor.Generator.Compiler
|
|||
{
|
||||
if(disposing)
|
||||
{
|
||||
Writer.Dispose();
|
||||
_writer.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue