~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/nrefactory/ICSharpCode.NRefactory.CSharp/Completion/CompletionDataWrapper.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
        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
                public CompletionDataWrapper (CSharpCompletionEngine completion)
 
53
                {
 
54
                        this.completion = completion;
 
55
                }
 
56
                
 
57
                public void Add (ICompletionData data)
 
58
                {
 
59
                        result.Add (data);
 
60
                }
 
61
 
 
62
 
 
63
                public void AddCustom (string displayText, string description = null, string completionText = null)
 
64
                {
 
65
                        result.Add (Factory.CreateLiteralCompletionData (displayText, description, completionText));
 
66
                }
 
67
                
 
68
                HashSet<string> usedNamespaces = new HashSet<string> ();
 
69
 
 
70
                bool IsAccessible(MemberLookup lookup, INamespace ns)
 
71
                {
 
72
                        if (ns.Types.Any (t => lookup.IsAccessible (t, false)))
 
73
                                return true;
 
74
                        foreach (var child in ns.ChildNamespaces)
 
75
                                if (IsAccessible (lookup, child))
 
76
                                        return true;
 
77
                        return false;
 
78
                }
 
79
                
 
80
                public void AddNamespace (MemberLookup lookup, INamespace ns)
 
81
                {
 
82
                        if (usedNamespaces.Contains (ns.Name))
 
83
                                return;
 
84
                        if (!IsAccessible (lookup, ns)) {
 
85
                                usedNamespaces.Add (ns.Name);
 
86
                                return;
 
87
                        }
 
88
                        usedNamespaces.Add (ns.Name);
 
89
                        result.Add (Factory.CreateNamespaceCompletionData (ns));
 
90
                }
 
91
 
 
92
                public void AddAlias(string alias)
 
93
                {
 
94
                        result.Add (Factory.CreateLiteralCompletionData (alias));
 
95
                }
 
96
 
 
97
                Dictionary<string, ICompletionData> typeDisplayText = new Dictionary<string, ICompletionData> ();
 
98
                Dictionary<IType, ICompletionData> addedTypes = new Dictionary<IType, ICompletionData> ();
 
99
                public ICompletionData AddType(IType type, bool showFullName, bool isInAttributeContext = false)
 
100
                {
 
101
                        if (type == null)
 
102
                                throw new ArgumentNullException("type");
 
103
                        if (type.Name == "Void" && type.Namespace == "System" || type.Kind == TypeKind.Unknown)
 
104
                                return null;
 
105
                        if (addedTypes.ContainsKey (type))
 
106
                                return addedTypes[type];
 
107
 
 
108
                        var def = type.GetDefinition();
 
109
                        if (def != null && def.ParentAssembly != completion.ctx.CurrentAssembly && !def.IsBrowsable())
 
110
                                return null;
 
111
                        ICompletionData usedType;
 
112
                        var data = Factory.CreateTypeCompletionData(type, showFullName, isInAttributeContext);
 
113
                        var text = data.DisplayText;
 
114
                        if (typeDisplayText.TryGetValue(text, out usedType)) {
 
115
                                usedType.AddOverload(data);
 
116
                                return usedType;
 
117
                        } 
 
118
                        typeDisplayText [text] = data;
 
119
                        result.Add(data);
 
120
                        addedTypes[type] = data;
 
121
                        return data;
 
122
                }
 
123
 
 
124
                Dictionary<string, List<ICompletionData>> data = new Dictionary<string, List<ICompletionData>> ();
 
125
                
 
126
                public ICompletionData AddVariable(IVariable variable)
 
127
                {
 
128
                        if (data.ContainsKey(variable.Name))
 
129
                                return null;
 
130
                        data [variable.Name] = new List<ICompletionData>();
 
131
                        var cd = Factory.CreateVariableCompletionData(variable);
 
132
                        result.Add(cd);
 
133
                        return cd;
 
134
                }
 
135
                
 
136
                public ICompletionData AddNamedParameterVariable(IVariable variable)
 
137
                {
 
138
                        var name = variable.Name + ":";
 
139
                        if (data.ContainsKey(name))
 
140
                                return null;
 
141
                        data [name] = new List<ICompletionData>();
 
142
                        
 
143
                        var cd = Factory.CreateVariableCompletionData(variable);
 
144
                        cd.CompletionText += ":";
 
145
                        cd.DisplayText += ":";
 
146
                        result.Add(cd);
 
147
                        return cd;
 
148
                }
 
149
                
 
150
                public void AddTypeParameter (ITypeParameter variable)
 
151
                {
 
152
                        if (data.ContainsKey (variable.Name))
 
153
                                return;
 
154
                        data [variable.Name] = new List<ICompletionData> ();
 
155
                        result.Add (Factory.CreateVariableCompletionData (variable));
 
156
                }
 
