|
| 1 | +using System.Runtime.CompilerServices; |
| 2 | + |
| 3 | +#pragma warning disable CS9113 // Parameter is unread. |
| 4 | +#pragma warning disable IDE0060 // Remove unused parameter |
| 5 | + |
| 6 | +internal partial class CodeBuilder |
| 7 | +{ |
| 8 | + public CodeBuilder Append( |
| 9 | + [InterpolatedStringHandlerArgument("")] |
| 10 | + ref CodeBuilderInterpolatedStringHandler builder) => this; |
| 11 | + |
| 12 | + public CodeBuilder Append(bool enabled, |
| 13 | + [InterpolatedStringHandlerArgument("", nameof(enabled))] |
| 14 | + ref ConditionalCodeBuilderInterpolatedStringHandler builder) => this; |
| 15 | + |
| 16 | + public CodeBuilder AppendLineRaw( |
| 17 | + [InterpolatedStringHandlerArgument("")] |
| 18 | + ref CodeBuilderInterpolatedStringHandler builder) => AppendLine(); |
| 19 | + |
| 20 | + public CodeBuilder AppendLineRaw(bool enabled, |
| 21 | + [InterpolatedStringHandlerArgument("", nameof(enabled))] |
| 22 | + ref ConditionalCodeBuilderInterpolatedStringHandler builder) => enabled ? AppendLine() : this; |
| 23 | + |
| 24 | + public CodeBuilder AppendLine( |
| 25 | + [InterpolatedStringHandlerArgument("")] |
| 26 | + IndentedCodeBuilderInterpolatedStringHandler builder) => AppendLine(); |
| 27 | + |
| 28 | + public CodeBuilder AppendLine(bool enabled, |
| 29 | + [InterpolatedStringHandlerArgument("", nameof(enabled))] |
| 30 | + ConditionalIndentedCodeBuilderInterpolatedStringHandler builder) => enabled ? AppendLine() : this; |
| 31 | + |
| 32 | + private void AppendFormatted(IEnumerable<string> items, string? format) |
| 33 | + { |
| 34 | + if (format == "comma") |
| 35 | + AppendCommaSeparated(items); |
| 36 | + |
| 37 | + if (format == "commaindent") |
| 38 | + AppendCommaIndented(items); |
| 39 | + } |
| 40 | + |
| 41 | + private void AppendCommaSeparated(IEnumerable<string> items) |
| 42 | + { |
| 43 | + var comma = false; |
| 44 | + foreach (var item in items) |
| 45 | + { |
| 46 | + if (comma) |
| 47 | + Append(", "); |
| 48 | + comma = true; |
| 49 | + Append(item); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + private void AppendCommaIndented(IEnumerable<string> items) |
| 54 | + { |
| 55 | + var count = items.Count(); |
| 56 | + |
| 57 | + if (count < 3) |
| 58 | + { |
| 59 | + AppendCommaSeparated(items); |
| 60 | + return; |
| 61 | + } |
| 62 | + |
| 63 | + AppendLine(); |
| 64 | + IncreaseIndent(); |
| 65 | + var comma = false; |
| 66 | + foreach (var item in items) |
| 67 | + { |
| 68 | + if (comma) |
| 69 | + Append(",").AppendLine(); |
| 70 | + comma = true; |
| 71 | + AppendIndent().Append(item); |
| 72 | + } |
| 73 | + AppendLine(); |
| 74 | + DecreaseIndent(); |
| 75 | + AppendIndent(); |
| 76 | + } |
| 77 | + |
| 78 | + [InterpolatedStringHandler] |
| 79 | + internal readonly struct CodeBuilderInterpolatedStringHandler( |
| 80 | + int literalLength, int formattedCount, CodeBuilder codeBuilder) |
| 81 | + { |
| 82 | + public readonly void AppendLiteral(string s) => codeBuilder.Append(s); |
| 83 | + public readonly void AppendFormatted(string s) => codeBuilder.Append(s); |
| 84 | + public readonly void AppendFormatted(IEnumerable<string> items, string? format) |
| 85 | + => codeBuilder.AppendFormatted(items, format); |
| 86 | + } |
| 87 | + |
| 88 | + [InterpolatedStringHandler] |
| 89 | + internal readonly struct ConditionalCodeBuilderInterpolatedStringHandler( |
| 90 | + int literalLength, int formattedCount, CodeBuilder codeBuilder, bool enabled) |
| 91 | + { |
| 92 | + public readonly bool AppendLiteral(string s) { if (enabled) codeBuilder.Append(s); return enabled; } |
| 93 | + public readonly bool AppendFormatted(string s) { if (enabled) codeBuilder.Append(s); return enabled; } |
| 94 | + public readonly bool AppendFormatted(IEnumerable<string> items, string? format) |
| 95 | + { if (enabled) codeBuilder.AppendFormatted(items, format); return enabled; } |
| 96 | + } |
| 97 | + |
| 98 | + [InterpolatedStringHandler] |
| 99 | + internal class IndentedCodeBuilderInterpolatedStringHandler( |
| 100 | + int literalLength, int formattedCount, CodeBuilder codeBuilder) |
| 101 | + { |
| 102 | + private bool _hasIndented; |
| 103 | + |
| 104 | + private void EnsureIndent() |
| 105 | + { |
| 106 | + if (_hasIndented) return; |
| 107 | + codeBuilder.AppendIndent(); |
| 108 | + _hasIndented = true; |
| 109 | + } |
| 110 | + |
| 111 | + public void AppendLiteral(string s) { EnsureIndent(); codeBuilder.Append(s); } |
| 112 | + public void AppendFormatted(string s) { EnsureIndent(); codeBuilder.Append(s); } |
| 113 | + public void AppendFormatted(IEnumerable<string> items, string? format) |
| 114 | + { EnsureIndent(); codeBuilder.AppendFormatted(items, format); } |
| 115 | + } |
| 116 | + |
| 117 | + [InterpolatedStringHandler] |
| 118 | + internal class ConditionalIndentedCodeBuilderInterpolatedStringHandler( |
| 119 | + int literalLength, int formattedCount, CodeBuilder codeBuilder, bool enabled) |
| 120 | + : IndentedCodeBuilderInterpolatedStringHandler(literalLength, formattedCount, codeBuilder) |
| 121 | + { |
| 122 | + public new bool AppendLiteral(string s) { if (enabled) base.AppendLiteral(s); return enabled; } |
| 123 | + public new bool AppendFormatted(string s) { if (enabled) base.AppendFormatted(s); return enabled; } |
| 124 | + public new bool AppendFormatted(IEnumerable<string> items, string? format) |
| 125 | + { if (enabled) base.AppendFormatted(items, format); return enabled; } |
| 126 | + } |
| 127 | +} |
0 commit comments