This repository has been archived by the owner on Dec 13, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
RoslynExtensions.cs
198 lines (179 loc) · 9.53 KB
/
RoslynExtensions.cs
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
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using System;
namespace FogCreek.Wasabi.CodeGenerators
{
static class RoslynExtensions
{
public static TypeDeclarationSyntax WithBaseList(this TypeDeclarationSyntax node, BaseListSyntax list)
{
switch (node.Kind())
{
case SyntaxKind.ClassDeclaration:
return ((ClassDeclarationSyntax)node).WithBaseList(list);
case SyntaxKind.InterfaceDeclaration:
return ((InterfaceDeclarationSyntax)node).WithBaseList(list);
case SyntaxKind.StructDeclaration:
return ((StructDeclarationSyntax)node).WithBaseList(list);
}
throw new NotImplementedException("WithBaseList " + node.Kind().ToString());
}
public static TypeDeclarationSyntax WithModifiers(this TypeDeclarationSyntax node, SyntaxTokenList modifiers)
{
switch (node.Kind())
{
case SyntaxKind.ClassDeclaration:
return ((ClassDeclarationSyntax)node).WithModifiers(modifiers);
case SyntaxKind.InterfaceDeclaration:
return ((InterfaceDeclarationSyntax)node).WithModifiers(modifiers);
case SyntaxKind.StructDeclaration:
return ((StructDeclarationSyntax)node).WithModifiers(modifiers);
}
throw new NotImplementedException("WithModifiers " + node.Kind().ToString());
}
public static BaseMethodDeclarationSyntax WithModifiers(this BaseMethodDeclarationSyntax node, SyntaxTokenList modifiers)
{
switch (node.Kind())
{
case SyntaxKind.OperatorDeclaration:
case SyntaxKind.ConversionOperatorDeclaration:
throw new NotImplementedException("Wasabi doesn't have operators");
case SyntaxKind.MethodDeclaration:
return ((MethodDeclarationSyntax)node).WithModifiers(modifiers);
case SyntaxKind.ConstructorDeclaration:
return ((ConstructorDeclarationSyntax)node).WithModifiers(modifiers);
case SyntaxKind.DestructorDeclaration:
return ((DestructorDeclarationSyntax)node).WithModifiers(modifiers);
}
throw new NotImplementedException("WithModifiers " + node.Kind().ToString());
}
public static BasePropertyDeclarationSyntax WithModifiers(this BasePropertyDeclarationSyntax node, SyntaxTokenList modifiers)
{
switch (node.Kind())
{
case SyntaxKind.IndexerDeclaration:
return ((IndexerDeclarationSyntax)node).WithModifiers(modifiers);
case SyntaxKind.PropertyDeclaration:
return (((PropertyDeclarationSyntax)node)).WithModifiers(modifiers);
}
throw new NotImplementedException("WithModifiers " + node.Kind().ToString());
}
public static BasePropertyDeclarationSyntax WithExplicitInterfaceSpecifier(this BasePropertyDeclarationSyntax node, ExplicitInterfaceSpecifierSyntax syntax)
{
switch (node.Kind())
{
case SyntaxKind.IndexerDeclaration:
return ((IndexerDeclarationSyntax)node).WithExplicitInterfaceSpecifier(syntax);
case SyntaxKind.PropertyDeclaration:
return (((PropertyDeclarationSyntax)node)).WithExplicitInterfaceSpecifier(syntax);
}
throw new NotImplementedException("WithExplicitInterfaceSpecifier " + node.Kind().ToString());
}
public static BasePropertyDeclarationSyntax WithAccessorList(this BasePropertyDeclarationSyntax node, AccessorListSyntax accessorList)
{
switch (node.Kind())
{
case SyntaxKind.IndexerDeclaration:
return ((IndexerDeclarationSyntax)node).WithAccessorList(accessorList);
case SyntaxKind.PropertyDeclaration:
return (((PropertyDeclarationSyntax)node)).WithAccessorList(accessorList);
}
throw new NotImplementedException();
}
public static TypeDeclarationSyntax AddMember(this TypeDeclarationSyntax node, MemberDeclarationSyntax member)
{
return node.WithMembers(node.Members.Add(member));
}
public static TypeDeclarationSyntax WithMembers(this TypeDeclarationSyntax node, SyntaxList<MemberDeclarationSyntax> members)
{
switch (node.Kind())
{
case SyntaxKind.ClassDeclaration:
return ((ClassDeclarationSyntax)node).WithMembers(members);
case SyntaxKind.InterfaceDeclaration:
return ((InterfaceDeclarationSyntax)node).WithMembers(members);
case SyntaxKind.StructDeclaration:
return ((StructDeclarationSyntax)node).WithMembers(members);
}
throw new NotImplementedException("WithMembers " + node.Kind().ToString());
}
public static TypeDeclarationSyntax WithAttributeLists(this TypeDeclarationSyntax node, SyntaxList<AttributeListSyntax> attributes)
{
switch (node.Kind())
{
case SyntaxKind.ClassDeclaration:
return ((ClassDeclarationSyntax)node).WithAttributeLists(attributes);
case SyntaxKind.InterfaceDeclaration:
return ((InterfaceDeclarationSyntax)node).WithAttributeLists(attributes);
case SyntaxKind.StructDeclaration:
return ((StructDeclarationSyntax)node).WithAttributeLists(attributes);
}
throw new NotImplementedException("WithAttributeLists " + node.Kind().ToString());
}
public static BaseMethodDeclarationSyntax WithAttributeLists(this BaseMethodDeclarationSyntax node, SyntaxList<AttributeListSyntax> attributes)
{
switch (node.Kind())
{
case SyntaxKind.OperatorDeclaration:
case SyntaxKind.ConversionOperatorDeclaration:
throw new NotImplementedException("Wasabi doesn't have operators");
case SyntaxKind.MethodDeclaration:
return ((MethodDeclarationSyntax)node).WithAttributeLists(attributes);
case SyntaxKind.ConstructorDeclaration:
return ((ConstructorDeclarationSyntax)node).WithAttributeLists(attributes);
case SyntaxKind.DestructorDeclaration:
return ((DestructorDeclarationSyntax)node).WithAttributeLists(attributes);
}
throw new NotImplementedException("WithAttributeLists " + node.Kind().ToString());
}
public static BaseMethodDeclarationSyntax WithParameterList(this BaseMethodDeclarationSyntax method, ParameterListSyntax pls)
{
switch (method.Kind())
{
case SyntaxKind.OperatorDeclaration:
case SyntaxKind.ConversionOperatorDeclaration:
throw new NotImplementedException("Wasabi doesn't have operators");
case SyntaxKind.MethodDeclaration:
return ((MethodDeclarationSyntax)method).WithParameterList(pls);
case SyntaxKind.ConstructorDeclaration:
return ((ConstructorDeclarationSyntax)method).WithParameterList(pls);
case SyntaxKind.DestructorDeclaration:
return ((DestructorDeclarationSyntax)method).WithParameterList(pls);
}
throw new NotImplementedException("WithParameterList " + method.Kind().ToString());
}
public static BaseMethodDeclarationSyntax WithReturnType(this BaseMethodDeclarationSyntax method, TypeSyntax type)
{
switch (method.Kind())
{
case SyntaxKind.OperatorDeclaration:
case SyntaxKind.ConversionOperatorDeclaration:
throw new NotImplementedException("Wasabi doesn't have operators");
case SyntaxKind.MethodDeclaration:
return ((MethodDeclarationSyntax)method).WithReturnType(type);
case SyntaxKind.ConstructorDeclaration:
throw new InvalidOperationException("Constructors don't have return type");
case SyntaxKind.DestructorDeclaration:
throw new InvalidOperationException("Destructors don't have return type");
}
throw new NotImplementedException("WithReturnType " + method.Kind().ToString());
}
public static BaseMethodDeclarationSyntax WithBody(this BaseMethodDeclarationSyntax method, BlockSyntax body)
{
switch (method.Kind())
{
case SyntaxKind.OperatorDeclaration:
case SyntaxKind.ConversionOperatorDeclaration:
throw new NotImplementedException("Wasabi doesn't have operators");
case SyntaxKind.MethodDeclaration:
return ((MethodDeclarationSyntax)method).WithBody(body);
case SyntaxKind.ConstructorDeclaration:
return ((ConstructorDeclarationSyntax)method).WithBody(body);
case SyntaxKind.DestructorDeclaration:
return ((DestructorDeclarationSyntax)method).WithBody(body);
}
throw new NotImplementedException("WithBody " + method.Kind().ToString());
}
}
}