-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCSharpInterpolatedStringHandler.cs
More file actions
132 lines (117 loc) · 4.71 KB
/
Copy pathCSharpInterpolatedStringHandler.cs
File metadata and controls
132 lines (117 loc) · 4.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#if NET6_0_OR_GREATER
namespace NetEvolve.CodeBuilder;
using System.Globalization;
using System.Runtime.CompilerServices;
/// <summary>
/// Custom interpolated string handler for <see cref="CSharpCodeBuilder"/>.
/// </summary>
/// <remarks>
/// This handler is instantiated by the compiler when an interpolated string is passed to
/// <see cref="CSharpCodeBuilder.AppendInterpolated"/> or <see cref="CSharpCodeBuilder.AppendLineInterpolated"/>.
/// It appends each literal and formatted part directly to the builder, applying indentation before
/// the first non-empty part on a new line.
/// </remarks>
[InterpolatedStringHandler]
public ref struct CSharpInterpolatedStringHandler
{
private readonly CSharpCodeBuilder _owner;
private bool _indentEnsured;
/// <summary>
/// Initializes a new instance of the <see cref="CSharpInterpolatedStringHandler"/> struct.
/// </summary>
/// <param name="literalLength">The total length of all literal parts (hint for capacity).</param>
/// <param name="formattedCount">The number of formatted holes in the interpolated string.</param>
/// <param name="builder">The <see cref="CSharpCodeBuilder"/> to append to.</param>
public CSharpInterpolatedStringHandler(int literalLength, int formattedCount, CSharpCodeBuilder builder)
{
_owner = builder;
_indentEnsured = false;
}
private void EnsureIndented()
{
if (!_indentEnsured)
{
_owner.HandlerEnsureIndented();
_indentEnsured = true;
}
}
/// <summary>Appends a literal string part of the interpolated string.</summary>
/// <param name="value">The literal string to append.</param>
public void AppendLiteral(string? value)
{
if (string.IsNullOrEmpty(value))
{
return;
}
EnsureIndented();
_owner.HandlerRawAppend(value);
}
/// <summary>Appends a formatted value from the interpolated string.</summary>
/// <typeparam name="T">The type of the value to format.</typeparam>
/// <param name="value">The value to append.</param>
public void AppendFormatted<T>(T value)
{
var str = value?.ToString();
if (string.IsNullOrEmpty(str))
{
return;
}
EnsureIndented();
_owner.HandlerRawAppend(str);
}
/// <summary>Appends a formatted value with a format string from the interpolated string.</summary>
/// <typeparam name="T">The type of the value to format. Must implement <see cref="System.IFormattable"/>.</typeparam>
/// <param name="value">The value to append.</param>
/// <param name="format">The format string.</param>
public void AppendFormatted<T>(T value, string? format)
where T : System.IFormattable
{
var str = value?.ToString(format, CultureInfo.InvariantCulture);
if (string.IsNullOrEmpty(str))
{
return;
}
EnsureIndented();
_owner.HandlerRawAppend(str);
}
/// <summary>Appends a formatted value with alignment from the interpolated string.</summary>
/// <typeparam name="T">The type of the value to format.</typeparam>
/// <param name="value">The value to append.</param>
/// <param name="alignment">Minimum width; negative values left-align.</param>
public void AppendFormatted<T>(T value, int alignment)
{
var str = value?.ToString();
if (str is null)
{
return;
}
str = alignment >= 0 ? str.PadLeft(alignment) : str.PadRight(-alignment);
EnsureIndented();
_owner.HandlerRawAppend(str);
}
/// <summary>Appends a formatted value with alignment and format string from the interpolated string.</summary>
/// <typeparam name="T">The type of the value to format. Must implement <see cref="System.IFormattable"/>.</typeparam>
/// <param name="value">The value to append.</param>
/// <param name="alignment">Minimum width; negative values left-align.</param>
/// <param name="format">The format string.</param>
public void AppendFormatted<T>(T value, int alignment, string? format)
where T : System.IFormattable
{
var str = value?.ToString(format, CultureInfo.InvariantCulture) ?? string.Empty;
str = alignment >= 0 ? str.PadLeft(alignment) : str.PadRight(-alignment);
EnsureIndented();
_owner.HandlerRawAppend(str);
}
/// <summary>Appends a <see cref="ReadOnlySpan{T}"/> value from the interpolated string.</summary>
/// <param name="value">The span to append.</param>
public void AppendFormatted(ReadOnlySpan<char> value)
{
if (value.IsEmpty)
{
return;
}
EnsureIndented();
_owner.HandlerRawAppend(value);
}
}
#endif