-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathUtility.cs
More file actions
132 lines (114 loc) · 4.65 KB
/
Copy pathUtility.cs
File metadata and controls
132 lines (114 loc) · 4.65 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
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis;
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using Microsoft.CodeAnalysis.CSharp;
namespace CodeGeneration
{
public static class Utility
{
public const string ToolName = "OpenLibraryNET.SourceGenerator";
public const string ToolVersion = "1.0.0.0";
public static string GetDeclaration(INamedTypeSymbol symbol)
{
SymbolDisplayFormat format = new SymbolDisplayFormat(
typeQualificationStyle: SymbolDisplayTypeQualificationStyle.NameOnly,
genericsOptions: SymbolDisplayGenericsOptions.IncludeTypeParameters | SymbolDisplayGenericsOptions.IncludeVariance | SymbolDisplayGenericsOptions.IncludeTypeConstraints,
kindOptions: SymbolDisplayKindOptions.IncludeTypeKeyword | SymbolDisplayKindOptions.IncludeNamespaceKeyword,
miscellaneousOptions: SymbolDisplayMiscellaneousOptions.UseSpecialTypes
);
string declaration = "";
if (symbol.DeclaredAccessibility == Accessibility.Public)
{
declaration += "public ";
}
else if (symbol.DeclaredAccessibility == Accessibility.Private)
{
declaration += "private ";
}
else if (symbol.DeclaredAccessibility == Accessibility.Internal)
{
declaration += "internal ";
}
else if (symbol.DeclaredAccessibility == Accessibility.Protected)
{
declaration += "protected ";
}
if (symbol.IsStatic)
{
declaration += "static ";
}
else
{
if (symbol.IsAbstract)
{
declaration += "abstract ";
}
if (symbol.IsSealed)
{
declaration += "sealed ";
}
}
declaration += "partial " + symbol.ToDisplayString(format);
return declaration;
}
public static List<string> GetPropertyAndFieldNames(TypeDeclarationSyntax declarationSyntax)
{
List<string> memberNames = new List<string>();
memberNames.AddRange(declarationSyntax.Members.OfType<PropertyDeclarationSyntax>().Select(p => p.Identifier.Text));
memberNames.AddRange(declarationSyntax.Members.OfType<FieldDeclarationSyntax>().SelectMany(p => p.Declaration.Variables.Select(v => v.Identifier.Text)));
return memberNames;
}
public static string BuildSource(INamedTypeSymbol symbol, string contents)
{
StringBuilder sb = new StringBuilder();
int indent = 0;
if (symbol.ContainingNamespace != null)
{
sb.AppendLine($"namespace {symbol.ContainingNamespace.ToDisplayString()}\r\n{{");
indent++;
}
if (symbol.ContainingType != null)
{
Stack<INamedTypeSymbol> containingTypes = new Stack<INamedTypeSymbol>();
INamedTypeSymbol tmp = symbol;
while (tmp.ContainingType != null)
{
containingTypes.Push(tmp.ContainingType);
tmp = tmp.ContainingType;
}
foreach (var currentSymbol in containingTypes)
{
sb.AppendIndented($"\r\n{GetDeclaration(currentSymbol)}\r\n{{", indent);
indent++;
}
}
sb.AppendIndented($"\r\n{GetDeclaration(symbol)}\r\n{{", indent);
indent++;
sb.AppendIndented(contents, indent);
indent--;
while (indent >= 0)
{
sb.AppendIndented("\r\n}", indent);
indent--;
}
return sb.ToString();
}
public static StringBuilder AppendIndented(this StringBuilder sb, string textBlock, int indentationLevel)
{
foreach (var line in textBlock.TrimEnd().Split(new[] { "\r\n", "\n", "\r" }, StringSplitOptions.None))
if (!string.IsNullOrWhiteSpace(line))
sb.AppendLine($"{string.Concat(Enumerable.Repeat("\t", indentationLevel))}{line}");
return sb;
}
//public static bool IsAutoProperty(this IPropertySymbol property)
//{
// property.BackingField
// return property.GetMethod != null &&
// property.GetMethod.DeclaredAccessibility == Accessibility.Public &&
// property.GetMethod.GetBody() == null;
//}
}
}