-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCsParameterExtensions.cs
More file actions
61 lines (52 loc) · 2.92 KB
/
CsParameterExtensions.cs
File metadata and controls
61 lines (52 loc) · 2.92 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
//*****************************************************************************
//* Code Factory SDK
//* Copyright (c) 2022 CodeFactory, LLC
//*****************************************************************************
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CodeFactory.DotNet.CSharp;
using CodeFactory.DotNet.CSharp.FormattedSyntax;
namespace CodeFactory.Formatting.CSharp
{
/// <summary>
/// Extensions class that provides common automation tasks rolled up under standard extension methods that support the <see cref="CsParameter"/> model.
/// </summary>
public static class CsParameterExtensions
{
/// <summary>
/// Extension method that create the fully formatted parameters section in c# syntax.
/// </summary>
/// <param name="source">The source list of parameters to be turned into a parameters signature.</param>
/// <param name="manager">Optional parameter that contains all the using statements from the source code, when used will replace namespaces on type definition in code.</param>
/// <returns>The fully formatted parameters signature or null if data was missing.</returns>
public static string CSharpFormatParametersSignature (this IReadOnlyList<CsParameter> source, NamespaceManager manager = null)
{
if (source == null) return null;
if (!source.Any()) return null;
StringBuilder parameterSignature = new StringBuilder(Symbols.ParametersDefinitionStart);
int totalParameters = source.Count;
int currentParameter = 0;
foreach (var sourceParameter in source)
{
currentParameter++;
if (sourceParameter.HasAttributes)
{
foreach (var sourceParameterAttribute in sourceParameter.Attributes)
{
parameterSignature.Append($"{sourceParameterAttribute.CSharpFormatAttributeSignature(manager)} ");
}
}
if (sourceParameter.IsOut) parameterSignature.Append($"{Keywords.Out} ");
if (sourceParameter.IsRef) parameterSignature.Append($"{Keywords.Ref} ");
if (sourceParameter.IsParams) parameterSignature.Append($"{Keywords.Params} ");
parameterSignature.Append(!sourceParameter.IsOptional
? $"{sourceParameter.ParameterType.CSharpFormatTypeName(manager)} {sourceParameter.Name}"
: $"{sourceParameter.ParameterType.CSharpFormatTypeName(manager)} {sourceParameter.Name} = {sourceParameter.DefaultValue.CSharpFormatParameterDefaultValue(sourceParameter.ParameterType)}");
if (totalParameters > currentParameter) parameterSignature.Append(", ");
}
parameterSignature.Append(Symbols.ParametersDefinitionEnd);
return parameterSignature.ToString();
}
}
}