1 | // Copyright (c) 2010-2013 AlphaSierraPapa for the SharpDevelop Team |
---|
2 | // |
---|
3 | // Permission is hereby granted, free of charge, to any person obtaining a copy of this |
---|
4 | // software and associated documentation files (the "Software"), to deal in the Software |
---|
5 | // without restriction, including without limitation the rights to use, copy, modify, merge, |
---|
6 | // publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons |
---|
7 | // to whom the Software is furnished to do so, subject to the following conditions: |
---|
8 | // |
---|
9 | // The above copyright notice and this permission notice shall be included in all copies or |
---|
10 | // substantial portions of the Software. |
---|
11 | // |
---|
12 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
---|
13 | // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR |
---|
14 | // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE |
---|
15 | // FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
---|
16 | // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
17 | // DEALINGS IN THE SOFTWARE. |
---|
18 | |
---|
19 | using System; |
---|
20 | using System.IO; |
---|
21 | using ICSharpCode.NRefactory.CSharp.Refactoring; |
---|
22 | using ICSharpCode.NRefactory.TypeSystem; |
---|
23 | |
---|
24 | namespace ICSharpCode.NRefactory.CSharp |
---|
25 | { |
---|
26 | /// <summary> |
---|
27 | /// C# ambience. Used to convert type system symbols to text (usually for displaying the symbol to the user; e.g. in editor tooltips) |
---|
28 | /// </summary> |
---|
29 | public class CSharpAmbience : IAmbience |
---|
30 | { |
---|
31 | public ConversionFlags ConversionFlags { get; set; } |
---|
32 | |
---|
33 | #region ConvertSymbol |
---|
34 | [Obsolete("Use ConvertSymbol() instead")] |
---|
35 | public string ConvertEntity(IEntity entity) |
---|
36 | { |
---|
37 | return ConvertSymbol(entity); |
---|
38 | } |
---|
39 | |
---|
40 | public string ConvertSymbol(ISymbol symbol) |
---|
41 | { |
---|
42 | if (symbol == null) |
---|
43 | throw new ArgumentNullException("symbol"); |
---|
44 | |
---|
45 | StringWriter writer = new StringWriter(); |
---|
46 | ConvertSymbol(symbol, new TextWriterTokenWriter(writer), FormattingOptionsFactory.CreateMono ()); |
---|
47 | return writer.ToString(); |
---|
48 | } |
---|
49 | |
---|
50 | [Obsolete("Use ConvertSymbol() instead")] |
---|
51 | public void ConvertEntity(IEntity entity, TokenWriter writer, CSharpFormattingOptions formattingPolicy) |
---|
52 | { |
---|
53 | ConvertSymbol(entity, writer, formattingPolicy); |
---|
54 | } |
---|
55 | |
---|
56 | public void ConvertSymbol(ISymbol symbol, TokenWriter writer, CSharpFormattingOptions formattingPolicy) |
---|
57 | { |
---|
58 | if (symbol == null) |
---|
59 | throw new ArgumentNullException("symbol"); |
---|
60 | if (writer == null) |
---|
61 | throw new ArgumentNullException("writer"); |
---|
62 | if (formattingPolicy == null) |
---|
63 | throw new ArgumentNullException("formattingPolicy"); |
---|
64 | |
---|
65 | TypeSystemAstBuilder astBuilder = CreateAstBuilder(); |
---|
66 | AstNode node = astBuilder.ConvertSymbol(symbol); |
---|
67 | EntityDeclaration entityDecl = node as EntityDeclaration; |
---|
68 | if (entityDecl != null) |
---|
69 | PrintModifiers(entityDecl.Modifiers, writer); |
---|
70 | |
---|
71 | if ((ConversionFlags & ConversionFlags.ShowDefinitionKeyword) == ConversionFlags.ShowDefinitionKeyword) { |
---|
72 | if (node is TypeDeclaration) { |
---|
73 | switch (((TypeDeclaration)node).ClassType) { |
---|
74 | case ClassType.Class: |
---|
75 | writer.WriteKeyword(Roles.ClassKeyword, "class"); |
---|
76 | break; |
---|
77 | case ClassType.Struct: |
---|
78 | writer.WriteKeyword(Roles.StructKeyword, "struct"); |
---|
79 | break; |
---|
80 | case ClassType.Interface: |
---|
81 | writer.WriteKeyword(Roles.InterfaceKeyword, "interface"); |
---|
82 | break; |
---|
83 | case ClassType.Enum: |
---|
84 | writer.WriteKeyword(Roles.EnumKeyword, "enum"); |
---|
85 | break; |
---|
86 | default: |
---|
87 | throw new Exception("Invalid value for ClassType"); |
---|
88 | } |
---|
89 | writer.Space(); |
---|
90 | } else if (node is DelegateDeclaration) { |
---|
91 | writer.WriteKeyword(Roles.DelegateKeyword, "delegate"); |
---|
92 | writer.Space(); |
---|
93 | } else if (node is EventDeclaration) { |
---|
94 | writer.WriteKeyword(EventDeclaration.EventKeywordRole, "event"); |
---|
95 | writer.Space(); |
---|
96 | } else if (node is NamespaceDeclaration) { |
---|
97 | writer.WriteKeyword(Roles.NamespaceKeyword, "namespace"); |
---|
98 | writer.Space(); |
---|
99 | } |
---|
100 | } |
---|
101 | |
---|
102 | if ((ConversionFlags & ConversionFlags.ShowReturnType) == ConversionFlags.ShowReturnType) { |
---|
103 | var rt = node.GetChildByRole(Roles.Type); |
---|
104 | if (!rt.IsNull) { |
---|
105 | rt.AcceptVisitor(new CSharpOutputVisitor(writer, formattingPolicy)); |
---|
106 | writer.Space(); |
---|
107 | } |
---|
108 | } |
---|
109 | |
---|
110 | if (symbol is ITypeDefinition) |
---|
111 | WriteTypeDeclarationName((ITypeDefinition)symbol, writer, formattingPolicy); |
---|
112 | else if (symbol is IMember) |
---|
113 | WriteMemberDeclarationName((IMember)symbol, writer, formattingPolicy); |
---|
114 | else |
---|
115 | writer.WriteIdentifier(Identifier.Create(symbol.Name)); |
---|
116 | |
---|
117 | if ((ConversionFlags & ConversionFlags.ShowParameterList) == ConversionFlags.ShowParameterList && HasParameters(symbol)) { |
---|
118 | writer.WriteToken(symbol.SymbolKind == SymbolKind.Indexer ? Roles.LBracket : Roles.LPar, symbol.SymbolKind == SymbolKind.Indexer ? "[" : "("); |
---|
119 | bool first = true; |
---|
120 | foreach (var param in node.GetChildrenByRole(Roles.Parameter)) { |
---|
121 | if (first) { |
---|
122 | first = false; |
---|
123 | } else { |
---|
124 | writer.WriteToken(Roles.Comma, ","); |
---|
125 | writer.Space(); |
---|
126 | } |
---|
127 | param.AcceptVisitor(new CSharpOutputVisitor(writer, formattingPolicy)); |
---|
128 | } |
---|
129 | writer.WriteToken(symbol.SymbolKind == SymbolKind.Indexer ? Roles.RBracket : Roles.RPar, symbol.SymbolKind == SymbolKind.Indexer ? "]" : ")"); |
---|
130 | } |
---|
131 | |
---|
132 | if ((ConversionFlags & ConversionFlags.ShowBody) == ConversionFlags.ShowBody && !(node is TypeDeclaration)) { |
---|
133 | IProperty property = symbol as IProperty; |
---|
134 | if (property != null) { |
---|
135 | writer.Space(); |
---|
136 | writer.WriteToken(Roles.LBrace, "{"); |
---|
137 | writer.Space(); |
---|
138 | if (property.CanGet) { |
---|
139 | writer.WriteKeyword(PropertyDeclaration.GetKeywordRole, "get"); |
---|
140 | writer.WriteToken(Roles.Semicolon, ";"); |
---|
141 | writer.Space(); |
---|
142 | } |
---|
143 | if (property.CanSet) { |
---|
144 | writer.WriteKeyword(PropertyDeclaration.SetKeywordRole, "set"); |
---|
145 | writer.WriteToken(Roles.Semicolon, ";"); |
---|
146 | writer.Space(); |
---|
147 | } |
---|
148 | writer.WriteToken(Roles.RBrace, "}"); |
---|
149 | } else { |
---|
150 | writer.WriteToken(Roles.Semicolon, ";"); |
---|
151 | } |
---|
152 | } |
---|
153 | } |
---|
154 | |
---|
155 | static bool HasParameters(ISymbol e) |
---|
156 | { |
---|
157 | switch (e.SymbolKind) { |
---|
158 | case SymbolKind.TypeDefinition: |
---|
159 | return ((ITypeDefinition)e).Kind == TypeKind.Delegate; |
---|
160 | case SymbolKind.Indexer: |
---|
161 | case SymbolKind.Method: |
---|
162 | case SymbolKind.Operator: |
---|
163 | case SymbolKind.Constructor: |
---|
164 | case SymbolKind.Destructor: |
---|
165 | return true; |
---|
166 | default: |
---|
167 | return false; |
---|
168 | } |
---|
169 | } |
---|
170 | |
---|
171 | TypeSystemAstBuilder CreateAstBuilder() |
---|
172 | { |
---|
173 | TypeSystemAstBuilder astBuilder = new TypeSystemAstBuilder(); |
---|
174 | astBuilder.AddAnnotations = true; |
---|
175 | astBuilder.ShowModifiers = (ConversionFlags & ConversionFlags.ShowModifiers) == ConversionFlags.ShowModifiers; |
---|
176 | astBuilder.ShowAccessibility = (ConversionFlags & ConversionFlags.ShowAccessibility) == ConversionFlags.ShowAccessibility; |
---|
177 | astBuilder.AlwaysUseShortTypeNames = (ConversionFlags & ConversionFlags.UseFullyQualifiedTypeNames) != ConversionFlags.UseFullyQualifiedTypeNames; |
---|
178 | astBuilder.ShowParameterNames = (ConversionFlags & ConversionFlags.ShowParameterNames) == ConversionFlags.ShowParameterNames; |
---|
179 | return astBuilder; |
---|
180 | } |
---|
181 | |
---|
182 | void WriteTypeDeclarationName(ITypeDefinition typeDef, TokenWriter writer, CSharpFormattingOptions formattingPolicy) |
---|
183 | { |
---|
184 | TypeSystemAstBuilder astBuilder = CreateAstBuilder(); |
---|
185 | EntityDeclaration node = astBuilder.ConvertEntity(typeDef); |
---|
186 | if (typeDef.DeclaringTypeDefinition != null) { |
---|
187 | WriteTypeDeclarationName(typeDef.DeclaringTypeDefinition, writer, formattingPolicy); |
---|
188 | writer.WriteToken(Roles.Dot, "."); |
---|
189 | } else if ((ConversionFlags & ConversionFlags.UseFullyQualifiedEntityNames) == ConversionFlags.UseFullyQualifiedEntityNames) { |
---|
190 | if (!string.IsNullOrEmpty(typeDef.Namespace)) { |
---|
191 | WriteQualifiedName(typeDef.Namespace, writer, formattingPolicy); |
---|
192 | writer.WriteToken(Roles.Dot, "."); |
---|
193 | } |
---|
194 | } |
---|
195 | writer.WriteIdentifier(node.NameToken); |
---|
196 | if ((ConversionFlags & ConversionFlags.ShowTypeParameterList) == ConversionFlags.ShowTypeParameterList) { |
---|
197 | var outputVisitor = new CSharpOutputVisitor(writer, formattingPolicy); |
---|
198 | outputVisitor.WriteTypeParameters(node.GetChildrenByRole(Roles.TypeParameter)); |
---|
199 | } |
---|
200 | } |
---|
201 | |
---|
202 | void WriteMemberDeclarationName(IMember member, TokenWriter writer, CSharpFormattingOptions formattingPolicy) |
---|
203 | { |
---|
204 | TypeSystemAstBuilder astBuilder = CreateAstBuilder(); |
---|
205 | EntityDeclaration node = astBuilder.ConvertEntity(member); |
---|
206 | if ((ConversionFlags & ConversionFlags.ShowDeclaringType) == ConversionFlags.ShowDeclaringType) { |
---|
207 | ConvertType(member.DeclaringType, writer, formattingPolicy); |
---|
208 | writer.WriteToken(Roles.Dot, "."); |
---|
209 | } |
---|
210 | switch (member.SymbolKind) { |
---|
211 | case SymbolKind.Indexer: |
---|
212 | writer.WriteKeyword(Roles.Identifier, "this"); |
---|
213 | break; |
---|
214 | case SymbolKind.Constructor: |
---|
215 | WriteQualifiedName(member.DeclaringType.Name, writer, formattingPolicy); |
---|
216 | break; |
---|
217 | case SymbolKind.Destructor: |
---|
218 | writer.WriteToken(DestructorDeclaration.TildeRole, "~"); |
---|
219 | WriteQualifiedName(member.DeclaringType.Name, writer, formattingPolicy); |
---|
220 | break; |
---|
221 | case SymbolKind.Operator: |
---|
222 | switch (member.Name) { |
---|
223 | case "op_Implicit": |
---|
224 | writer.WriteKeyword(OperatorDeclaration.ImplicitRole, "implicit"); |
---|
225 | writer.Space(); |
---|
226 | writer.WriteKeyword(OperatorDeclaration.OperatorKeywordRole, "operator"); |
---|
227 | writer.Space(); |
---|
228 | ConvertType(member.ReturnType, writer, formattingPolicy); |
---|
229 | break; |
---|
230 | case "op_Explicit": |
---|
231 | writer.WriteKeyword(OperatorDeclaration.ExplicitRole, "explicit"); |
---|
232 | writer.Space(); |
---|
233 | writer.WriteKeyword(OperatorDeclaration.OperatorKeywordRole, "operator"); |
---|
234 | writer.Space(); |
---|
235 | ConvertType(member.ReturnType, writer, formattingPolicy); |
---|
236 | break; |
---|
237 | default: |
---|
238 | writer.WriteKeyword(OperatorDeclaration.OperatorKeywordRole, "operator"); |
---|
239 | writer.Space(); |
---|
240 | var operatorType = OperatorDeclaration.GetOperatorType(member.Name); |
---|
241 | if (operatorType.HasValue) |
---|
242 | writer.WriteToken(OperatorDeclaration.GetRole(operatorType.Value), OperatorDeclaration.GetToken(operatorType.Value)); |
---|
243 | else |
---|
244 | writer.WriteIdentifier(node.NameToken); |
---|
245 | break; |
---|
246 | } |
---|
247 | break; |
---|
248 | default: |
---|
249 | writer.WriteIdentifier(Identifier.Create(member.Name)); |
---|
250 | break; |
---|
251 | } |
---|
252 | if ((ConversionFlags & ConversionFlags.ShowTypeParameterList) == ConversionFlags.ShowTypeParameterList && member.SymbolKind == SymbolKind.Method) { |
---|
253 | var outputVisitor = new CSharpOutputVisitor(writer, formattingPolicy); |
---|
254 | outputVisitor.WriteTypeParameters(node.GetChildrenByRole(Roles.TypeParameter)); |
---|
255 | } |
---|
256 | } |
---|
257 | |
---|
258 | void PrintModifiers(Modifiers modifiers, TokenWriter writer) |
---|
259 | { |
---|
260 | foreach (var m in CSharpModifierToken.AllModifiers) { |
---|
261 | if ((modifiers & m) == m) { |
---|
262 | writer.WriteKeyword(EntityDeclaration.ModifierRole, CSharpModifierToken.GetModifierName(m)); |
---|
263 | writer.Space(); |
---|
264 | } |
---|
265 | } |
---|
266 | } |
---|
267 | |
---|
268 | void WriteQualifiedName(string name, TokenWriter writer, CSharpFormattingOptions formattingPolicy) |
---|
269 | { |
---|
270 | var node = AstType.Create(name); |
---|
271 | var outputVisitor = new CSharpOutputVisitor(writer, formattingPolicy); |
---|
272 | node.AcceptVisitor(outputVisitor); |
---|
273 | } |
---|
274 | #endregion |
---|
275 | |
---|
276 | public string ConvertVariable(IVariable v) |
---|
277 | { |
---|
278 | TypeSystemAstBuilder astBuilder = CreateAstBuilder(); |
---|
279 | AstNode astNode = astBuilder.ConvertVariable(v); |
---|
280 | return astNode.ToString().TrimEnd(';', '\r', '\n', (char)8232); |
---|
281 | } |
---|
282 | |
---|
283 | public string ConvertType(IType type) |
---|
284 | { |
---|
285 | if (type == null) |
---|
286 | throw new ArgumentNullException("type"); |
---|
287 | |
---|
288 | TypeSystemAstBuilder astBuilder = CreateAstBuilder(); |
---|
289 | astBuilder.AlwaysUseShortTypeNames = (ConversionFlags & ConversionFlags.UseFullyQualifiedEntityNames) != ConversionFlags.UseFullyQualifiedEntityNames; |
---|
290 | AstType astType = astBuilder.ConvertType(type); |
---|
291 | return astType.ToString(); |
---|
292 | } |
---|
293 | |
---|
294 | public void ConvertType(IType type, TokenWriter writer, CSharpFormattingOptions formattingPolicy) |
---|
295 | { |
---|
296 | TypeSystemAstBuilder astBuilder = CreateAstBuilder(); |
---|
297 | astBuilder.AlwaysUseShortTypeNames = (ConversionFlags & ConversionFlags.UseFullyQualifiedEntityNames) != ConversionFlags.UseFullyQualifiedEntityNames; |
---|
298 | AstType astType = astBuilder.ConvertType(type); |
---|
299 | astType.AcceptVisitor(new CSharpOutputVisitor(writer, formattingPolicy)); |
---|
300 | } |
---|
301 | |
---|
302 | public string ConvertConstantValue(object constantValue) |
---|
303 | { |
---|
304 | return TextWriterTokenWriter.PrintPrimitiveValue(constantValue); |
---|
305 | } |
---|
306 | |
---|
307 | public string WrapComment(string comment) |
---|
308 | { |
---|
309 | return "// " + comment; |
---|
310 | } |
---|
311 | } |
---|
312 | } |
---|