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

« back to all changes in this revision

Viewing changes to contrib/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
 
 
32
 
namespace ICSharpCode.NRefactory.CSharp.Completion
33
 
{
34
 
        class CompletionDataWrapper
35
 
        {
36
 
                CSharpCompletionEngine completion;
37
 
                List<ICompletionData> result = new List<ICompletionData> ();
38
 
                        
39
 
                public List<ICompletionData> Result {
40
 
                        get {
41
 
                                return result;
42
 
                        }
43
 
                }
44
 
                
45
 
                ICompletionDataFactory Factory {
46
 
                        get {
47
 
                                return completion.factory;
48
 
                        }
49
 
                }
50
 
                        
51
 
                public CompletionDataWrapper (CSharpCompletionEngine completion)
52
 
                {
53
 
                        this.completion = completion;
54
 
                }
55
 
 
56
 
                public void Add (ICompletionData data)
57
 
                {
58
 
                        result.Add (data);
59
 
                }
60
 
 
61
 
                public void AddCustom (string displayText, string description = null, string completionText = null)
62
 
                {
63
 
                        result.Add (Factory.CreateLiteralCompletionData (displayText, description, completionText));
64
 
                }
65
 
                        
66
 
                HashSet<string> usedNamespaces = new HashSet<string> ();
67
 
                        
68
 
                public void AddNamespace (string name)
69
 
                {
70
 
                        if (string.IsNullOrEmpty (name) || usedNamespaces.Contains (name))
71
 
                                return;
72
 
                        usedNamespaces.Add (name);
73
 
                        result.Add (Factory.CreateNamespaceCompletionData (name));
74
 
                }
75
 
                        
76
 
                HashSet<string> usedTypes = new HashSet<string> ();
77
 
 
78
 
                public ICompletionData AddType(IType type, string shortType)
79
 
                {
80
 
                        if (type == null || string.IsNullOrEmpty(shortType) || usedTypes.Contains(shortType))
81
 
                                return null;
82
 
                        usedTypes.Add(shortType);
83
 
                        var iCompletionData = Factory.CreateTypeCompletionData(type, shortType);
84
 
                        result.Add(iCompletionData);
85
 
                        return iCompletionData;
86
 
                }
87
 
                
88
 
                public ICompletionData AddType(IUnresolvedTypeDefinition type, string shortType)
89
 
                {
90
 
                        if (type == null || string.IsNullOrEmpty(shortType) || usedTypes.Contains(shortType))
91
 
                                return null;
92
 
                        usedTypes.Add(shortType);
93
 
                        var iCompletionData = Factory.CreateTypeCompletionData(type, shortType);
94
 
                        result.Add(iCompletionData);
95
 
                        return iCompletionData;
96
 
                }
97
 
                        
98
 
                Dictionary<string, List<ICompletionData>> data = new Dictionary<string, List<ICompletionData>> ();
99
 
                        
100
 
                public ICompletionData AddVariable(IVariable variable)
101
 
                {
102
 
                        if (data.ContainsKey(variable.Name))
103
 
                                return null;
104
 
                        data [variable.Name] = new List<ICompletionData>();
105
 
                        var cd = Factory.CreateVariableCompletionData(variable);
106
 
                        result.Add(cd);
107
 
                        return cd;
108
 
                }
109
 
 
110
 
                public ICompletionData AddNamedParameterVariable(IVariable variable)
111
 
                {
112
 
                        var name = variable.Name + ":";
113
 
                        if (data.ContainsKey(name))
114
 
                                return null;
115
 
                        data [name] = new List<ICompletionData>();
116
 
 
117
 
                        var cd = Factory.CreateVariableCompletionData(variable);
118
 
                        cd.CompletionText += ":";
119
 
                        cd.DisplayText += ":";
120
 
                        result.Add(cd);
121
 
                        return cd;
122
 
                }
123
 
                
124
 
                public void AddTypeParameter (IUnresolvedTypeParameter variable)
125
 
                {
126
 
                        if (data.ContainsKey (variable.Name))
127
 
                                return;
128
 
                        data [variable.Name] = new List<ICompletionData> ();
129
 
                        result.Add (Factory.CreateVariableCompletionData (variable));
130
 
                }
131
 
                        
132
 
                public ICompletionData AddMember (IUnresolvedMember member)
133
 
                {
134
 
                        var newData = Factory.CreateEntityCompletionData (member);
135
 
                        
136
 
//                              newData.HideExtensionParameter = HideExtensionParameter;
137
 
                        string memberKey = newData.DisplayText;
138
 
                        if (memberKey == null)
139
 
                                return null;
140
 
                        if (member is IMember) {
141
 
                                newData.CompletionCategory = GetCompletionCategory (member.DeclaringTypeDefinition.Resolve (completion.ctx));
142
 
                        }
143
 
                        List<ICompletionData> existingData;
144
 
                        data.TryGetValue (memberKey, out existingData);
145
 
                                
146
 
                        if (existingData != null) {
147
 
                                var a = member as IEntity;
148
 
                                foreach (var d in existingData) {
149
 
                                        if (!(d is IEntityCompletionData))
150
 
                                                continue;
151
 
                                        var b = ((IEntityCompletionData)d).Entity;
152
 
                                        if (a == null || b == null || a.EntityType == b.EntityType) {
153
 
                                                d.AddOverload (newData);
154
 
                                                return d;
155
 
                                        } 
156
 
                                }
157
 
                                if (newData != null) {
158
 
                                        result.Add (newData);
159
 
                                        data [memberKey].Add (newData);
160
 
                                }
161
 
                        } else {
162
 
                                result.Add (newData);
163
 
                                data [memberKey] = new List<ICompletionData> ();
164
 
                                data [memberKey].Add (newData);
165
 
                        }
166
 
                        return newData;
167
 
                }
168
 
                
169
 
                public ICompletionData AddMember (IMember member)
170
 
                {
171
 
                        var newData = Factory.CreateEntityCompletionData (member);
172
 
                        
173
 
//                              newData.HideExtensionParameter = HideExtensionParameter;
174
 
                        string memberKey = newData.DisplayText;
175
 
                        if (memberKey == null)
176
 
                                return null;
177
 
                        if (member is IMember) {
178
 
                                newData.CompletionCategory = GetCompletionCategory (member.DeclaringTypeDefinition);
179
 
                        }
180
 
                        List<ICompletionData> existingData;
181
 
                        data.TryGetValue (memberKey, out existingData);
182
 
                                
183
 
                        if (existingData != null) {
184
 
                                var a = member as IEntity;
185
 
                                foreach (var d in existingData) {
186
 
                                        if (!(d is IEntityCompletionData))
187
 
                                                continue;
188
 
                                        var b = ((IEntityCompletionData)d).Entity;
189
 
                                        if (a == null || b == null || a.EntityType == b.EntityType) {
190
 
                                                d.AddOverload (newData);
191
 
                                                return d;
192
 
                                        } 
193
 
                                }
194
 
                                if (newData != null) {
195
 
                                        result.Add (newData);
196
 
                                        data [memberKey].Add (newData);
197
 
                                }
198
 
                        } else {
199
 
                                result.Add (newData);
200
 
                                data [memberKey] = new List<ICompletionData> ();
201
 
                                data [memberKey].Add (newData);
202
 
                        }
203
 
                        return newData;
204
 
                }
205
 
                        
206
 
                internal CompletionCategory GetCompletionCategory (IType type)
207
 
                {
208
 
                        if (type == null)
209
 
                                return null;
210
 
                        if (!completionCategories.ContainsKey (type))
211
 
                                completionCategories [type] = new TypeCompletionCategory (type);
212
 
                        return completionCategories [type];
213
 
                }
214
 
                        
215
 
                Dictionary<IType, CompletionCategory> completionCategories = new Dictionary<IType, CompletionCategory> ();
216
 
                class TypeCompletionCategory : CompletionCategory
217
 
                {
218
 
                        public IType Type {
219
 
                                get;
220
 
                                private set;
221
 
                        }
222
 
                        
223
 
                        public TypeCompletionCategory (IType type) : base (type.FullName, null)
224
 
                        {
225
 
                                this.Type = type;
226
 
                        }
227
 
                        
228
 
                        public override int CompareTo (CompletionCategory other)
229
 
                        {
230
 
                                var compareCategory = other as TypeCompletionCategory;
231
 
                                if (compareCategory == null)
232
 
                                        return -1;
233
 
                                        
234
 
                                if (Type.ReflectionName == compareCategory.Type.ReflectionName)
235
 
                                        return 0;
236
 
                                        
237
 
                                if (Type.GetAllBaseTypes ().Any (t => t.ReflectionName == compareCategory.Type.ReflectionName))
238
 
                                        return -1;
239
 
                                return 1;
240
 
                        }
241
 
                }
242
 
        }
243
 
}
244
 
 
245