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

« back to all changes in this revision

Viewing changes to src/core/MonoDevelop.Ide/MonoDevelop.Ide.TypeSystem/CodeGenerator.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:
34
34
using MonoDevelop.Ide.TypeSystem;
35
35
using ICSharpCode.NRefactory;
36
36
using MonoDevelop.Projects.Policies;
 
37
using MonoDevelop.Ide.Extensions;
37
38
 
38
39
namespace MonoDevelop.Ide.TypeSystem
39
40
{
78
79
                                return null;
79
80
 
80
81
                        var result = (CodeGenerator)node.CreateInstance ();
81
 
                        result.UseSpaceIndent = doc.Editor.Options.TabsToSpaces;
 
82
                        result.UseSpaceIndent = doc.Editor.TabsToSpaces;
82
83
                        result.EolMarker = doc.Editor.EolMarker;
83
84
                        result.TabSize = doc.Editor.Options.TabSize;
84
85
                        result.Compilation = doc.Compilation;
92
93
                                return null;
93
94
 
94
95
                        var result = (CodeGenerator)node.CreateInstance ();
95
 
                        result.UseSpaceIndent = editor.Options.TabsToSpaces;
 
96
                        result.UseSpaceIndent = editor.TabsToSpaces;
96
97
                        result.EolMarker = editor.EolMarker;
97
98
                        result.TabSize = editor.Options.TabSize;
98
99
                        result.Compilation = compilation;
159
160
                                IndentLevel = AutoIndent ? CodeGenerationService.CalculateBodyIndentLevel (implementingType) : 0;
160
161
                }
161
162
 
162
 
                public string CreateInterfaceImplementation (ITypeDefinition implementingType, IUnresolvedTypeDefinition implementingPart, IType interfaceType, bool explicitly, bool wrapRegions = true)
163
 
                {
164
 
                        SetIndentTo (implementingPart);
165
 
                        StringBuilder result = new StringBuilder ();
166
 
                        List<IMember> implementedMembers = new List<IMember> ();
167
 
                        foreach (var def in interfaceType.GetAllBaseTypes ().Where (bt => bt.Kind == TypeKind.Interface)) {
168
 
                                if (result.Length > 0) {
169
 
                                        AppendLine (result);
170
 
                                        AppendLine (result);
171
 
                                }
172
 
                                string implementation = InternalCreateInterfaceImplementation (implementingType, implementingPart, def, explicitly, implementedMembers);
173
 
                                if (string.IsNullOrWhiteSpace (implementation))
174
 
                                        continue;
175
 
                                if (wrapRegions) {
176
 
                                        result.Append (WrapInRegions (def.Name + " implementation", implementation));
177
 
                                } else {
178
 
                                        result.Append (implementation);
179
 
                                }
180
 
                        }
181
 
                        return result.ToString ();
182
 
                }
183
 
 
184
163
                static bool CompareMethods (IMethod interfaceMethod, IMethod typeMethod)
185
164
                {
186
165
                        if (typeMethod.IsExplicitInterfaceImplementation)
188
167
                        return SignatureComparer.Ordinal.Equals (interfaceMethod, typeMethod);
189
168
                }
190
169
 
191
 
                public static List<KeyValuePair<IMember, bool>> CollectMembersToImplement (ITypeDefinition implementingType, IType interfaceType, bool explicitly)