157
 
 
158
                public ICompletionData AddMember (IMember member)
 
159
                {
 
160
                        var newData = Factory.CreateEntityCompletionData (member);
 
161
                        
 
162
                        if (member.ParentAssembly != completion.ctx.CurrentAssembly && !member.IsBrowsable ())
 
163
                                return null;
 
164
 
 
165
                        string memberKey = newData.DisplayText;
 
166
                        if (memberKey == null)
 
167
                                return null;
 
168
 
 
169
                        if (member is IMember) {
 
170
                                newData.CompletionCategory = GetCompletionCategory (member.DeclaringTypeDefinition);
 
171
                        }
 
172
                        List<ICompletionData> existingData;
 
173
                        data.TryGetValue (memberKey, out existingData);
 
174
                        if (existingData != null) {
 
175
                                if (member.EntityType == EntityType.Field || member.EntityType == EntityType.Property || member.EntityType == EntityType.Event)
 
176
                                        return null;
 
177
                                var a = member as IEntity;
 
178
                                foreach (var d in existingData) {
 
179
                                        if (!(d is IEntityCompletionData))
 
180
                                                continue;
 
181
                                        var b = ((IEntityCompletionData)d).Entity;
 
182
                                        if (a == null || b == null || a.EntityType == b.EntityType) {
 
183
                                                d.AddOverload (newData);
 
184
                                                return d;
 
185
                                        } 
 
186
                                }
 
187
                                if (newData != null) {
 
188
                                        result.Add (newData);
 
189
                                        data [memberKey].Add (newData);
 
190
                                }
 
191
                        } else {
 
192
                                result.Add (newData);
 
193
                                data [memberKey] = new List<ICompletionData> ();
 
194
                                data [memberKey].Add (newData);
 
195
                        }
 
196
                        return newData;
 
197
                }
 
198
                
 
199
                internal CompletionCategory GetCompletionCategory (IType type)
 
200
                {
 
201
                        if (type == null)
 
202
                                return null;
 
203
                        if (!completionCategories.ContainsKey (type))
 
204
                                completionCategories [type] = new TypeCompletionCategory (type);
 
205
                        return completionCategories [type];
 
206
                }
 
207
                
 
208
                Dictionary<IType, CompletionCategory> completionCategories = new Dictionary<IType, CompletionCategory> ();
 
209
                class TypeCompletionCategory : CompletionCategory
 
210
                {
 
211
                        public IType Type {
 
212
                                get;
 
213
                                private set;
 
214
                        }
 
215
                        
 
216
                        public TypeCompletionCategory (IType type) : base (type.FullName, null)
 
217
                        {
 
218
                                this.Type = type;
 
219
                        }
 
220
                        
 
221
                        public override int CompareTo (CompletionCategory other)
 
222
                        {
 
223
                                var compareCategory = other as TypeCompletionCategory;
 
224
                                if (compareCategory == null)
 
225
                                        return -1;
 
226
                                int result;
 
227
                                if (Type.ReflectionName == compareCategory.Type.ReflectionName) {
 
228
                                        result = 0;
 
229
                                } else if (Type.GetAllBaseTypes().Any(t => t.ReflectionName == compareCategory.Type.ReflectionName)) {
 
230
                                        result = -1;
 
231
                                } else if (compareCategory.Type.GetAllBaseTypes().Any(t => t.ReflectionName == Type.ReflectionName)) {
 
232
                                        result = 1;
 
233
                                } else {
 
234
                                        var d = Type.GetDefinition ();
 
235
                                        var ct = compareCategory.Type.GetDefinition();
 
236
                                        if (ct.IsStatic && d.IsStatic) {
 
237
                                                result = d.FullName.CompareTo (ct.FullName);
 
238
                                        } else if (d.IsStatic) {
 
239
                                                result = 1;
 
240
                                        }else if (ct.IsStatic) {
 
241
                                                result = -1;
 
242
                                        } else {
 
243
                                                result = 0;
 
244
                                        }
 
245
                                }
 
246
                                return result;
 
247
                        }
 
248
                }
 
249
                HashSet<IType> addedEnums = new HashSet<IType> ();
 
250
                public ICompletionData AddEnumMembers (IType resolvedType, CSharpResolver state)
 
251
                {
 
252
                        if (addedEnums.Contains (resolvedType))
 
253
                                return null;
 
254
                        addedEnums.Add (resolvedType);
 
255
                        var result = AddType(resolvedType, true);
 
256
                        foreach (var field in resolvedType.GetFields ()) {
 
257
                                if (field.IsPublic && (field.IsConst || field.IsStatic)) {
 
258
                                        Result.Add(Factory.CreateMemberCompletionData(resolvedType, field));
 
259
                                }
 
260
                        }
 
261
                        return result;
 
262
                }
 
263
        }
 
264
}
 
265
 
 
266