-
-
Notifications
You must be signed in to change notification settings - Fork 250
/
Copy pathIntellisenseParser.cs
415 lines (346 loc) · 17.3 KB
/
IntellisenseParser.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using System.Xml.Linq;
using EnvDTE;
using EnvDTE80;
using Microsoft.VisualStudio.Text.Editor;
using Microsoft.VisualStudio.Utilities;
namespace MadsKristensen.EditorExtensions
{
[ContentType("CSharp")]
[ContentType("VisualBasic")]
[TextViewRole(PredefinedTextViewRoles.Document)]
public static class IntellisenseParser
{
private const string DefaultModuleName = "server";
private const string ModuleNameAttributeName = "TypeScriptModule";
private static readonly Regex IsNumber = new Regex("^[0-9a-fx]+[ul]{0,2}$", RegexOptions.IgnoreCase | RegexOptions.Compiled);
internal static class Ext
{
public const string JavaScript = ".js";
public const string TypeScript = ".d.ts";
}
internal static IEnumerable<IntellisenseObject> ProcessFile(ProjectItem item, HashSet<CodeClass> underProcess = null)
{
if (item.FileCodeModel == null)
return null;
List<IntellisenseObject> list = new List<IntellisenseObject>();
if (underProcess == null)
underProcess = new HashSet<CodeClass>();
foreach (CodeElement element in item.FileCodeModel.CodeElements)
{
if (element.Kind == vsCMElement.vsCMElementNamespace)
{
CodeNamespace cn = (CodeNamespace)element;
foreach (CodeElement member in cn.Members)
{
if (ShouldProcess(member))
ProcessElement(member, list, underProcess);
}
}
else if (ShouldProcess(element))
ProcessElement(element, list, underProcess);
}
return new HashSet<IntellisenseObject>(list);
}
private static void ProcessElement(CodeElement element, List<IntellisenseObject> list, HashSet<CodeClass> underProcess)
{
if (element.Kind == vsCMElement.vsCMElementEnum)
{
ProcessEnum((CodeEnum)element, list);
}
else if (element.Kind == vsCMElement.vsCMElementClass)
{
var cc = (CodeClass)element;
// Don't re-generate the intellisense.
if (list.Any(x => x.Name == GetClassName(cc) && x.Namespace == GetNamespace(cc)))
return;
// Collect inherit classes.
CodeClass baseClass = null;
try
{
// To recuse from throwing from a weird case
// where user inherit class from struct and save. As such inheritance is disallowed.
baseClass = cc.Bases.Cast<CodeClass>()
.FirstOrDefault(c => c.FullName != "System.Object");
}
catch { /* Silently continue. */ }
ProcessClass(cc, baseClass, list, underProcess);
var references = new HashSet<string>();
// Process Inheritence.
if (baseClass != null && !underProcess.Contains(baseClass) && !HasIntellisense(baseClass.ProjectItem, Ext.TypeScript, references))
{
list.Last().UpdateReferences(references);
underProcess.Add(baseClass);
list.AddRange(ProcessFile(baseClass.ProjectItem, underProcess));
}
}
}
private static bool ShouldProcess(CodeElement member)
{
return
member.Kind == vsCMElement.vsCMElementClass
|| member.Kind == vsCMElement.vsCMElementEnum;
}
private static void ProcessEnum(CodeEnum element, List<IntellisenseObject> list)
{
IntellisenseObject data = new IntellisenseObject
{
Name = element.Name,
IsEnum = element.Kind == vsCMElement.vsCMElementEnum,
FullName = element.FullName,
Namespace = GetNamespace(element),
Summary = GetSummary(element)
};
foreach (var codeEnum in element.Members.OfType<CodeVariable>())
{
var prop = new IntellisenseProperty
{
Name = codeEnum.Name,
Summary = GetSummary(codeEnum),
InitExpression = GetInitializer(codeEnum.InitExpression)
};
data.Properties.Add(prop);
}
if (data.Properties.Count > 0)
list.Add(data);
}
private static void ProcessClass(CodeClass cc, CodeClass baseClass, List<IntellisenseObject> list, HashSet<CodeClass> underProcess)
{
string baseNs = null;
string baseClassName = null;
string ns = GetNamespace(cc);
string className = GetClassName(cc);
HashSet<string> references = new HashSet<string>();
IList<IntellisenseProperty> properties = GetProperties(cc.Members, new HashSet<string>(), references).ToList();
foreach (CodeElement member in cc.Members)
{
if (ShouldProcess(member))
ProcessElement(member, list, underProcess);
}
if (baseClass != null)
{
baseClassName = GetClassName(baseClass);
baseNs = GetNamespace(baseClass);
}
var intellisenseObject = new IntellisenseObject(properties.ToList(), references)
{
Namespace = ns,
Name = className,
BaseNamespace = baseNs,
BaseName = baseClassName,
FullName = cc.FullName,
Summary = GetSummary(cc)
};
list.Add(intellisenseObject);
}
private static IEnumerable<IntellisenseProperty> GetProperties(CodeElements props, HashSet<string> traversedTypes, HashSet<string> references = null)
{
return from p in props.OfType<CodeProperty>()
where !p.Attributes.Cast<CodeAttribute>().Any(a => a.Name == "IgnoreDataMember")
where p.Getter != null && !p.Getter.IsShared && p.Getter.Access == vsCMAccess.vsCMAccessPublic
select new IntellisenseProperty
{
Name = GetName(p),
Type = GetType(p.Parent, p.Type, traversedTypes, references),
Summary = GetSummary(p)
};
}
private static string GetClassName(CodeClass cc)
{
return GetDataContractName(cc, "Name") ?? cc.Name;
}
private static string GetNamespace(CodeClass cc)
{
return GetDataContractName(cc, "Namespace") ?? GetNamespace(cc.Attributes);
}
private static string GetDataContractName(CodeClass cc, string attrName)
{
var dataContractAttribute = cc.Attributes.Cast<CodeAttribute>().Where(a => a.Name == "DataContract");
if (!dataContractAttribute.Any())
return null;
string name = null;
var keyValues = dataContractAttribute.First().Children.OfType<CodeAttributeArgument>()
.ToDictionary(a => a.Name, a => (a.Value ?? "").Trim('\"', '\''));
if (keyValues.ContainsKey(attrName))
name = keyValues[attrName];
return name;
}
private static string GetNamespace(CodeEnum cc) { return GetNamespace(cc.Attributes); }
private static string GetNamespace(CodeElements attrs)
{
if (attrs == null) return DefaultModuleName;
var namespaceFromAttr = from a in attrs.Cast<CodeAttribute2>()
where a.Name.EndsWith(ModuleNameAttributeName, StringComparison.OrdinalIgnoreCase)
from arg in a.Arguments.Cast<CodeAttributeArgument>()
let v = (arg.Value ?? "").Trim('\"')
where !string.IsNullOrWhiteSpace(v)
select v;
return namespaceFromAttr.FirstOrDefault() ?? DefaultModuleName;
}
private static IntellisenseType GetType(CodeClass rootElement, CodeTypeRef codeTypeRef, HashSet<string> traversedTypes, HashSet<string> references)
{
var isArray = codeTypeRef.TypeKind == vsCMTypeRef.vsCMTypeRefArray;
var isCollection = codeTypeRef.AsString.StartsWith("System.Collections", StringComparison.Ordinal);
var isDictionary = false;
var effectiveTypeRef = codeTypeRef;
if (isArray && codeTypeRef.ElementType != null) effectiveTypeRef = effectiveTypeRef.ElementType;
else if (isCollection) effectiveTypeRef = TryToGuessGenericArgument(rootElement, effectiveTypeRef);
if (isCollection)
{
isDictionary = codeTypeRef.AsString.StartsWith("System.Collections.Generic.Dictionary", StringComparison.Ordinal);
}
var codeClass = effectiveTypeRef.CodeType as CodeClass2;
var codeEnum = effectiveTypeRef.CodeType as CodeEnum;
var isPrimitive = IsPrimitive(effectiveTypeRef);
var result = new IntellisenseType
{
IsArray = !isDictionary && (isArray || isCollection),
IsDictionary = isDictionary,
CodeName = effectiveTypeRef.AsString,
ClientSideReferenceName =
effectiveTypeRef.TypeKind == vsCMTypeRef.vsCMTypeRefCodeType &&
effectiveTypeRef.CodeType.InfoLocation == vsCMInfoLocation.vsCMInfoLocationProject
?
(codeClass != null && HasIntellisense(codeClass.ProjectItem, Ext.TypeScript, references) ? (GetNamespace(codeClass) + "." + GetClassName(codeClass)) : null) ??
(codeEnum != null && HasIntellisense(codeEnum.ProjectItem, Ext.TypeScript, references) ? (GetNamespace(codeEnum) + "." + codeEnum.Name) : null)
: null
};
if (!isPrimitive && codeClass != null && !traversedTypes.Contains(effectiveTypeRef.CodeType.FullName) && !isCollection)
{
traversedTypes.Add(effectiveTypeRef.CodeType.FullName);
result.Shape = GetProperties(effectiveTypeRef.CodeType.Members, traversedTypes, references).ToList();
traversedTypes.Remove(effectiveTypeRef.CodeType.FullName);
}
return result;
}
private static CodeTypeRef TryToGuessGenericArgument(CodeClass rootElement, CodeTypeRef codeTypeRef)
{
var codeTypeRef2 = codeTypeRef as CodeTypeRef2;
if (codeTypeRef2 == null || !codeTypeRef2.IsGeneric) return codeTypeRef;
// There is no way to extract generic parameter as CodeTypeRef or something similar
// (see http://social.msdn.microsoft.com/Forums/vstudio/en-US/09504bdc-2b81-405a-a2f7-158fb721ee90/envdte-envdte80-codetyperef2-and-generic-types?forum=vsx)
// but we can make it work at least for some simple case with the following heuristic:
// 1) get the argument's local name by parsing the type reference's full text
// 2) if it's a known primitive (i.e. string, int, etc.), return that
// 3) otherwise, guess that it's a type from the same namespace and same project,
// and use the project CodeModel to retrieve it by full name
// 4) if CodeModel returns null - well, bad luck, don't have any more guesses
var typeNameAsInCode = codeTypeRef2.AsString.Split('<', '>').ElementAtOrDefault(1) ?? "";
CodeModel projCodeModel;
try
{
projCodeModel = rootElement.ProjectItem.ContainingProject.CodeModel;
}
catch (COMException)
{
projCodeModel = ProjectHelpers.GetActiveProject().CodeModel;
}
var codeType = projCodeModel.CodeTypeFromFullName(TryToGuessFullName(typeNameAsInCode));
if (codeType != null) return projCodeModel.CreateCodeTypeRef(codeType);
return codeTypeRef;
}
private static readonly Dictionary<string, Type> _knownPrimitiveTypes = new Dictionary<string, Type>(StringComparer.OrdinalIgnoreCase) {
{ "string", typeof( string ) },
{ "int", typeof( int ) },
{ "long", typeof( long ) },
{ "short", typeof( short ) },
{ "byte", typeof( byte ) },
{ "uint", typeof( uint ) },
{ "ulong", typeof( ulong ) },
{ "ushort", typeof( ushort ) },
{ "sbyte", typeof( sbyte ) },
{ "float", typeof( float ) },
{ "double", typeof( double ) },
{ "decimal", typeof( decimal ) },
};
private static string TryToGuessFullName(string typeName)
{
Type primitiveType;
if (_knownPrimitiveTypes.TryGetValue(typeName, out primitiveType)) return primitiveType.FullName;
else return typeName;
}
private static bool IsPrimitive(CodeTypeRef codeTypeRef)
{
if (codeTypeRef.TypeKind != vsCMTypeRef.vsCMTypeRefOther && codeTypeRef.TypeKind != vsCMTypeRef.vsCMTypeRefCodeType)
return true;
if (codeTypeRef.AsString.EndsWith("DateTime", StringComparison.Ordinal))
return true;
return false;
}
private static bool HasIntellisense(ProjectItem projectItem, string ext, HashSet<string> references)
{
for (short i = 0; i < projectItem.FileCount; i++)
{
var fileName = projectItem.FileNames[i] + ext;
if (File.Exists(fileName))
{
references.Add(fileName);
return true;
}
}
return false;
}
// Maps attribute name to array of attribute properties to get resultant name from
private static readonly IReadOnlyDictionary<string, string[]> nameAttributes = new Dictionary<string, string[]>
{
{ "DataMember", new [] { "Name" } },
{ "JsonProperty", new [] { "", "PropertyName" } }
};
private static string GetName(CodeProperty property)
{
foreach (CodeAttribute attr in property.Attributes)
{
var className = Path.GetExtension(attr.Name);
if (string.IsNullOrEmpty(className)) className = attr.Name;
string[] argumentNames;
if (!nameAttributes.TryGetValue(className, out argumentNames))
continue;
var value = attr.Children.OfType<CodeAttributeArgument>().FirstOrDefault(a => argumentNames.Contains(a.Name));
if (value == null)
break;
// Strip the leading & trailing quotes
return value.Value.Trim('@', '\'', '"');
}
return property.Name.Trim('@');
}
// External items throw an exception from the DocComment getter
private static string GetSummary(CodeProperty property) { return property.InfoLocation != vsCMInfoLocation.vsCMInfoLocationProject ? null : GetSummary(property.InfoLocation, property.DocComment, property.Comment, property.FullName); }
private static string GetSummary(CodeClass property) { return GetSummary(property.InfoLocation, property.DocComment, property.Comment, property.FullName); }
private static string GetSummary(CodeEnum property) { return GetSummary(property.InfoLocation, property.DocComment, property.Comment, property.FullName); }
private static string GetSummary(CodeVariable property) { return GetSummary(property.InfoLocation, property.DocComment, property.Comment, property.FullName); }
private static string GetSummary(vsCMInfoLocation location, string xmlComment, string inlineComment, string fullName)
{
if (location != vsCMInfoLocation.vsCMInfoLocationProject || (string.IsNullOrWhiteSpace(xmlComment) && string.IsNullOrWhiteSpace(inlineComment)))
return null;
try
{
string summary = XElement.Parse(xmlComment)
.Descendants("summary")
.Select(x => x.Value)
.FirstOrDefault();
if (!string.IsNullOrEmpty(summary)) return summary.Trim();
if (!string.IsNullOrWhiteSpace(inlineComment)) return inlineComment.Trim();
return null;
}
catch (Exception ex)
{
Logger.Log("Couldn't parse XML Doc Comment for " + fullName + ":\n" + ex);
return null;
}
}
private static string GetInitializer(object initExpression)
{
if (initExpression != null)
{
string initializer = initExpression.ToString();
if (IsNumber.IsMatch(initializer)) return initializer;
}
return null;
}
}
}