2020-02-24 22:45:51 +01:00
|
|
|
|
using System;
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
|
|
namespace LibHacBuild.CodeGen
|
|
|
|
|
{
|
|
|
|
|
public class IndentingStringBuilder
|
|
|
|
|
{
|
|
|
|
|
public int LevelSize { get; set; } = 4;
|
|
|
|
|
public int Level { get; private set; }
|
|
|
|
|
|
|
|
|
|
private StringBuilder _sb = new StringBuilder();
|
|
|
|
|
private string _indentation = string.Empty;
|
|
|
|
|
private bool _hasIndentedCurrentLine;
|
2020-02-26 08:55:53 +01:00
|
|
|
|
private bool _lastLineWasEmpty;
|
2020-02-24 22:45:51 +01:00
|
|
|
|
|
|
|
|
|
public IndentingStringBuilder() { }
|
|
|
|
|
public IndentingStringBuilder(int levelSize) => LevelSize = levelSize;
|
|
|
|
|
|
|
|
|
|
public void SetLevel(int level)
|
|
|
|
|
{
|
|
|
|
|
Level = Math.Max(level, 0);
|
|
|
|
|
_indentation = new string(' ', Level * LevelSize);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void IncreaseLevel() => SetLevel(Level + 1);
|
|
|
|
|
public void DecreaseLevel() => SetLevel(Level - 1);
|
|
|
|
|
|
|
|
|
|
public IndentingStringBuilder AppendLine()
|
|
|
|
|
{
|
|
|
|
|
_sb.AppendLine();
|
|
|
|
|
_hasIndentedCurrentLine = false;
|
2020-02-26 08:55:53 +01:00
|
|
|
|
_lastLineWasEmpty = true;
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IndentingStringBuilder AppendSpacerLine()
|
|
|
|
|
{
|
|
|
|
|
if (!_lastLineWasEmpty)
|
|
|
|
|
{
|
|
|
|
|
_sb.AppendLine();
|
|
|
|
|
_hasIndentedCurrentLine = false;
|
|
|
|
|
_lastLineWasEmpty = true;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-24 22:45:51 +01:00
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IndentingStringBuilder AppendLine(string value)
|
|
|
|
|
{
|
|
|
|
|
IndentIfNeeded();
|
|
|
|
|
_sb.AppendLine(value);
|
|
|
|
|
_hasIndentedCurrentLine = false;
|
2020-02-26 08:55:53 +01:00
|
|
|
|
_lastLineWasEmpty = string.IsNullOrWhiteSpace(value);
|
2020-02-24 22:45:51 +01:00
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IndentingStringBuilder Append(string value)
|
|
|
|
|
{
|
|
|
|
|
IndentIfNeeded();
|
|
|
|
|
_sb.Append(value);
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IndentingStringBuilder AppendLineAndIncrease(string value)
|
|
|
|
|
{
|
|
|
|
|
AppendLine(value);
|
|
|
|
|
IncreaseLevel();
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IndentingStringBuilder DecreaseAndAppendLine(string value)
|
|
|
|
|
{
|
|
|
|
|
DecreaseLevel();
|
|
|
|
|
AppendLine(value);
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void IndentIfNeeded()
|
|
|
|
|
{
|
|
|
|
|
if (!_hasIndentedCurrentLine)
|
|
|
|
|
{
|
|
|
|
|
_sb.Append(_indentation);
|
|
|
|
|
_hasIndentedCurrentLine = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override string ToString() => _sb.ToString();
|
|
|
|
|
}
|
|
|
|
|
}
|