This repository has been archived by the owner on Sep 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 262
/
CSharpAmbience.cs
276 lines (255 loc) · 9.92 KB
/
CSharpAmbience.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using System.IO;
using ICSharpCode.NRefactory.CSharp.Refactoring;
using ICSharpCode.NRefactory.TypeSystem;
using ICSharpCode.NRefactory.TypeSystem.Implementation;
namespace ICSharpCode.NRefactory.CSharp
{
/// <summary>
/// C# ambience.
/// </summary>
public class CSharpAmbience : IAmbience
{
public ConversionFlags ConversionFlags { get; set; }
#region ConvertEntity
public string ConvertEntity(IEntity entity)
{
if (entity == null)
throw new ArgumentNullException("entity");
StringWriter writer = new StringWriter();
ConvertEntity(entity, new TextWriterOutputFormatter(writer), FormattingOptionsFactory.CreateMono ());
return writer.ToString();
}
public void ConvertEntity(IEntity entity, IOutputFormatter formatter, CSharpFormattingOptions formattingPolicy)
{
if (entity == null)
throw new ArgumentNullException("entity");
if (formatter == null)
throw new ArgumentNullException("formatter");
if (formattingPolicy == null)
throw new ArgumentNullException("options");
TypeSystemAstBuilder astBuilder = CreateAstBuilder();
EntityDeclaration node = astBuilder.ConvertEntity(entity);
PrintModifiers(node.Modifiers, formatter);
if ((ConversionFlags & ConversionFlags.ShowDefinitionKeyword) == ConversionFlags.ShowDefinitionKeyword) {
if (node is TypeDeclaration) {
switch (((TypeDeclaration)node).ClassType) {
case ClassType.Class:
formatter.WriteKeyword("class");
break;
case ClassType.Struct:
formatter.WriteKeyword("struct");
break;
case ClassType.Interface:
formatter.WriteKeyword("interface");
break;
case ClassType.Enum:
formatter.WriteKeyword("enum");
break;
default:
throw new Exception("Invalid value for ClassType");
}
formatter.Space();
} else if (node is DelegateDeclaration) {
formatter.WriteKeyword("delegate");
formatter.Space();
} else if (node is EventDeclaration) {
formatter.WriteKeyword("event");
formatter.Space();
}
}
if ((ConversionFlags & ConversionFlags.ShowReturnType) == ConversionFlags.ShowReturnType) {
var rt = node.GetChildByRole(Roles.Type);
if (!rt.IsNull) {
rt.AcceptVisitor(new CSharpOutputVisitor(formatter, formattingPolicy));
formatter.Space();
}
}
if (entity is ITypeDefinition)
WriteTypeDeclarationName((ITypeDefinition)entity, formatter, formattingPolicy);
else
WriteMemberDeclarationName((IMember)entity, formatter, formattingPolicy);
if ((ConversionFlags & ConversionFlags.ShowParameterList) == ConversionFlags.ShowParameterList && HasParameters(entity)) {
formatter.WriteToken(entity.EntityType == EntityType.Indexer ? "[" : "(");
bool first = true;
foreach (var param in node.GetChildrenByRole(Roles.Parameter)) {
if (first) {
first = false;
} else {
formatter.WriteToken(",");
formatter.Space();
}
param.AcceptVisitor(new CSharpOutputVisitor(formatter, formattingPolicy));
}
formatter.WriteToken(entity.EntityType == EntityType.Indexer ? "]" : ")");
}
if ((ConversionFlags & ConversionFlags.ShowBody) == ConversionFlags.ShowBody && !(node is TypeDeclaration)) {
IProperty property = entity as IProperty;
if (property != null) {
formatter.Space();
formatter.WriteToken("{");
formatter.Space();
if (property.CanGet) {
formatter.WriteKeyword("get");
formatter.WriteToken(";");
formatter.Space();
}
if (property.CanSet) {
formatter.WriteKeyword("set");
formatter.WriteToken(";");
formatter.Space();
}
formatter.WriteToken("}");
} else {
formatter.WriteToken(";");
}
}
}
bool HasParameters(IEntity e)
{
switch (e.EntityType) {
case EntityType.TypeDefinition:
return ((ITypeDefinition)e).Kind == TypeKind.Delegate;
case EntityType.Indexer:
case EntityType.Method:
case EntityType.Operator:
case EntityType.Constructor:
case EntityType.Destructor:
return true;
default:
return false;
}
}
TypeSystemAstBuilder CreateAstBuilder()
{
TypeSystemAstBuilder astBuilder = new TypeSystemAstBuilder();
astBuilder.AddAnnotations = true;
astBuilder.ShowModifiers = (ConversionFlags & ConversionFlags.ShowModifiers) == ConversionFlags.ShowModifiers;
astBuilder.ShowAccessibility = (ConversionFlags & ConversionFlags.ShowAccessibility) == ConversionFlags.ShowAccessibility;
astBuilder.AlwaysUseShortTypeNames = (ConversionFlags & ConversionFlags.UseFullyQualifiedTypeNames) != ConversionFlags.UseFullyQualifiedTypeNames;
astBuilder.ShowParameterNames = (ConversionFlags & ConversionFlags.ShowParameterNames) == ConversionFlags.ShowParameterNames;
return astBuilder;
}
void WriteTypeDeclarationName(ITypeDefinition typeDef, IOutputFormatter formatter, CSharpFormattingOptions formattingPolicy)
{
TypeSystemAstBuilder astBuilder = CreateAstBuilder();
if (typeDef.DeclaringTypeDefinition != null) {
WriteTypeDeclarationName(typeDef.DeclaringTypeDefinition, formatter, formattingPolicy);
formatter.WriteToken(".");
} else if ((ConversionFlags & ConversionFlags.UseFullyQualifiedTypeNames) == ConversionFlags.UseFullyQualifiedTypeNames) {
formatter.WriteIdentifier(typeDef.Namespace);
formatter.WriteToken(".");
}
formatter.WriteIdentifier(typeDef.Name);
if ((ConversionFlags & ConversionFlags.ShowTypeParameterList) == ConversionFlags.ShowTypeParameterList) {
var outputVisitor = new CSharpOutputVisitor(formatter, formattingPolicy);
outputVisitor.WriteTypeParameters(astBuilder.ConvertEntity(typeDef).GetChildrenByRole(Roles.TypeParameter));
}
}
void WriteMemberDeclarationName(IMember member, IOutputFormatter formatter, CSharpFormattingOptions formattingPolicy)
{
TypeSystemAstBuilder astBuilder = CreateAstBuilder();
if ((ConversionFlags & ConversionFlags.ShowDeclaringType) == ConversionFlags.ShowDeclaringType) {
ConvertType(member.DeclaringType, formatter, formattingPolicy);
formatter.WriteToken(".");
}
switch (member.EntityType) {
case EntityType.Indexer:
formatter.WriteKeyword("this");
break;
case EntityType.Constructor:
formatter.WriteIdentifier(member.DeclaringType.Name);
break;
case EntityType.Destructor:
formatter.WriteToken("~");
formatter.WriteIdentifier(member.DeclaringType.Name);
break;
case EntityType.Operator:
switch (member.Name) {
case "op_Implicit":
formatter.WriteKeyword("implicit");
formatter.Space();
formatter.WriteKeyword("operator");
formatter.Space();
ConvertType(member.ReturnType, formatter, formattingPolicy);
break;
case "op_Explicit":
formatter.WriteKeyword("explicit");
formatter.Space();
formatter.WriteKeyword("operator");
formatter.Space();
ConvertType(member.ReturnType, formatter, formattingPolicy);
break;
default:
formatter.WriteKeyword("operator");
formatter.Space();
var operatorType = OperatorDeclaration.GetOperatorType(member.Name);
if (operatorType.HasValue)
formatter.WriteToken(OperatorDeclaration.GetToken(operatorType.Value));
else
formatter.WriteIdentifier(member.Name);
break;
}
break;
default:
formatter.WriteIdentifier(member.Name);
break;
}
if ((ConversionFlags & ConversionFlags.ShowTypeParameterList) == ConversionFlags.ShowTypeParameterList && member.EntityType == EntityType.Method) {
var outputVisitor = new CSharpOutputVisitor(formatter, formattingPolicy);
outputVisitor.WriteTypeParameters(astBuilder.ConvertEntity(member).GetChildrenByRole(Roles.TypeParameter));
}
}
void PrintModifiers(Modifiers modifiers, IOutputFormatter formatter)
{
foreach (var m in CSharpModifierToken.AllModifiers) {
if ((modifiers & m) == m) {
formatter.WriteKeyword(CSharpModifierToken.GetModifierName(m));
formatter.Space();
}
}
}
#endregion
public string ConvertVariable(IVariable v)
{
TypeSystemAstBuilder astBuilder = CreateAstBuilder();
AstNode astNode = astBuilder.ConvertVariable(v);
return astNode.GetText().TrimEnd(';', '\r', '\n');
}
public string ConvertType(IType type)
{
if (type == null)
throw new ArgumentNullException("type");
TypeSystemAstBuilder astBuilder = CreateAstBuilder();
AstType astType = astBuilder.ConvertType(type);
return astType.GetText();
}
public void ConvertType(IType type, IOutputFormatter formatter, CSharpFormattingOptions formattingPolicy)
{
TypeSystemAstBuilder astBuilder = CreateAstBuilder();
AstType astType = astBuilder.ConvertType(type);
astType.AcceptVisitor(new CSharpOutputVisitor(formatter, formattingPolicy));
}
public string WrapComment(string comment)
{
return "// " + comment;
}
}
}