< Summary

Information
Class: Hertzole.ScriptableValues.Generator.CodeWriter
Assembly: Hertzole.ScriptableValues.Generator
File(s): Generator/Hertzole.ScriptableValues.Generator/CodeWriter.cs
Tag: 52_28197466029
Line coverage
98%
Covered lines: 83
Uncovered lines: 1
Coverable lines: 84
Total lines: 187
Line coverage: 98.8%
Branch coverage
92%
Covered branches: 13
Total branches: 14
Branch coverage: 92.8%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor()100%11100%
Append(...)100%11100%
Append(...)100%11100%
Append(...)100%11100%
Append(...)100%11100%
AppendLine(...)100%11100%
AppendLine(...)100%11100%
AppendLine()100%11100%
AppendGeneratedCodeAttribute(...)100%11100%
AppendExcludeFromCodeCoverageAttribute(...)100%11100%
AppendWithinIfDefines(...)100%44100%
AppendFileHeader()100%11100%
AppendNamespace(...)75%4492.3%
Clear()100%11100%
WriteIndentIfNeeded()100%22100%
WithIndent(...)100%11100%
ToString()100%44100%
.ctor(...)100%11100%
Dispose()100%11100%

File(s)

Generator/Hertzole.ScriptableValues.Generator/CodeWriter.cs

#LineLine coverage
 1using System;
 2using System.Text;
 3
 4namespace Hertzole.ScriptableValues.Generator;
 5
 6public sealed class CodeWriter
 7{
 468    private readonly StringBuilder sb = new StringBuilder(1024);
 9
 10    private bool shouldWriteIndent = false;
 11    private bool hasNamespace = false;
 12    private bool hasWrittenNamespace = false;
 13
 340714    public int Indent { get; set; }
 15
 16    public void Append(string value)
 17    {
 239218        WriteIndentIfNeeded();
 19
 239220        sb.Append(value);
 239221    }
 22
 23    public void Append(char value)
 24    {
 21625        WriteIndentIfNeeded();
 26
 21627        sb.Append(value);
 21628    }
 29
 30    public void Append(ReadOnlySpan<char> value)
 31    {
 2432        WriteIndentIfNeeded();
 33
 2434        sb.Append(value.ToString());
 2435    }
 36
 37    public void Append(int value)
 38    {
 31939        WriteIndentIfNeeded();
 40
 31941        sb.Append(value);
 31942    }
 43
 44    public void AppendLine(string value)
 45    {
 164046        WriteIndentIfNeeded();
 47
 164048        sb.AppendLine(value);
 164049        shouldWriteIndent = true;
 164050    }
 51
 52    public void AppendLine(ReadOnlySpan<char> value)
 53    {
 2454        AppendLine(value.ToString());
 2455    }
 56
 57    public void AppendLine()
 58    {
 14259        sb.AppendLine();
 14260        shouldWriteIndent = true;
 14261    }
 62
 63    public void AppendGeneratedCodeAttribute(string generator, string version)
 64    {
 9665        WriteIndentIfNeeded();
 66
 9667        sb.Append("[global::System.CodeDom.Compiler.GeneratedCode(\"");
 9668        sb.Append(generator);
 9669        sb.Append("\", \"");
 9670        sb.Append(version);
 9671        sb.Append("\")]\n");
 9672        shouldWriteIndent = true;
 9673    }
 74
 75    public void AppendExcludeFromCodeCoverageAttribute(bool withIfDefines = true)
 76    {
 9677        AppendWithinIfDefines(withIfDefines, static s => s.Append("[global::System.Diagnostics.CodeAnalysis.ExcludeFromC
 4878    }
 79
 80    private void AppendWithinIfDefines(bool withIfDefines, Action<StringBuilder> writeAction)
 81    {
 4882        if (withIfDefines)
 83        {
 4884            using (WithIndent(0))
 85            {
 4886                sb.AppendLine("#if UNITY_INCLUDE_TESTS");
 4887            }
 88        }
 89
 4890        WriteIndentIfNeeded();
 4891        writeAction.Invoke(sb);
 92
 4893        if (withIfDefines)
 94        {
 4895            using (WithIndent(0))
 96            {
 4897                sb.AppendLine("#endif // UNITY_INCLUDE_TESTS");
 4898            }
 99        }
 100
 48101        shouldWriteIndent = true;
 48102    }
 103
 104    public void AppendFileHeader()
 105    {
 24106        sb.AppendLine("// <auto-generated/>");
 24107        sb.AppendLine("#nullable enable");
 24108    }
 109
 110    public void AppendNamespace(string? nspace)
 111    {
 24112        if (string.IsNullOrEmpty(nspace))
 113        {
 12114            hasNamespace = false;
 12115            return;
 116        }
 117
 12118        if (hasNamespace)
 119        {
 0120            return;
 121        }
 122
 12123        hasNamespace = true;
 12124        hasWrittenNamespace = false;
 12125        sb.Append("namespace ");
 12126        sb.AppendLine(nspace);
 12127        sb.AppendLine("{");
 12128        Indent++;
 12129        shouldWriteIndent = true;
 12130    }
 131
 132    public void Clear()
 133    {
 11134        Indent = 0;
 11135        shouldWriteIndent = false;
 11136        sb.Clear();
 11137    }
 138
 139    private void WriteIndentIfNeeded()
 140    {
 4735141        if (!shouldWriteIndent)
 142        {
 2915143            return;
 144        }
 145
 1820146        shouldWriteIndent = false;
 1820147        sb.Append('\t', Indent);
 1820148    }
 149
 150    public IndentScope WithIndent(int indent)
 151    {
 288152        return new IndentScope(this, indent);
 153    }
 154
 155    public override string ToString()
 156    {
 46157        if (hasNamespace && !hasWrittenNamespace)
 158        {
 12159            sb.AppendLine();
 12160            Indent--;
 12161            sb.Append("}");
 162
 12163            hasWrittenNamespace = true;
 12164            hasNamespace = false;
 165        }
 166
 46167        return sb.ToString();
 168    }
 169
 170    public readonly ref struct IndentScope
 171    {
 172        private readonly CodeWriter writer;
 173        private readonly int originalIndent;
 174
 175        public IndentScope(CodeWriter writer, int newIndent)
 176        {
 288177            this.writer = writer;
 288178            originalIndent = writer.Indent;
 288179            writer.Indent = newIndent;
 288180        }
 181
 182        public void Dispose()
 183        {
 288184            writer.Indent = originalIndent;
 288185        }
 186    }
 187}