2
// CompletionDataWrapper.cs
5
// Mike KrĆ¼ger <mkrueger@xamarin.com>
7
// Copyright (c) 2011 Xamarin Inc. (http://xamarin.com)
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:
16
// The above copyright notice and this permission notice shall be included in
17
// all copies or substantial portions of the Software.
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
27
using System.Collections.Generic;
28
using ICSharpCode.NRefactory.Completion;
29
using ICSharpCode.NRefactory.TypeSystem;
31
using ICSharpCode.NRefactory.CSharp.Resolver;
33
namespace ICSharpCode.NRefactory.CSharp.Completion
35
class CompletionDataWrapper
37
CSharpCompletionEngine completion;
38
List<ICompletionData> result = new List<ICompletionData> ();
40
public List<ICompletionData> Result {
46
ICompletionDataFactory Factory {
48
return completion.factory;
52
public CompletionDataWrapper (CSharpCompletionEngine completion)
54
this.completion = completion;
57
public void Add (ICompletionData data)
63
public void AddCustom (string displayText, string description = null, string completionText = null)
65
result.Add (Factory.CreateLiteralCompletionData (displayText, description, completionText));
68
HashSet<string> usedNamespaces = new HashSet<string> ();
70
bool IsAccessible(MemberLookup lookup, INamespace ns)
72
if (ns.Types.Any (t => lookup.IsAccessible (t, false)))
74
foreach (var child in ns.ChildNamespaces)
75
if (IsAccessible (lookup, child))
80
public void AddNamespace (MemberLookup lookup, INamespace ns)
82
if (usedNamespaces.Contains (ns.Name))
84
if (!IsAccessible (lookup, ns)) {
85
usedNamespaces.Add (ns.Name);
88
usedNamespaces.Add (ns.Name);
89
result.Add (Factory.CreateNamespaceCompletionData (ns));
92
public void AddAlias(string alias)
94
result.Add (Factory.CreateLiteralCompletionData (alias));
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)
102
throw new ArgumentNullException("type");
103
if (type.Name == "Void" && type.Namespace == "System" || type.Kind == TypeKind.Unknown)
105
if (addedTypes.ContainsKey (type))
106
return addedTypes[type];
108
var def = type.GetDefinition();
109
if (def != null && def.ParentAssembly != completion.ctx.CurrentAssembly && !def.IsBrowsable())
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);
118
typeDisplayText [text] = data;
120
addedTypes[type] = data;
124
Dictionary<string, List<ICompletionData>> data = new Dictionary<string, List<ICompletionData>> ();
126
public ICompletionData AddVariable(IVariable variable)
128
if (data.ContainsKey(variable.Name))
130
data [variable.Name] = new List<ICompletionData>();
131
var cd = Factory.CreateVariableCompletionData(variable);
136
public ICompletionData AddNamedParameterVariable(IVariable variable)
138
var name = variable.Name + ":";
139
if (data.ContainsKey(name))
141
data [name] = new List<ICompletionData>();
143
var cd = Factory.CreateVariableCompletionData(variable);
144
cd.CompletionText += ":";
145
cd.DisplayText += ":";
150
public void AddTypeParameter (ITypeParameter variable)
152
if (data.ContainsKey (variable.Name))
154
data [variable.Name] = new List<ICompletionData> ();
155
result.Add (Factory.CreateVariableCompletionData (variable));
158
public ICompletionData AddMember (IMember member)
160
var newData = Factory.CreateEntityCompletionData (member);
162
if (member.ParentAssembly != completion.ctx.CurrentAssembly && !member.IsBrowsable ())
165
string memberKey = newData.DisplayText;
166
if (memberKey == null)
169
if (member is IMember) {
170
newData.CompletionCategory = GetCompletionCategory (member.DeclaringTypeDefinition);
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)
177
var a = member as IEntity;
178
foreach (var d in existingData) {
179
if (!(d is IEntityCompletionData))
181
var b = ((IEntityCompletionData)d).Entity;
182
if (a == null || b == null || a.EntityType == b.EntityType) {
183
d.AddOverload (newData);
187
if (newData != null) {
188
result.Add (newData);
189
data [memberKey].Add (newData);
192
result.Add (newData);
193
data [memberKey] = new List<ICompletionData> ();
194
data [memberKey].Add (newData);
199
internal CompletionCategory GetCompletionCategory (IType type)
203
if (!completionCategories.ContainsKey (type))
204
completionCategories [type] = new TypeCompletionCategory (type);
205
return completionCategories [type];
208
Dictionary<IType, CompletionCategory> completionCategories = new Dictionary<IType, CompletionCategory> ();
209
class TypeCompletionCategory : CompletionCategory
216
public TypeCompletionCategory (IType type) : base (type.FullName, null)
221
public override int CompareTo (CompletionCategory other)
223
var compareCategory = other as TypeCompletionCategory;
224
if (compareCategory == null)
227
if (Type.ReflectionName == compareCategory.Type.ReflectionName) {
229
} else if (Type.GetAllBaseTypes().Any(t => t.ReflectionName == compareCategory.Type.ReflectionName)) {
231
} else if (compareCategory.Type.GetAllBaseTypes().Any(t => t.ReflectionName == Type.ReflectionName)) {
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) {
240
}else if (ct.IsStatic) {
249
HashSet<IType> addedEnums = new HashSet<IType> ();
250
public ICompletionData AddEnumMembers (IType resolvedType, CSharpResolver state)
252
if (addedEnums.Contains (resolvedType))
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));