192
 
                {
193
 
                        var def = interfaceType.GetDefinition ();
194
 
                        List<KeyValuePair<IMember, bool>> toImplement = new List<KeyValuePair<IMember, bool>> ();
195
 
                        bool alreadyImplemented;
196
 
                        // Stub out non-implemented events defined by @iface
197
 
                        foreach (var ev in interfaceType.GetEvents (e => !e.IsSynthetic && e.DeclaringTypeDefinition.ReflectionName == def.ReflectionName)) {
198
 
                                bool needsExplicitly = explicitly;
199
 
                                alreadyImplemented = implementingType.GetAllBaseTypeDefinitions ().Any (x => x.Kind != TypeKind.Interface && x.Events.Any (y => y.Name == ev.Name));
200
 
 
201
 
                                if (!alreadyImplemented)
202
 
                                        toImplement.Add (new KeyValuePair<IMember, bool> (ev, needsExplicitly));
203
 
                        }
204
 
 
205
 
                        // Stub out non-implemented methods defined by @iface
206
 
                        foreach (var method in interfaceType.GetMethods (d => !d.IsSynthetic && d.DeclaringTypeDefinition.ReflectionName == def.ReflectionName)) {
207
 
                                bool needsExplicitly = explicitly;
208
 
                                alreadyImplemented = false;
209
 
                                
210
 
                                foreach (var cmet in implementingType.GetMethods ()) {
211
 
                                        if (CompareMethods (method, cmet)) {
212
 
                                                if (!needsExplicitly && !cmet.ReturnType.Equals (method.ReturnType))
213
 
                                                        needsExplicitly = true;
214
 
                                                else
215
 
                                                        alreadyImplemented |= !needsExplicitly /*|| cmet.InterfaceImplementations.Any (impl => impl.InterfaceType.Equals (interfaceType))*/;
216
 
                                        }
217
 
                                }
218
 
                                if (!alreadyImplemented) 
219
 
                                        toImplement.Add (new KeyValuePair<IMember, bool> (method, needsExplicitly));
220
 
                        }
221
 
 
222
 
                        // Stub out non-implemented properties defined by @iface
223
 
                        foreach (var prop in interfaceType.GetProperties (p => !p.IsSynthetic && p.DeclaringTypeDefinition.ReflectionName == def.ReflectionName)) {
224
 
                                bool needsExplicitly = explicitly;
225
 
                                alreadyImplemented = false;
226
 
                                foreach (var t in implementingType.GetAllBaseTypeDefinitions ()) {
227
 
                                        if (t.Kind == TypeKind.Interface)
228
 
                                                continue;
229
 
                                        foreach (IProperty cprop in t.Properties) {
230
 
                                                if (cprop.Name == prop.Name) {
231
 
                                                        if (!needsExplicitly && !cprop.ReturnType.Equals (prop.ReturnType))
232
 
                                                                needsExplicitly = true;
233
 
                                                        else
234
 
                                                                alreadyImplemented |= !needsExplicitly/* || cprop.InterfaceImplementations.Any (impl => impl.InterfaceType.Resolve (ctx).Equals (interfaceType))*/;
235
 
                                                }
236
 
                                        }
237
 
                                }
238
 
                                if (!alreadyImplemented)
239
 
                                        toImplement.Add (new KeyValuePair<IMember, bool> (prop, needsExplicitly));
240
 
                        }
241
 
                        return toImplement;
242
 
                }
243
 
 
244
 
                protected string InternalCreateInterfaceImplementation (ITypeDefinition implementingType, IUnresolvedTypeDefinition part, IType interfaceType, bool explicitly, List<IMember> implementedMembers)
245
 
                {
246
 
                        StringBuilder result = new StringBuilder ();
247
 
                        var toImplement = CollectMembersToImplement (implementingType, interfaceType, explicitly);
248
 
 
249
 
                        bool first = true;
250
 
                        foreach (var pair in toImplement) {
251
 
                                if (!first) {
252
 
                                        AppendLine (result);
253
 
                                        AppendLine (result);
254
 
                                } else {
255
 
                                        first = false;
256
 
                                }
257
 
                                bool isExplicit = pair.Value;
258
 
                                foreach (var member in implementedMembers.Where (m => m.Name == pair.Key.Name && m.EntityType == pair.Key.EntityType)) {
259
 
                                        
260
 
                                        if (member is IMethod && pair.Key is IMethod) {
261
 
                                                var method = (IMethod)member;
262
 
                                                var othermethod = (IMethod)pair.Key;
263
 
                                                isExplicit = CompareMethods (othermethod, method);
264
 
                                        } else {
265
 
                                                isExplicit = true;
266
 
                                        }
267
 
                                }
268
 
                                result.Append (CreateMemberImplementation (implementingType, part, pair.Key, isExplicit).Code);
269
 
                                implementedMembers.Add (pair.Key);
270
 
                        }
271
 
 
272
 
                        return result.ToString ();
273
 
                }
274
 
 
275
170
                public abstract string WrapInRegions (string regionName, string text);
276
171
                public abstract CodeGeneratorMemberResult CreateMemberImplementation (ITypeDefinition implementingType, IUnresolvedTypeDefinition part, IUnresolvedMember member, bool explicitDeclaration);
277
172
                public abstract CodeGeneratorMemberResult CreateMemberImplementation (ITypeDefinition implementingType, IUnresolvedTypeDefinition part, IMember member, bool explicitDeclaration);