mirror of https://github.com/mono/CppSharp.git
c-sharpdotnetmonobindingsbridgecclangcpluspluscppsharpglueinteropparserparsingpinvokeswigsyntax-treevisitorsxamarinxamarin-bindings
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
97 lines
2.1 KiB
97 lines
2.1 KiB
using System.Collections.Generic; |
|
using System.Linq; |
|
using System.Text; |
|
|
|
namespace Cxxi |
|
{ |
|
public class TextGenerator |
|
{ |
|
private const uint DefaultIndent = 4; |
|
private const uint MaxIndent = 80; |
|
|
|
private readonly StringBuilder sb; |
|
private readonly Stack<uint> currentIndent; |
|
private bool isStartOfLine; |
|
private bool needsNewLine; |
|
|
|
public TextGenerator() |
|
{ |
|
sb = new StringBuilder(); |
|
currentIndent = new Stack<uint>(); |
|
isStartOfLine = false; |
|
} |
|
|
|
public void Write(string msg, params object[] args) |
|
{ |
|
if (isStartOfLine) |
|
sb.Append(new string(' ', (int)currentIndent.Sum(u => u))); |
|
|
|
if (args.Length > 0) |
|
msg = string.Format(msg, args); |
|
|
|
if (msg.Length > 0) |
|
isStartOfLine = false; |
|
|
|
sb.Append(msg); |
|
} |
|
|
|
public void WriteLine(string msg, params object[] args) |
|
{ |
|
Write(msg, args); |
|
NewLine(); |
|
} |
|
|
|
public void WriteLineIndent(string msg, params object[] args) |
|
{ |
|
PushIndent(); |
|
WriteLine(msg, args); |
|
PopIndent(); |
|
} |
|
|
|
public void NewLine() |
|
{ |
|
sb.AppendLine(string.Empty); |
|
isStartOfLine = true; |
|
} |
|
|
|
public void NewLineIfNeeded() |
|
{ |
|
if (!needsNewLine) return; |
|
|
|
NewLine(); |
|
needsNewLine = false; |
|
} |
|
|
|
public void NeedNewLine() |
|
{ |
|
needsNewLine = true; |
|
} |
|
|
|
public void PushIndent(uint indent = DefaultIndent) |
|
{ |
|
currentIndent.Push(indent); |
|
} |
|
|
|
public void PopIndent() |
|
{ |
|
currentIndent.Pop(); |
|
} |
|
|
|
public void WriteStartBraceIndent() |
|
{ |
|
WriteLine("{"); |
|
PushIndent(); |
|
} |
|
|
|
public void WriteCloseBraceIndent() |
|
{ |
|
PopIndent(); |
|
WriteLine("}"); |
|
} |
|
|
|
public override string ToString() |
|
{ |
|
return sb.ToString(); |
|
} |
|
} |
|
}
|
|
|