public void Write(string textToAppend)
{
if (string.IsNullOrEmpty(textToAppend))
{
return;
}
// If we're starting off, or if the previous text ended with a newline,
// we have to append the current indent first.
if ((GenerationEnvironment.Length == 0)
|| endsWithNewline)
{
GenerationEnvironment.Append(CurrentIndent);
endsWithNewline = false;
}
// Check if the current text ends with a newline
if (textToAppend.EndsWith(Environment.NewLine, StringComparison.CurrentCulture))
{
endsWithNewline = true;
}
// This is an optimization. If the current indent is "", then we don't have to do any
// of the more complex stuff further down.
if (CurrentIndent.Length == 0)
{
GenerationEnvironment.Append(textToAppend);
return;
}
// Everywhere there is a newline in the text, add an indent after it
textToAppend = textToAppend.Replace(Environment.NewLine, Environment.NewLine + CurrentIndent);
// If the text ends with a newline, then we should strip off the indent added at the very end
// because the appropriate indent will be added when the next time Write() is called
if (endsWithNewline)
{
GenerationEnvironment.Append(textToAppend, 0, textToAppend.Length - CurrentIndent.Length);
}
else
{
GenerationEnvironment.Append(textToAppend);
}
}