Something went wrong. Try again.
.NET ATProtocol Library - Built for Source Gen
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168// <auto-generated />#nullable enable
using System;using System.Text;
namespace CarpaNet.Generation;
/// <summary>/// Helper class for building C# source code with proper indentation./// </summary>public sealed class SourceBuilder{ private readonly StringBuilder _sb = new(); private int _indentLevel; private const string IndentString = " ";
public SourceBuilder AppendLine() { _sb.AppendLine(); return this; }
public SourceBuilder AppendLine(string line) { if (string.IsNullOrEmpty(line)) { _sb.AppendLine(); } else { AppendIndent(); _sb.AppendLine(line); } return this; }
public SourceBuilder Append(string text) { _sb.Append(text); return this; }
public SourceBuilder AppendIndent() { for (int i = 0; i < _indentLevel; i++) { _sb.Append(IndentString); } return this; }
public SourceBuilder OpenBrace() { AppendLine("{"); _indentLevel++; return this; }
public SourceBuilder CloseBrace(bool withSemicolon = false) { _indentLevel--; AppendLine(withSemicolon ? "};" : "}"); return this; }
public SourceBuilder Indent() { _indentLevel++; return this; }
public SourceBuilder Unindent() { if (_indentLevel > 0) { _indentLevel--; } return this; }
/// <summary> /// Writes an XML documentation summary. /// </summary> public SourceBuilder WriteSummary(string? description) { if (string.IsNullOrWhiteSpace(description)) { return this; }
AppendLine("/// <summary>");
// Split description into lines and escape XML var lines = description!.Replace("\r\n", "\n").Split('\n'); foreach (var line in lines) { AppendLine($"/// {EscapeXml(line)}"); }
AppendLine("/// </summary>"); return this; }
/// <summary> /// Writes a using directive. /// </summary> public SourceBuilder WriteUsing(string ns) { AppendLine($"using {ns};"); return this; }
/// <summary> /// Writes a namespace declaration with file-scoped syntax. /// </summary> public SourceBuilder WriteNamespace(string ns) { AppendLine($"namespace {ns};"); AppendLine(); return this; }
/// <summary> /// Writes a namespace declaration with block syntax. /// </summary> public SourceBuilder WriteNamespaceBlock(string ns) { AppendLine($"namespace {ns}"); OpenBrace(); return this; }
/// <summary> /// Writes an attribute. /// </summary> public SourceBuilder WriteAttribute(string attribute) { AppendLine($"[{attribute}]"); return this; }
/// <summary> /// Writes the file header. /// </summary> public SourceBuilder WriteHeader() { AppendLine("// <auto-generated />"); AppendLine("#nullable enable"); AppendLine(); return this; }
private static string EscapeXml(string text) { return text .Replace("&", "&") .Replace("<", "<") .Replace(">", ">") .Replace("\"", """) .Replace("'", "'"); }
public override string ToString() => _sb.ToString();
public int Length => _sb.Length;}