-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCSharpCodeBuilder.Documentation.cs
More file actions
382 lines (333 loc) · 15.8 KB
/
CSharpCodeBuilder.Documentation.cs
File metadata and controls
382 lines (333 loc) · 15.8 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
namespace NetEvolve.CodeBuilder;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
public partial record CSharpCodeBuilder
{
/// <summary>
/// Appends a single-line XML documentation comment.
/// </summary>
/// <param name="content">The content for the documentation comment.</param>
/// <returns>The current <see cref="CSharpCodeBuilder"/> instance to allow for method chaining.</returns>
/// <remarks>If the content is null or empty, the method returns without appending anything.</remarks>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public CSharpCodeBuilder AppendXmlDoc(string? content) =>
string.IsNullOrEmpty(content) ? this : EnsureNewLineForXmlDoc().AppendLine($"/// {content}");
/// <summary>
/// Appends an XML documentation summary element.
/// </summary>
/// <param name="summary">The summary text to include in the documentation.</param>
/// <returns>The current <see cref="CSharpCodeBuilder"/> instance to allow for method chaining.</returns>
/// <remarks>If the summary is null or empty, the method returns without appending anything.</remarks>
public CSharpCodeBuilder AppendXmlDocSummary(string? summary)
{
if (string.IsNullOrEmpty(summary))
{
return this;
}
return EnsureNewLineForXmlDoc()
.AppendLine("/// <summary>")
.AppendLine($"/// {summary}")
.AppendLine("/// </summary>");
}
/// <summary>
/// Appends an XML documentation summary element with multiple lines.
/// </summary>
/// <param name="summaryLines">The summary lines to include in the documentation.</param>
/// <returns>The current <see cref="CSharpCodeBuilder"/> instance to allow for method chaining.</returns>
/// <remarks>If the summary lines collection is null or empty, the method returns without appending anything.</remarks>
public CSharpCodeBuilder AppendXmlDocSummary(IEnumerable<string>? summaryLines)
{
if (summaryLines is null)
{
return this;
}
var hasContent = false;
var builder = EnsureNewLineForXmlDoc().AppendLine("/// <summary>");
foreach (var line in summaryLines.Where(l => !string.IsNullOrEmpty(l)))
{
builder = builder.AppendLine($"/// {line}");
hasContent = true;
}
return hasContent ? builder.AppendLine("/// </summary>") : this;
}
/// <summary>
/// Appends an XML documentation param element.
/// </summary>
/// <param name="paramName">The name of the parameter.</param>
/// <param name="description">The description of the parameter.</param>
/// <returns>The current <see cref="CSharpCodeBuilder"/> instance to allow for method chaining.</returns>
/// <remarks>If the parameter name or description is null or empty, the method returns without appending anything.</remarks>
public CSharpCodeBuilder AppendXmlDocParam(string? paramName, string? description)
{
if (string.IsNullOrEmpty(paramName) || string.IsNullOrEmpty(description))
{
return this;
}
return EnsureNewLineForXmlDoc().AppendLine($"/// <param name=\"{paramName}\">{description}</param>");
}
/// <summary>
/// Appends multiple XML documentation param elements.
/// </summary>
/// <param name="parameters">A collection of parameter name and description pairs.</param>
/// <returns>The current <see cref="CSharpCodeBuilder"/> instance to allow for method chaining.</returns>
/// <remarks>If the parameters collection is null or empty, the method returns without appending anything.</remarks>
public CSharpCodeBuilder AppendXmlDocParams(IEnumerable<(string Name, string Description)>? parameters)
{
if (parameters is null)
{
return this;
}
var builder = this;
foreach (var (name, description) in parameters)
{
builder = builder.AppendXmlDocParam(name, description);
}
return builder;
}
/// <summary>
/// Appends an XML documentation returns element.
/// </summary>
/// <param name="description">The description of the return value.</param>
/// <returns>The current <see cref="CSharpCodeBuilder"/> instance to allow for method chaining.</returns>
/// <remarks>If the description is null or empty, the method returns without appending anything.</remarks>
public CSharpCodeBuilder AppendXmlDocReturns(string? description)
{
if (string.IsNullOrEmpty(description))
{
return this;
}
return EnsureNewLineForXmlDoc().AppendLine($"/// <returns>{description}</returns>");
}
/// <summary>
/// Appends an XML documentation remarks element.
/// </summary>
/// <param name="remarks">The remarks text to include in the documentation.</param>
/// <returns>The current <see cref="CSharpCodeBuilder"/> instance to allow for method chaining.</returns>
/// <remarks>If the remarks text is null or empty, the method returns without appending anything.</remarks>
public CSharpCodeBuilder AppendXmlDocRemarks(string? remarks)
{
if (string.IsNullOrEmpty(remarks))
{
return this;
}
return EnsureNewLineForXmlDoc()
.AppendLine("/// <remarks>")
.AppendLine($"/// {remarks}")
.AppendLine("/// </remarks>");
}
/// <summary>
/// Appends an XML documentation remarks element with multiple lines.
/// </summary>
/// <param name="remarksLines">The remarks lines to include in the documentation.</param>
/// <returns>The current <see cref="CSharpCodeBuilder"/> instance to allow for method chaining.</returns>
/// <remarks>If the remarks lines collection is null or empty, the method returns without appending anything.</remarks>
public CSharpCodeBuilder AppendXmlDocRemarks(IEnumerable<string>? remarksLines)
{
if (remarksLines is null)
{
return this;
}
var hasContent = false;
var builder = EnsureNewLineForXmlDoc().AppendLine("/// <remarks>");
foreach (var line in remarksLines.Where(l => !string.IsNullOrEmpty(l)))
{
builder = builder.AppendLine($"/// {line}");
hasContent = true;
}
return hasContent ? builder.AppendLine("/// </remarks>") : this;
}
/// <summary>
/// Appends an XML documentation exception element.
/// </summary>
/// <param name="exceptionType">The type of exception that can be thrown.</param>
/// <param name="description">The description of when the exception is thrown.</param>
/// <returns>The current <see cref="CSharpCodeBuilder"/> instance to allow for method chaining.</returns>
/// <remarks>If the exception type or description is null or empty, the method returns without appending anything.</remarks>
public CSharpCodeBuilder AppendXmlDocException(string? exceptionType, string? description)
{
if (string.IsNullOrEmpty(exceptionType) || string.IsNullOrEmpty(description))
{
return this;
}
return EnsureNewLineForXmlDoc()
.AppendLine($"/// <exception cref=\"{exceptionType}\">{description}</exception>");
}
/// <summary>
/// Appends multiple XML documentation exception elements.
/// </summary>
/// <param name="exceptions">A collection of exception type and description pairs.</param>
/// <returns>The current <see cref="CSharpCodeBuilder"/> instance to allow for method chaining.</returns>
/// <remarks>If the exceptions collection is null or empty, the method returns without appending anything.</remarks>
public CSharpCodeBuilder AppendXmlDocExceptions(IEnumerable<(string Type, string Description)>? exceptions)
{
if (exceptions is null)
{
return this;
}
var builder = this;
foreach (var (type, description) in exceptions)
{
builder = builder.AppendXmlDocException(type, description);
}
return builder;
}
/// <summary>
/// Appends an XML documentation example element.
/// </summary>
/// <param name="example">The example text or code to include in the documentation.</param>
/// <returns>The current <see cref="CSharpCodeBuilder"/> instance to allow for method chaining.</returns>
/// <remarks>If the example is null or empty, the method returns without appending anything.</remarks>
public CSharpCodeBuilder AppendXmlDocExample(string? example)
{
if (string.IsNullOrEmpty(example))
{
return this;
}
return EnsureNewLineForXmlDoc()
.AppendLine("/// <example>")
.AppendLine($"/// {example}")
.AppendLine("/// </example>");
}
/// <summary>
/// Appends an XML documentation example element with multiple lines.
/// </summary>
/// <param name="exampleLines">The example lines to include in the documentation.</param>
/// <returns>The current <see cref="CSharpCodeBuilder"/> instance to allow for method chaining.</returns>
/// <remarks>If the example lines collection is null or empty, the method returns without appending anything.</remarks>
public CSharpCodeBuilder AppendXmlDocExample(IEnumerable<string>? exampleLines)
{
if (exampleLines is null)
{
return this;
}
var hasContent = false;
var builder = EnsureNewLineForXmlDoc().AppendLine("/// <example>");
foreach (var line in exampleLines.Where(l => !string.IsNullOrEmpty(l)))
{
builder = builder.AppendLine($"/// {line}");
hasContent = true;
}
return hasContent ? builder.AppendLine("/// </example>") : this;
}
/// <summary>
/// Appends an XML documentation see element for cross-references.
/// </summary>
/// <param name="cref">The cross-reference to another member or type.</param>
/// <returns>The current <see cref="CSharpCodeBuilder"/> instance to allow for method chaining.</returns>
/// <remarks>If the cref is null or empty, the method returns without appending anything.</remarks>
public CSharpCodeBuilder AppendXmlDocSee(string? cref)
{
if (string.IsNullOrEmpty(cref))
{
return this;
}
return EnsureNewLineForXmlDoc().AppendLine($"/// <see cref=\"{cref}\"/>");
}
/// <summary>
/// Appends an XML documentation seealso element for see-also references.
/// </summary>
/// <param name="cref">The cross-reference to another member or type.</param>
/// <returns>The current <see cref="CSharpCodeBuilder"/> instance to allow for method chaining.</returns>
/// <remarks>If the cref is null or empty, the method returns without appending anything.</remarks>
public CSharpCodeBuilder AppendXmlDocSeeAlso(string? cref)
{
if (string.IsNullOrEmpty(cref))
{
return this;
}
return EnsureNewLineForXmlDoc().AppendLine($"/// <seealso cref=\"{cref}\"/>");
}
/// <summary>
/// Appends an XML documentation value element for property documentation.
/// </summary>
/// <param name="description">The description of the property value.</param>
/// <returns>The current <see cref="CSharpCodeBuilder"/> instance to allow for method chaining.</returns>
/// <remarks>If the description is null or empty, the method returns without appending anything.</remarks>
public CSharpCodeBuilder AppendXmlDocValue(string? description)
{
if (string.IsNullOrEmpty(description))
{
return this;
}
return EnsureNewLineForXmlDoc().AppendLine($"/// <value>{description}</value>");
}
/// <summary>
/// Appends an XML documentation typeparam element for generic type parameters.
/// </summary>
/// <param name="paramName">The name of the type parameter.</param>
/// <param name="description">The description of the type parameter.</param>
/// <returns>The current <see cref="CSharpCodeBuilder"/> instance to allow for method chaining.</returns>
/// <remarks>If the parameter name or description is null or empty, the method returns without appending anything.</remarks>
public CSharpCodeBuilder AppendXmlDocTypeParam(string? paramName, string? description)
{
if (string.IsNullOrEmpty(paramName) || string.IsNullOrEmpty(description))
{
return this;
}
return EnsureNewLineForXmlDoc().AppendLine($"/// <typeparam name=\"{paramName}\">{description}</typeparam>");
}
/// <summary>
/// Appends multiple XML documentation typeparam elements.
/// </summary>
/// <param name="typeParameters">A collection of type parameter name and description pairs.</param>
/// <returns>The current <see cref="CSharpCodeBuilder"/> instance to allow for method chaining.</returns>
/// <remarks>If the type parameters collection is null or empty, the method returns without appending anything.</remarks>
public CSharpCodeBuilder AppendXmlDocTypeParams(IEnumerable<(string, string)>? typeParameters)
{
if (typeParameters is null)
{
return this;
}
var builder = this;
foreach (var (name, description) in typeParameters)
{
builder = builder.AppendXmlDocTypeParam(name, description);
}
return builder;
}
/// <summary>
/// Appends an inheritdoc XML documentation element.
/// </summary>
/// <param name="cref">Optional cross-reference to specify which member to inherit documentation from.</param>
/// <returns>The current <see cref="CSharpCodeBuilder"/> instance to allow for method chaining.</returns>
/// <remarks>If no cref is provided, inherits documentation from the base or interface member.</remarks>
public CSharpCodeBuilder AppendXmlDocInheritDoc(string? cref = null) =>
string.IsNullOrEmpty(cref)
? EnsureNewLineForXmlDoc().AppendLine("/// <inheritdoc />")
: EnsureNewLineForXmlDoc().AppendLine($"/// <inheritdoc cref=\"{cref}\" />");
/// <summary>
/// Appends a custom XML documentation element.
/// </summary>
/// <param name="elementName">The name of the XML element.</param>
/// <param name="content">The content of the XML element.</param>
/// <param name="attributes">Optional attributes for the XML element.</param>
/// <returns>The current <see cref="CSharpCodeBuilder"/> instance to allow for method chaining.</returns>
/// <remarks>If the element name is null or empty, the method returns without appending anything.</remarks>
public CSharpCodeBuilder AppendXmlDocCustomElement(
string? elementName,
string? content = null,
string? attributes = null
)
{
if (string.IsNullOrEmpty(elementName))
{
return this;
}
var attributesPart = string.IsNullOrEmpty(attributes) ? string.Empty : $" {attributes}";
if (string.IsNullOrEmpty(content))
{
return EnsureNewLineForXmlDoc().AppendLine($"/// <{elementName}{attributesPart} />");
}
return EnsureNewLineForXmlDoc().AppendLine($"/// <{elementName}{attributesPart}>{content}</{elementName}>");
}
/// <summary>
/// Ensures that XML documentation comments start on a new line if there's already content in the builder.
/// </summary>
/// <returns>The current <see cref="CSharpCodeBuilder"/> instance to allow for method chaining.</returns>
/// <remarks>
/// This method checks if the builder already has content and if we're not already on a new line,
/// it appends a line break to ensure XML documentation starts on a fresh line.
/// </remarks>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private CSharpCodeBuilder EnsureNewLineForXmlDoc() => AppendLineIf(Length > 0 && !_isNewline);
}