1 | // |
---|
2 | // CompletionDataWrapper.cs |
---|
3 | // |
---|
4 | // Author: |
---|
5 | // Mike Krüger <mkrueger@xamarin.com> |
---|
6 | // |
---|
7 | // Copyright (c) 2011 Xamarin Inc. (http://xamarin.com) |
---|
8 | // |
---|
9 | // Permission is hereby granted, free of charge, to any person obtaining a copy |
---|
10 | // of this software and associated documentation files (the "Software"), to deal |
---|
11 | // in the Software without restriction, including without limitation the rights |
---|
12 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
---|
13 | // copies of the Software, and to permit persons to whom the Software is |
---|
14 | // furnished to do so, subject to the following conditions: |
---|
15 | // |
---|
16 | // The above copyright notice and this permission notice shall be included in |
---|
17 | // all copies or substantial portions of the Software. |
---|
18 | // |
---|
19 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
20 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
21 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
22 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
23 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
---|
24 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
---|
25 | // THE SOFTWARE. |
---|
26 | using System; |
---|
27 | using System.Collections.Generic; |
---|
28 | using ICSharpCode.NRefactory.Completion; |
---|
29 | using ICSharpCode.NRefactory.TypeSystem; |
---|
30 | using System.Linq; |
---|
31 | using ICSharpCode.NRefactory.CSharp.Resolver; |
---|
32 | |
---|
33 | namespace ICSharpCode.NRefactory.CSharp.Completion |
---|
34 | { |
---|
35 | public class CompletionDataWrapper |
---|
36 | { |
---|
37 | CSharpCompletionEngine completion; |
---|
38 | List<ICompletionData> result = new List<ICompletionData> (); |
---|
39 | |
---|
40 | public List<ICompletionData> Result { |
---|
41 | get { |
---|
42 | return result; |
---|
43 | } |
---|
44 | } |
---|
45 | |
---|
46 | ICompletionDataFactory Factory { |
---|
47 | get { |
---|
48 | return completion.factory; |
---|
49 | } |
---|
50 | } |
---|
51 | |
---|
52 | internal bool AnonymousDelegateAdded { |
---|
53 | get; |
---|
54 | set; |
---|
55 | } |
---|
56 | |
---|
57 | public CompletionDataWrapper (CSharpCompletionEngine completion) |
---|
58 | { |
---|
59 | this.completion = completion; |
---|
60 | } |
---|
61 | |
---|
62 | public void Add (ICompletionData data) |
---|
63 | { |
---|
64 | result.Add (data); |
---|
65 | } |
---|
66 | |
---|
67 | |
---|
68 | public ICompletionData AddCustom (string displayText, string description = null, string completionText = null) |
---|
69 | { |
---|
70 | var literalCompletionData = Factory.CreateLiteralCompletionData(displayText, description, completionText); |
---|
71 | result.Add(literalCompletionData); |
---|
72 | return literalCompletionData; |
---|
73 | } |
---|
74 | |
---|
75 | HashSet<string> usedNamespaces = new HashSet<string> (); |
---|
76 | |
---|
77 | bool IsAccessible(MemberLookup lookup, INamespace ns) |
---|
78 | { |
---|
79 | if (ns.Types.Any (t => lookup.IsAccessible (t, false))) |
---|
80 | return true; |
---|
81 | foreach (var child in ns.ChildNamespaces) |
---|
82 | if (IsAccessible (lookup, child)) |
---|
83 | return true; |
---|
84 | return false; |
---|
85 | } |
---|
86 | |
---|
87 | public void AddNamespace (MemberLookup lookup, INamespace ns) |
---|
88 | { |
---|
89 | if (usedNamespaces.Contains (ns.Name)) |
---|
90 | return; |
---|
91 | if (!IsAccessible (lookup, ns)) { |
---|
92 | usedNamespaces.Add (ns.Name); |
---|
93 | return; |
---|
94 | } |
---|
95 | usedNamespaces.Add (ns.Name); |
---|
96 | result.Add (Factory.CreateNamespaceCompletionData (ns)); |
---|
97 | } |
---|
98 | |
---|
99 | public void AddAlias(string alias) |
---|
100 | { |
---|
101 | result.Add (Factory.CreateLiteralCompletionData (alias)); |
---|
102 | } |
---|
103 | |
---|
104 | Dictionary<string, ICompletionData> typeDisplayText = new Dictionary<string, ICompletionData> (); |
---|
105 | Dictionary<IType, ICompletionData> addedTypes = new Dictionary<IType, ICompletionData> (); |
---|
106 | |
---|
107 | public ICompletionData AddConstructors(IType type, bool showFullName, bool isInAttributeContext = false) |
---|
108 | { |
---|
109 | return InternalAddType(type, showFullName, isInAttributeContext, true); |
---|
110 | } |
---|
111 | |
---|
112 | public ICompletionData AddType(IType type, bool showFullName, bool isInAttributeContext = false) |
---|
113 | { |
---|
114 | return InternalAddType(type, showFullName, isInAttributeContext, false); |
---|
115 | } |
---|
116 | |
---|
117 | ICompletionData InternalAddType(IType type, bool showFullName, bool isInAttributeContext, bool addConstrurs) |
---|
118 | { |
---|
119 | if (type == null) |
---|
120 | throw new ArgumentNullException("type"); |
---|
121 | if (type.Name == "Void" && type.Namespace == "System" || type.Kind == TypeKind.Unknown) |
---|
122 | return null; |
---|
123 | if (addedTypes.ContainsKey (type)) |
---|
124 | return addedTypes[type]; |
---|
125 | usedNamespaces.Add(type.Name); |
---|
126 | var def = type.GetDefinition(); |
---|
127 | if (def != null && def.ParentAssembly != completion.ctx.CurrentAssembly) { |
---|
128 | switch (completion.EditorBrowsableBehavior) { |
---|
129 | case EditorBrowsableBehavior.Ignore: |
---|
130 | break; |
---|
131 | case EditorBrowsableBehavior.Normal: |
---|
132 | var state = def.GetEditorBrowsableState(); |
---|
133 | if (state != System.ComponentModel.EditorBrowsableState.Always) |
---|
134 | return null; |
---|
135 | break; |
---|
136 | case EditorBrowsableBehavior.IncludeAdvanced: |
---|
137 | if (!def.IsBrowsable()) |
---|
138 | return null; |
---|
139 | break; |
---|
140 | default: |
---|
141 | throw new ArgumentOutOfRangeException(); |
---|
142 | } |
---|
143 | } |
---|
144 | ICompletionData usedType; |
---|
145 | var data = Factory.CreateTypeCompletionData(type, showFullName, isInAttributeContext, addConstrurs); |
---|
146 | var text = data.DisplayText; |
---|
147 | if (typeDisplayText.TryGetValue(text, out usedType)) { |
---|
148 | usedType.AddOverload(data); |
---|
149 | return usedType; |
---|
150 | } |
---|
151 | typeDisplayText [text] = data; |
---|
152 | result.Add(data); |
---|
153 | addedTypes[type] = data; |
---|
154 | return data; |
---|
155 | } |
---|
156 | |
---|
157 | Dictionary<string, List<ICompletionData>> data = new Dictionary<string, List<ICompletionData>> (); |
---|
158 | |
---|
159 | public ICompletionData AddVariable(IVariable variable) |
---|
160 | { |
---|
161 | if (data.ContainsKey(variable.Name)) |
---|
162 | return null; |
---|
163 | data [variable.Name] = new List<ICompletionData>(); |
---|
164 | var cd = Factory.CreateVariableCompletionData(variable); |
---|
165 | result.Add(cd); |
---|
166 | return cd; |
---|
167 | } |
---|
168 | |
---|
169 | public ICompletionData AddNamedParameterVariable(IVariable variable) |
---|
170 | { |
---|
171 | var name = variable.Name + ":"; |
---|
172 | if (data.ContainsKey(name)) |
---|
173 | return null; |
---|
174 | data [name] = new List<ICompletionData>(); |
---|
175 | |
---|
176 | var cd = Factory.CreateVariableCompletionData(variable); |
---|
177 | cd.CompletionText += ":"; |
---|
178 | cd.DisplayText += ":"; |
---|
179 | result.Add(cd); |
---|
180 | return cd; |
---|
181 | } |
---|
182 | |
---|
183 | public void AddTypeParameter (ITypeParameter variable) |
---|
184 | { |
---|
185 | if (data.ContainsKey (variable.Name)) |
---|
186 | return; |
---|
187 | data [variable.Name] = new List<ICompletionData> (); |
---|
188 | result.Add (Factory.CreateVariableCompletionData (variable)); |
---|
189 | } |
---|
190 | |
---|
191 | public void AddTypeImport(ITypeDefinition type, bool useFullName, bool addForTypeCreation) |
---|
192 | { |
---|
193 | result.Add(Factory.CreateImportCompletionData(type, useFullName, addForTypeCreation)); |
---|
194 | } |
---|
195 | |
---|
196 | public ICompletionData AddMember (IMember member) |
---|
197 | { |
---|
198 | var newData = Factory.CreateEntityCompletionData (member); |
---|
199 | |
---|
200 | if (member.ParentAssembly != completion.ctx.CurrentAssembly) { |
---|
201 | switch (completion.EditorBrowsableBehavior) { |
---|
202 | case EditorBrowsableBehavior.Ignore: |
---|
203 | break; |
---|
204 | case EditorBrowsableBehavior.Normal: |
---|
205 | var state = member.GetEditorBrowsableState(); |
---|
206 | if (state != System.ComponentModel.EditorBrowsableState.Always) |
---|
207 | return null; |
---|
208 | break; |
---|
209 | case EditorBrowsableBehavior.IncludeAdvanced: |
---|
210 | if (!member.IsBrowsable()) |
---|
211 | return null; |
---|
212 | break; |
---|
213 | default: |
---|
214 | throw new ArgumentOutOfRangeException(); |
---|
215 | } |
---|
216 | } |
---|
217 | string memberKey = newData.DisplayText; |
---|
218 | if (memberKey == null) |
---|
219 | return null; |
---|
220 | |
---|
221 | newData.CompletionCategory = GetCompletionCategory (member.DeclaringTypeDefinition); |
---|
222 | |
---|
223 | List<ICompletionData> existingData; |
---|
224 | data.TryGetValue (memberKey, out existingData); |
---|
225 | if (existingData != null) { |
---|
226 | if (member.SymbolKind == SymbolKind.Field || member.SymbolKind == SymbolKind.Property || member.SymbolKind == SymbolKind.Event) |
---|
227 | return null; |
---|
228 | var a = member as IEntity; |
---|
229 | foreach (var d in existingData) { |
---|
230 | if (!(d is IEntityCompletionData)) |
---|
231 | continue; |
---|
232 | var b = ((IEntityCompletionData)d).Entity; |
---|
233 | if (a == null || b == null || a.SymbolKind == b.SymbolKind) { |
---|
234 | d.AddOverload (newData); |
---|
235 | return d; |
---|
236 | } |
---|
237 | } |
---|
238 | if (newData != null) { |
---|
239 | result.Add (newData); |
---|
240 | data [memberKey].Add (newData); |
---|
241 | } |
---|
242 | } else { |
---|
243 | result.Add (newData); |
---|
244 | data [memberKey] = new List<ICompletionData> (); |
---|
245 | data [memberKey].Add (newData); |
---|
246 | } |
---|
247 | return newData; |
---|
248 | } |
---|
249 | |
---|
250 | internal CompletionCategory GetCompletionCategory (IType type) |
---|
251 | { |
---|
252 | if (type == null) |
---|
253 | return null; |
---|
254 | if (!completionCategories.ContainsKey (type)) |
---|
255 | completionCategories [type] = new TypeCompletionCategory (type); |
---|
256 | return completionCategories [type]; |
---|
257 | } |
---|
258 | |
---|
259 | Dictionary<IType, CompletionCategory> completionCategories = new Dictionary<IType, CompletionCategory> (); |
---|
260 | class TypeCompletionCategory : CompletionCategory |
---|
261 | { |
---|
262 | public IType Type { |
---|
263 | get; |
---|
264 | private set; |
---|
265 | } |
---|
266 | |
---|
267 | public TypeCompletionCategory (IType type) : base (type.FullName, null) |
---|
268 | { |
---|
269 | this.Type = type; |
---|
270 | } |
---|
271 | |
---|
272 | public override int CompareTo (CompletionCategory other) |
---|
273 | { |
---|
274 | var compareCategory = other as TypeCompletionCategory; |
---|
275 | if (compareCategory == null) |
---|
276 | return -1; |
---|
277 | int result; |
---|
278 | if (Type.ReflectionName == compareCategory.Type.ReflectionName) { |
---|
279 | result = 0; |
---|
280 | } else if (Type.GetAllBaseTypes().Any(t => t.ReflectionName == compareCategory.Type.ReflectionName)) { |
---|
281 | result = -1; |
---|
282 | } else if (compareCategory.Type.GetAllBaseTypes().Any(t => t.ReflectionName == Type.ReflectionName)) { |
---|
283 | result = 1; |
---|
284 | } else { |
---|
285 | var d = Type.GetDefinition (); |
---|
286 | var ct = compareCategory.Type.GetDefinition(); |
---|
287 | if (ct.IsStatic && d.IsStatic) { |
---|
288 | result = d.FullName.CompareTo (ct.FullName); |
---|
289 | } else if (d.IsStatic) { |
---|
290 | result = 1; |
---|
291 | }else if (ct.IsStatic) { |
---|
292 | result = -1; |
---|
293 | } else { |
---|
294 | result = 0; |
---|
295 | } |
---|
296 | } |
---|
297 | return result; |
---|
298 | } |
---|
299 | } |
---|
300 | HashSet<IType> addedEnums = new HashSet<IType> (); |
---|
301 | public ICompletionData AddEnumMembers (IType resolvedType, CSharpResolver state) |
---|
302 | { |
---|
303 | if (addedEnums.Contains (resolvedType)) |
---|
304 | return null; |
---|
305 | addedEnums.Add (resolvedType); |
---|
306 | var result = AddType(resolvedType, true); |
---|
307 | foreach (var field in resolvedType.GetFields ()) { |
---|
308 | if (field.IsPublic && (field.IsConst || field.IsStatic)) { |
---|
309 | Result.Add(Factory.CreateMemberCompletionData(resolvedType, field)); |
---|
310 | } |
---|
311 | } |
---|
312 | return result; |
---|
313 | } |
---|
314 | HashSet<string> anonymousSignatures = new HashSet<string> (); |
---|
315 | |
---|
316 | public bool HasAnonymousDelegateAdded(string signature) |
---|
317 | { |
---|
318 | return anonymousSignatures.Contains(signature); |
---|
319 | } |
---|
320 | |
---|
321 | public void AddAnonymousDelegateAdded(string signature) |
---|
322 | { |
---|
323 | anonymousSignatures.Add(signature); |
---|
324 | } |
---|
325 | } |
---|
326 | } |
---|
327 | |
---|
328 | |
---|