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;
32
namespace ICSharpCode.NRefactory.CSharp.Completion
34
class CompletionDataWrapper
36
CSharpCompletionEngine completion;
37
List<ICompletionData> result = new List<ICompletionData> ();
39
public List<ICompletionData> Result {
45
ICompletionDataFactory Factory {
47
return completion.factory;
51
public CompletionDataWrapper (CSharpCompletionEngine completion)
53
this.completion = completion;
56
public void Add (ICompletionData data)
61
public void AddCustom (string displayText, string description = null, string completionText = null)
63
result.Add (Factory.CreateLiteralCompletionData (displayText, description, completionText));
66
HashSet<string> usedNamespaces = new HashSet<string> ();
68
public void AddNamespace (string name)
70
if (string.IsNullOrEmpty (name) || usedNamespaces.Contains (name))
72
usedNamespaces.Add (name);
73
result.Add (Factory.CreateNamespaceCompletionData (name));
76
HashSet<string> usedTypes = new HashSet<string> ();
78
public ICompletionData AddType(IType type, string shortType)
80
if (type == null || string.IsNullOrEmpty(shortType) || usedTypes.Contains(shortType))
82
usedTypes.Add(shortType);
83
var iCompletionData = Factory.CreateTypeCompletionData(type, shortType);
84
result.Add(iCompletionData);
85
return iCompletionData;
88
public ICompletionData AddType(IUnresolvedTypeDefinition type, string shortType)
90
if (type == null || string.IsNullOrEmpty(shortType) || usedTypes.Contains(shortType))
92
usedTypes.Add(shortType);
93
var iCompletionData = Factory.CreateTypeCompletionData(type, shortType);
94
result.Add(iCompletionData);
95
return iCompletionData;
98
Dictionary<string, List<ICompletionData>> data = new Dictionary<string, List<ICompletionData>> ();
100
public ICompletionData AddVariable(IVariable variable)
102
if (data.ContainsKey(variable.Name))
104
data [variable.Name] = new List<ICompletionData>();
105
var cd = Factory.CreateVariableCompletionData(variable);
110
public ICompletionData AddNamedParameterVariable(IVariable variable)
112
var name = variable.Name + ":";
113
if (data.ContainsKey(name))
115
data [name] = new List<ICompletionData>();
117
var cd = Factory.CreateVariableCompletionData(variable);
118
cd.CompletionText += ":";
119
cd.DisplayText += ":";
124
public void AddTypeParameter (IUnresolvedTypeParameter variable)
126
if (data.ContainsKey (variable.Name))
128
data [variable.Name] = new List<ICompletionData> ();
129
result.Add (Factory.CreateVariableCompletionData (variable));
132
public ICompletionData AddMember (IUnresolvedMember member)
134
var newData = Factory.CreateEntityCompletionData (member);
136
// newData.HideExtensionParameter = HideExtensionParameter;
137
string memberKey = newData.DisplayText;
138
if (memberKey == null)
140
if (member is IMember) {
141
newData.CompletionCategory = GetCompletionCategory (member.DeclaringTypeDefinition.Resolve (completion.ctx));
143
List<ICompletionData> existingData;
144
data.TryGetValue (memberKey, out existingData);
146
if (existingData != null) {
147
var a = member as IEntity;
148
foreach (var d in existingData) {
149
if (!(d is IEntityCompletionData))
151
var b = ((IEntityCompletionData)d).Entity;
152
if (a == null || b == null || a.EntityType == b.EntityType) {
153
d.AddOverload (newData);
157
if (newData != null) {
158
result.Add (newData);
159
data [memberKey].Add (newData);
162
result.Add (newData);
163
data [memberKey] = new List<ICompletionData> ();
164
data [memberKey].Add (newData);
169
public ICompletionData AddMember (IMember member)
171
var newData = Factory.CreateEntityCompletionData (member);
173
// newData.HideExtensionParameter = HideExtensionParameter;
174
string memberKey = newData.DisplayText;
175
if (memberKey == null)
177
if (member is IMember) {
178
newData.CompletionCategory = GetCompletionCategory (member.DeclaringTypeDefinition);
180
List<ICompletionData> existingData;
181
data.TryGetValue (memberKey, out existingData);
183
if (existingData != null) {
184
var a = member as IEntity;
185
foreach (var d in existingData) {
186
if (!(d is IEntityCompletionData))
188
var b = ((IEntityCompletionData)d).Entity;
189
if (a == null || b == null || a.EntityType == b.EntityType) {
190
d.AddOverload (newData);
194
if (newData != null) {
195
result.Add (newData);
196
data [memberKey].Add (newData);
199
result.Add (newData);
200
data [memberKey] = new List<ICompletionData> ();
201
data [memberKey].Add (newData);
206
internal CompletionCategory GetCompletionCategory (IType type)
210
if (!completionCategories.ContainsKey (type))
211
completionCategories [type] = new TypeCompletionCategory (type);
212
return completionCategories [type];
215
Dictionary<IType, CompletionCategory> completionCategories = new Dictionary<IType, CompletionCategory> ();
216
class TypeCompletionCategory : CompletionCategory
223
public TypeCompletionCategory (IType type) : base (type.FullName, null)
228
public override int CompareTo (CompletionCategory other)
230
var compareCategory = other as TypeCompletionCategory;
231
if (compareCategory == null)
234
if (Type.ReflectionName == compareCategory.Type.ReflectionName)
237
if (Type.GetAllBaseTypes ().Any (t => t.ReflectionName == compareCategory.Type.ReflectionName))