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)
|
private void AddImports(CodeTree codeTree, CSharpCodeWriter writer, IEnumerable<string> defaultImports)
|
||||||
|
|
|
||||||
|
|
@ -48,7 +48,7 @@ namespace Microsoft.AspNet.Razor.Generator.Compiler.CSharp
|
||||||
|
|
||||||
public void MarkLineMappingEnd()
|
public void MarkLineMappingEnd()
|
||||||
{
|
{
|
||||||
_generatedContentLength = _writer.ToString().Length - _generatedLocation.AbsoluteIndex;
|
_generatedContentLength = _writer.GenerateCode().Length - _generatedLocation.AbsoluteIndex;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected virtual void Dispose(bool disposing)
|
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
|
// Verify that the generated length has not already been calculated
|
||||||
if (_generatedContentLength == 0)
|
if (_generatedContentLength == 0)
|
||||||
{
|
{
|
||||||
_generatedContentLength = _writer.ToString().Length - _generatedLocation.AbsoluteIndex;
|
_generatedContentLength = _writer.GenerateCode().Length - _generatedLocation.AbsoluteIndex;
|
||||||
}
|
}
|
||||||
|
|
||||||
var generatedLocation = new MappingLocation(_generatedLocation, _generatedContentLength);
|
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.
|
// 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 endsWithNewline = _writer.ToString().EndsWith("\n");
|
bool endsWithNewline = _writer.GenerateCode().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();
|
||||||
|
|
|
||||||
|
|
@ -6,17 +6,10 @@ namespace Microsoft.AspNet.Razor.Generator.Compiler
|
||||||
{
|
{
|
||||||
public class CodeWriter : IDisposable
|
public class CodeWriter : IDisposable
|
||||||
{
|
{
|
||||||
protected StringWriter Writer;
|
private StringWriter _writer = new StringWriter();
|
||||||
|
|
||||||
private bool _newLine;
|
private bool _newLine;
|
||||||
private string _cache;
|
private string _cache = string.Empty;
|
||||||
private bool _dirty;
|
private bool _dirty = false;
|
||||||
|
|
||||||
public CodeWriter()
|
|
||||||
{
|
|
||||||
Writer = new StringWriter();
|
|
||||||
_dirty = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public string LastWrite { get; private set; }
|
public string LastWrite { get; private set; }
|
||||||
public int CurrentIndent { get; private set; }
|
public int CurrentIndent { get; private set; }
|
||||||
|
|
@ -51,7 +44,7 @@ namespace Microsoft.AspNet.Razor.Generator.Compiler
|
||||||
{
|
{
|
||||||
if (_newLine)
|
if (_newLine)
|
||||||
{
|
{
|
||||||
Writer.Write(new string(' ', size));
|
_writer.Write(new string(' ', size));
|
||||||
Flush();
|
Flush();
|
||||||
_dirty = true;
|
_dirty = true;
|
||||||
_newLine = false;
|
_newLine = false;
|
||||||
|
|
@ -64,7 +57,7 @@ namespace Microsoft.AspNet.Razor.Generator.Compiler
|
||||||
{
|
{
|
||||||
Indent(CurrentIndent);
|
Indent(CurrentIndent);
|
||||||
|
|
||||||
Writer.Write(data);
|
_writer.Write(data);
|
||||||
|
|
||||||
Flush();
|
Flush();
|
||||||
|
|
||||||
|
|
@ -79,7 +72,7 @@ namespace Microsoft.AspNet.Razor.Generator.Compiler
|
||||||
{
|
{
|
||||||
LastWrite = Environment.NewLine;
|
LastWrite = Environment.NewLine;
|
||||||
|
|
||||||
Writer.WriteLine();
|
_writer.WriteLine();
|
||||||
|
|
||||||
Flush();
|
Flush();
|
||||||
|
|
||||||
|
|
@ -96,16 +89,17 @@ namespace Microsoft.AspNet.Razor.Generator.Compiler
|
||||||
|
|
||||||
public CodeWriter Flush()
|
public CodeWriter Flush()
|
||||||
{
|
{
|
||||||
Writer.Flush();
|
_writer.Flush();
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override string ToString()
|
public string GenerateCode()
|
||||||
{
|
{
|
||||||
if (_dirty)
|
if (_dirty)
|
||||||
{
|
{
|
||||||
_cache = Writer.ToString();
|
_cache = _writer.ToString();
|
||||||
|
_dirty = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return _cache;
|
return _cache;
|
||||||
|
|
@ -113,7 +107,7 @@ namespace Microsoft.AspNet.Razor.Generator.Compiler
|
||||||
|
|
||||||
public SourceLocation GetCurrentSourceLocation()
|
public SourceLocation GetCurrentSourceLocation()
|
||||||
{
|
{
|
||||||
string output = ToString();
|
string output = GenerateCode();
|
||||||
string unescapedOutput = output.Replace("\\r", String.Empty).Replace("\\n", String.Empty);
|
string unescapedOutput = output.Replace("\\r", String.Empty).Replace("\\n", String.Empty);
|
||||||
|
|
||||||
return new SourceLocation(
|
return new SourceLocation(
|
||||||
|
|
@ -126,7 +120,7 @@ namespace Microsoft.AspNet.Razor.Generator.Compiler
|
||||||
{
|
{
|
||||||
if(disposing)
|
if(disposing)
|
||||||
{
|
{
|
||||||
Writer.Dispose();
|
_writer.Dispose();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue