-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCsStructureExtensions.cs
More file actions
106 lines (84 loc) · 4.56 KB
/
CsStructureExtensions.cs
File metadata and controls
106 lines (84 loc) · 4.56 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
//*****************************************************************************
//* Code Factory SDK
//* Copyright (c) 2022 CodeFactory, LLC
//*****************************************************************************
using System.Linq;
using System.Text;
using CodeFactory.DotNet.CSharp;
using CodeFactory.DotNet.CSharp.FormattedSyntax;
namespace CodeFactory.Formatting.CSharp
{
/// <summary>
/// Extensions class that manage extensions that support CodeFactory models that implement the <see cref="CsStructure"/> model.
/// </summary>
public static class CsStructureExtensions
{
/// <summary>
/// Defines the fully qualified type name for the structure model.
/// </summary>
/// <param name="source">The source structure model to generate the type name from.</param>
/// <param name="manager">Namespace manager used to format type names.This is an optional parameter.</param>
/// <returns>The full type name or null if model data was missing.</returns>
public static string CSharpFormatTypeName(this CsStructure source, NamespaceManager manager = null)
{
if (source == null) return null;
if (!source.IsLoaded) return null;
StringBuilder structureNameBuilder = new StringBuilder();
var namespaceManager = manager ?? new NamespaceManager(null);
var formattedNamespace = namespaceManager.AppendingNamespace(source.Namespace);
structureNameBuilder.Append(
formattedNamespace == null ? source.Name : $"{formattedNamespace}.{source.Name}");
if (source.IsGeneric)
structureNameBuilder.Append(
source.GenericParameters.CSharpFormatGenericParametersSignature(namespaceManager));
return structureNameBuilder.ToString();
}
/// <summary>
/// Extension method that generates a the full interface declaration syntax based on the provided model.
/// </summary>
/// <example>
/// Format with no generics [security] struct [name] [:[inherited interfaces*]]
/// Format with generics [security] struct [name] <[generic parameters]> [: [inherited interfaces*]] [Generic Where Clauses*]
/// </example>
/// <param name="source">The source structure model to format.</param>
/// <param name="security">The security level the structure should be implemented as.</param>
/// <param name="manager">Namespace manager used to format type names.This is an optional parameter.</param>
/// <param name="interfaceName">Optional parameter that allows you to specify a new name for the structure.</param>
/// <returns>The full structure declaration or null if model data was missing.</returns>
public static string CSharpFormatDeclaration(this CsStructure source, CsSecurity security, NamespaceManager manager = null,
string interfaceName = null)
{
if (source == null) return null;
if (!source.IsLoaded) return null;
var name = interfaceName ?? source.Name;
StringBuilder interfaceBuilder = new StringBuilder($"{security.CSharpFormatKeyword()} {Keywords.Interface} {name}");
if (source.IsGeneric) interfaceBuilder.Append(source.GenericParameters.CSharpFormatGenericParametersSignature(manager));
if (source.InheritedInterfaces.Any())
{
var interfaces = source.InheritedInterfaces;
int totalCount = interfaces.Count;
int currentInterface = 0;
interfaceBuilder.Append(": ");
foreach (var csInterface in interfaces)
{
currentInterface++;
var interfaceType = csInterface.CSharpFormatInheritanceTypeName(manager);
if (interfaceType == null) continue;
interfaceBuilder.Append(interfaceType);
if (totalCount > currentInterface) interfaceBuilder.Append(", ");
}
}
if (source.IsGeneric)
{
interfaceBuilder.Append(" ");
foreach (var sourceGenericParameter in source.GenericParameters)
{
var whereClause = sourceGenericParameter.CSharpFormatGenericWhereClauseSignature(manager);
if (string.IsNullOrEmpty(whereClause)) continue;
interfaceBuilder.Append($"{whereClause} ");
}
}
return interfaceBuilder.ToString();
}
}
}