-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCsClassExtensions.cs
More file actions
118 lines (94 loc) · 5.05 KB
/
CsClassExtensions.cs
File metadata and controls
118 lines (94 loc) · 5.05 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
//*****************************************************************************
//* 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="CsClass"/> model.
/// </summary>
public static class CsClassExtensions
{
/// <summary>
/// Defines the fully qualified base type name for the class model.
/// </summary>
/// <param name="source">The source interface 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 CSharpFormatBaseTypeName(this CsClass source, NamespaceManager manager = null)
{
if (source == null) return null;
if (!source.IsLoaded) return null;
StringBuilder baseNameBuilder = new StringBuilder();
var namespaceManager = manager ?? new NamespaceManager(null);
var targetNamespace = namespaceManager.AppendingNamespace(source.Namespace);
baseNameBuilder.Append(targetNamespace == null ? source.Name : $"{targetNamespace}.{source.Name}");
if (source.IsGeneric)
baseNameBuilder.Append(
source.GenericParameters.CSharpFormatGenericParametersSignature(namespaceManager));
return baseNameBuilder.ToString();
}
/// <summary>
/// Extension method that generates a the full class declaration syntax based on the provided model.
/// </summary>
/// <example>
/// Format with no generics [security] class [name] [:[base class*], [inherited interfaces*]]
/// Format with generics [security] class [name] <[generic parameters]> [: [base class*], [inherited interfaces*]] [Generic Where Clauses*]
/// </example>
/// <param name="source">The source class model to format.</param>
/// <param name="security">The security level the class should be implemented as.</param>
/// <param name="manager">Namespace manager used to format type names.This is an optional parameter.</param>
/// <param name="className">Optional parameter that allows you to specify a new name for the class.</param>
/// <returns>The full class declaration or null if model data was missing.</returns>
public static string CSharpFormatDeclaration(this CsClass source, CsSecurity security, NamespaceManager manager = null,
string className = null)
{
if (source == null) return null;
if (!source.IsLoaded) return null;
bool hasBaseClass = false;
var name = className ?? source.Name;
StringBuilder classBuilder = new StringBuilder($"{security.CSharpFormatKeyword()} {Keywords.Class} {name}");
if (source.IsGeneric) classBuilder.Append(source.GenericParameters.CSharpFormatGenericParametersSignature(manager));
if(source.BaseClass != null)
if (source.BaseClass.Namespace.ToLowerInvariant() == "system" &
source.BaseClass.Name.ToLowerInvariant() == "object") hasBaseClass = false;
else hasBaseClass = true;
if (hasBaseClass)
{
var baseName = source.BaseClass.CSharpFormatBaseTypeName(manager);
if (string.IsNullOrEmpty(baseName)) return null;
classBuilder.Append($":{baseName}");
}
if (source.InheritedInterfaces.Any())
{
var interfaces = source.InheritedInterfaces;
int totalCount = interfaces.Count;
int currentInterface = 0;
classBuilder.Append(hasBaseClass ? ", " :":");
foreach (var csInterface in interfaces)
{
currentInterface++;
var interfaceType = csInterface.CSharpFormatInheritanceTypeName(manager);
if (interfaceType == null) continue;
classBuilder.Append(interfaceType);
if (totalCount > currentInterface) classBuilder.Append(", ");
}
}
if (source.IsGeneric)
{
classBuilder.Append(" ");
foreach (var sourceGenericParameter in source.GenericParameters)
{
var whereClause = sourceGenericParameter.CSharpFormatGenericWhereClauseSignature(manager);
if (string.IsNullOrEmpty(whereClause)) continue;
classBuilder.Append($"{whereClause} ");
}
}
return classBuilder.ToString();
}
}
}