|
| 1 | +namespace NetEvolve.CodeBuilder; |
| 2 | + |
| 3 | +#if NET6_0_OR_GREATER |
| 4 | +using System.Globalization; |
| 5 | +using System.Runtime.CompilerServices; |
| 6 | + |
| 7 | +public partial class CSharpCodeBuilder |
| 8 | +{ |
| 9 | + /// <summary> |
| 10 | + /// Appends an interpolated string to the current builder. |
| 11 | + /// </summary> |
| 12 | + /// <param name="handler">The interpolated string handler built by the compiler.</param> |
| 13 | + /// <returns>The current <see cref="CSharpCodeBuilder"/> instance to allow for method chaining.</returns> |
| 14 | + /// <example> |
| 15 | + /// <code> |
| 16 | + /// builder.AppendInterpolated($"public {typeName} {memberName}"); |
| 17 | + /// </code> |
| 18 | + /// </example> |
| 19 | + public CSharpCodeBuilder AppendInterpolated( |
| 20 | + [InterpolatedStringHandlerArgument("")] ref CSharpInterpolatedStringHandler handler |
| 21 | + ) => this; |
| 22 | + |
| 23 | + /// <summary> |
| 24 | + /// Appends an interpolated string followed by a line terminator to the current builder. |
| 25 | + /// </summary> |
| 26 | + /// <param name="handler">The interpolated string handler built by the compiler.</param> |
| 27 | + /// <returns>The current <see cref="CSharpCodeBuilder"/> instance to allow for method chaining.</returns> |
| 28 | + /// <example> |
| 29 | + /// <code> |
| 30 | + /// builder.AppendLineInterpolated($"public class {className}"); |
| 31 | + /// </code> |
| 32 | + /// </example> |
| 33 | + public CSharpCodeBuilder AppendLineInterpolated( |
| 34 | + [InterpolatedStringHandlerArgument("")] ref CSharpInterpolatedStringHandler handler |
| 35 | + ) => AppendLine(); |
| 36 | + |
| 37 | + internal void HandlerEnsureIndented() => EnsureIndented(); |
| 38 | + |
| 39 | + internal void HandlerRawAppend(string? value) |
| 40 | + { |
| 41 | + if (!string.IsNullOrEmpty(value)) |
| 42 | + { |
| 43 | + _ = _builder.Append(value); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + internal void HandlerRawAppend(ReadOnlySpan<char> value) |
| 48 | + { |
| 49 | + if (!value.IsEmpty) |
| 50 | + { |
| 51 | + _ = _builder.Append(value); |
| 52 | + } |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +/// <summary> |
| 57 | +/// Custom interpolated string handler for <see cref="CSharpCodeBuilder"/>. |
| 58 | +/// </summary> |
| 59 | +/// <remarks> |
| 60 | +/// This handler is instantiated by the compiler when an interpolated string is passed to |
| 61 | +/// <see cref="CSharpCodeBuilder.AppendInterpolated"/> or <see cref="CSharpCodeBuilder.AppendLineInterpolated"/>. |
| 62 | +/// It appends each literal and formatted part directly to the builder, applying indentation before |
| 63 | +/// the first non-empty part on a new line. |
| 64 | +/// </remarks> |
| 65 | +[InterpolatedStringHandler] |
| 66 | +public ref struct CSharpInterpolatedStringHandler |
| 67 | +{ |
| 68 | + private readonly CSharpCodeBuilder _owner; |
| 69 | + private bool _indentEnsured; |
| 70 | + |
| 71 | + /// <summary> |
| 72 | + /// Initializes a new instance of the <see cref="CSharpInterpolatedStringHandler"/> struct. |
| 73 | + /// </summary> |
| 74 | + /// <param name="literalLength">The total length of all literal parts (hint for capacity).</param> |
| 75 | + /// <param name="formattedCount">The number of formatted holes in the interpolated string.</param> |
| 76 | + /// <param name="builder">The <see cref="CSharpCodeBuilder"/> to append to.</param> |
| 77 | + public CSharpInterpolatedStringHandler(int literalLength, int formattedCount, CSharpCodeBuilder builder) |
| 78 | + { |
| 79 | + _owner = builder; |
| 80 | + _indentEnsured = false; |
| 81 | + } |
| 82 | + |
| 83 | + private void EnsureIndented() |
| 84 | + { |
| 85 | + if (!_indentEnsured) |
| 86 | + { |
| 87 | + _owner.HandlerEnsureIndented(); |
| 88 | + _indentEnsured = true; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + /// <summary>Appends a literal string part of the interpolated string.</summary> |
| 93 | + /// <param name="value">The literal string to append.</param> |
| 94 | + public void AppendLiteral(string? value) |
| 95 | + { |
| 96 | + if (string.IsNullOrEmpty(value)) |
| 97 | + { |
| 98 | + return; |
| 99 | + } |
| 100 | + |
| 101 | + EnsureIndented(); |
| 102 | + _owner.HandlerRawAppend(value); |
| 103 | + } |
| 104 | + |
| 105 | + /// <summary>Appends a formatted value from the interpolated string.</summary> |
| 106 | + /// <typeparam name="T">The type of the value to format.</typeparam> |
| 107 | + /// <param name="value">The value to append.</param> |
| 108 | + public void AppendFormatted<T>(T value) |
| 109 | + { |
| 110 | + var str = value?.ToString(); |
| 111 | + if (string.IsNullOrEmpty(str)) |
| 112 | + { |
| 113 | + return; |
| 114 | + } |
| 115 | + |
| 116 | + EnsureIndented(); |
| 117 | + _owner.HandlerRawAppend(str); |
| 118 | + } |
| 119 | + |
| 120 | + /// <summary>Appends a formatted value with a format string from the interpolated string.</summary> |
| 121 | + /// <typeparam name="T">The type of the value to format. Must implement <see cref="System.IFormattable"/>.</typeparam> |
| 122 | + /// <param name="value">The value to append.</param> |
| 123 | + /// <param name="format">The format string.</param> |
| 124 | + public void AppendFormatted<T>(T value, string? format) |
| 125 | + where T : System.IFormattable |
| 126 | + { |
| 127 | + var str = value?.ToString(format, CultureInfo.InvariantCulture); |
| 128 | + if (string.IsNullOrEmpty(str)) |
| 129 | + { |
| 130 | + return; |
| 131 | + } |
| 132 | + |
| 133 | + EnsureIndented(); |
| 134 | + _owner.HandlerRawAppend(str); |
| 135 | + } |
| 136 | + |
| 137 | + /// <summary>Appends a formatted value with alignment from the interpolated string.</summary> |
| 138 | + /// <typeparam name="T">The type of the value to format.</typeparam> |
| 139 | + /// <param name="value">The value to append.</param> |
| 140 | + /// <param name="alignment">Minimum width; negative values left-align.</param> |
| 141 | + public void AppendFormatted<T>(T value, int alignment) |
| 142 | + { |
| 143 | + var str = value?.ToString(); |
| 144 | + if (str is null) |
| 145 | + { |
| 146 | + return; |
| 147 | + } |
| 148 | + |
| 149 | + str = alignment >= 0 ? str.PadLeft(alignment) : str.PadRight(-alignment); |
| 150 | + EnsureIndented(); |
| 151 | + _owner.HandlerRawAppend(str); |
| 152 | + } |
| 153 | + |
| 154 | + /// <summary>Appends a formatted value with alignment and format string from the interpolated string.</summary> |
| 155 | + /// <typeparam name="T">The type of the value to format. Must implement <see cref="System.IFormattable"/>.</typeparam> |
| 156 | + /// <param name="value">The value to append.</param> |
| 157 | + /// <param name="alignment">Minimum width; negative values left-align.</param> |
| 158 | + /// <param name="format">The format string.</param> |
| 159 | + public void AppendFormatted<T>(T value, int alignment, string? format) |
| 160 | + where T : System.IFormattable |
| 161 | + { |
| 162 | + var str = value?.ToString(format, CultureInfo.InvariantCulture) ?? string.Empty; |
| 163 | + str = alignment >= 0 ? str.PadLeft(alignment) : str.PadRight(-alignment); |
| 164 | + EnsureIndented(); |
| 165 | + _owner.HandlerRawAppend(str); |
| 166 | + } |
| 167 | + |
| 168 | + /// <summary>Appends a string value from the interpolated string.</summary> |
| 169 | + /// <param name="value">The string to append.</param> |
| 170 | + public void AppendFormatted(string? value) |
| 171 | + { |
| 172 | + if (string.IsNullOrEmpty(value)) |
| 173 | + { |
| 174 | + return; |
| 175 | + } |
| 176 | + |
| 177 | + EnsureIndented(); |
| 178 | + _owner.HandlerRawAppend(value); |
| 179 | + } |
| 180 | + |
| 181 | + /// <summary>Appends a <see cref="ReadOnlySpan{T}"/> value from the interpolated string.</summary> |
| 182 | + /// <param name="value">The span to append.</param> |
| 183 | + public void AppendFormatted(ReadOnlySpan<char> value) |
| 184 | + { |
| 185 | + if (value.IsEmpty) |
| 186 | + { |
| 187 | + return; |
| 188 | + } |
| 189 | + |
| 190 | + EnsureIndented(); |
| 191 | + _owner.HandlerRawAppend(value); |
| 192 | + } |
| 193 | +} |
| 194 | +#endif |
0 commit comments