diff options
author | Mike Krüger <mkrueger@xamarin.com> | 2011-12-01 10:17:03 +0400 |
---|---|---|
committer | Mike Krüger <mkrueger@xamarin.com> | 2011-12-01 10:17:03 +0400 |
commit | 55990368eccbc8b19947053a572878af46e74d0b (patch) | |
tree | fbcfb2302c3eb6a48d355629474cae9509a2d515 /main/src | |
parent | 61e8b606f706e4fdcf94ffffb61c77eaf4fdea48 (diff) |
[DesignerService] Updated nrefactory api.
Diffstat (limited to 'main/src')
4 files changed, 109 insertions, 103 deletions
diff --git a/main/src/addins/MonoDevelop.DesignerSupport/MonoDevelop.DesignerSupport/BindingService.cs b/main/src/addins/MonoDevelop.DesignerSupport/MonoDevelop.DesignerSupport/BindingService.cs index 62f316d402..2b7337ae4d 100644 --- a/main/src/addins/MonoDevelop.DesignerSupport/MonoDevelop.DesignerSupport/BindingService.cs +++ b/main/src/addins/MonoDevelop.DesignerSupport/MonoDevelop.DesignerSupport/BindingService.cs @@ -52,62 +52,63 @@ namespace MonoDevelop.DesignerSupport //TODO: currently case-sensitive, so some languages may not like this const bool ignoreCase = false; - public static IMember GetCompatibleMemberInClass (ITypeResolveContext ctx, ITypeDefinition cls, CodeTypeMember member) + public static IUnresolvedMember GetCompatibleMemberInClass (ICompilation ctx, ITypeDefinition cls, CodeTypeMember member) { - //check for identical property names - foreach (var prop in cls.GetProperties (ctx)) { - if (string.Compare (prop.Name, member.Name, StringComparison.OrdinalIgnoreCase) == 0) { - string rt = prop.ReturnType.Resolve (ctx).ReflectionName; - EnsureClassExists (ctx, rt, GetValidRegion (prop)); - CodeMemberProperty memProp = member as CodeMemberProperty; - if (memProp == null || !IsTypeCompatible (ctx, rt, memProp.Type.BaseType)) - throw new MemberExistsException (cls.FullName, MemberType.Property, member, GetValidRegion (prop), cls.Region.FileName); - return prop; - } - } - - //check for identical method names - foreach (var meth in cls.GetMethods (ctx)) { - if (string.Compare (meth.Name, member.Name, StringComparison.OrdinalIgnoreCase) == 0) { - string rt = meth.ReturnType.Resolve (ctx).ReflectionName; - EnsureClassExists (ctx, rt, GetValidRegion (meth)); - CodeMemberMethod memMeth = member as CodeMemberMethod; - if (memMeth == null || !IsTypeCompatible (ctx, rt, memMeth.ReturnType.BaseType)) - throw new MemberExistsException (cls.FullName, MemberType.Method, member, GetValidRegion (meth), cls.Region.FileName); - return meth; - } - } - - //check for identical event names - foreach (var ev in cls.GetEvents (ctx)) { - if (string.Compare (ev.Name, member.Name, StringComparison.OrdinalIgnoreCase) == 0) { - string rt = ev.ReturnType.Resolve (ctx).ReflectionName; - EnsureClassExists (ctx, rt, GetValidRegion (ev)); - CodeMemberEvent memEv = member as CodeMemberEvent; - if (memEv == null || !IsTypeCompatible (ctx, rt, memEv.Type.BaseType)) - throw new MemberExistsException (cls.FullName, MemberType.Event, member, GetValidRegion (ev), cls.Region.FileName); - return ev; - } - } - - //check for identical field names - foreach (var field in cls.GetFields (ctx)) { - if (string.Compare (field.Name, member.Name, StringComparison.OrdinalIgnoreCase) == 0) { - string rt = field.ReturnType.Resolve (ctx).ReflectionName; - EnsureClassExists (ctx, rt, GetValidRegion (field)); - CodeMemberField memField = member as CodeMemberField; - if (memField == null || !IsTypeCompatible (ctx, rt, memField.Type.BaseType)) - throw new MemberExistsException (cls.FullName, MemberType.Field, member, GetValidRegion (field), cls.Region.FileName); - return field; - } - } - - //walk down into base classes, if any - foreach (var baseType in cls.GetBaseTypes (ctx)) { - IMember mem = GetCompatibleMemberInClass (ctx, baseType.GetDefinition (), member); - if (mem != null) - return mem; - } + // TODO Type system conversion +// //check for identical property names +// foreach (var prop in cls.Properties) { +// if (string.Compare (prop.Name, member.Name, StringComparison.OrdinalIgnoreCase) == 0) { +// string rt = prop.ReturnType.ReflectionName; +// EnsureClassExists (ctx, rt, GetValidRegion (prop)); +// CodeMemberProperty memProp = member as CodeMemberProperty; +// if (memProp == null || !IsTypeCompatible (ctx, rt, memProp.Type.BaseType)) +// throw new MemberExistsException (cls.FullName, MemberType.Property, member, GetValidRegion (prop), cls.Region.FileName); +// return prop; +// } +// } +// +// //check for identical method names +// foreach (var meth in cls.Methods) { +// if (string.Compare (meth.Name, member.Name, StringComparison.OrdinalIgnoreCase) == 0) { +// string rt = meth.ReturnType.ReflectionName; +// EnsureClassExists (ctx, rt, GetValidRegion (meth)); +// CodeMemberMethod memMeth = member as CodeMemberMethod; +// if (memMeth == null || !IsTypeCompatible (ctx, rt, memMeth.ReturnType.BaseType)) +// throw new MemberExistsException (cls.FullName, MemberType.Method, member, GetValidRegion (meth), cls.Region.FileName); +// return meth; +// } +// } +// +// //check for identical event names +// foreach (var ev in cls.Events) { +// if (string.Compare (ev.Name, member.Name, StringComparison.OrdinalIgnoreCase) == 0) { +// string rt = ev.ReturnType.ReflectionName; +// EnsureClassExists (ctx, rt, GetValidRegion (ev)); +// CodeMemberEvent memEv = member as CodeMemberEvent; +// if (memEv == null || !IsTypeCompatible (ctx, rt, memEv.Type.BaseType)) +// throw new MemberExistsException (cls.FullName, MemberType.Event, member, GetValidRegion (ev), cls.Region.FileName); +// return ev; +// } +// } +// +// //check for identical field names +// foreach (var field in cls.Fields) { +// if (string.Compare (field.Name, member.Name, StringComparison.OrdinalIgnoreCase) == 0) { +// string rt = field.ReturnType.ReflectionName; +// EnsureClassExists (ctx, rt, GetValidRegion (field)); +// CodeMemberField memField = member as CodeMemberField; +// if (memField == null || !IsTypeCompatible (ctx, rt, memField.Type.BaseType)) +// throw new MemberExistsException (cls.FullName, MemberType.Field, member, GetValidRegion (field), cls.Region.FileName); +// return field; +// } +// } +// +// //walk down into base classes, if any +// foreach (var baseType in cls.GetAllBaseTypeDefinitions ()) { +// IMember mem = GetCompatibleMemberInClass (ctx, baseType, member); +// if (mem != null) +// return mem; +// } //return null if no match return null; @@ -120,7 +121,14 @@ namespace MonoDevelop.DesignerSupport return member.BodyRegion; } - static IType EnsureClassExists (ITypeResolveContext ctx, string className, DomRegion location) + static DomRegion GetValidRegion (IUnresolvedMember member) + { + if (member.BodyRegion.IsEmpty || member.DeclaringTypeDefinition.Region.FileName == FilePath.Null) + return member.DeclaringTypeDefinition.Region; + return member.BodyRegion; + } + + static IType EnsureClassExists (ICompilation ctx, string className, DomRegion location) { string ns; string name; @@ -132,38 +140,39 @@ namespace MonoDevelop.DesignerSupport ns = className.Substring (0, idx); name = className.Substring (idx + 1); } - var cls = ctx.GetTypeDefinition (ns, name, 0, StringComparer.InvariantCulture); + var cls = ctx.MainAssembly.GetTypeDefinition (ns, name, 0); if (cls == null) throw new TypeNotFoundException (className, location, null); return cls; } - static bool IsTypeCompatible (ITypeResolveContext ctx, string existingType, string checkType) + static bool IsTypeCompatible (ICompilation ctx, string existingType, string checkType) { if (existingType == checkType) return true; IType cls = EnsureClassExists (ctx, checkType, DomRegion.Empty); - foreach (var baseType in cls.GetBaseTypes (ctx)) { + foreach (var baseType in cls.GetAllBaseTypeDefinitions ()) { if (IsTypeCompatible (ctx, existingType, baseType.FullName)) return true; } return false; } - public static IMember AddMemberToClass (Project project, ITypeDefinition cls, ITypeDefinition specificPartToAffect, CodeTypeMember member, bool throwIfExists) + public static IBaseEntity AddMemberToClass (Project project, ITypeDefinition cls, IUnresolvedTypeDefinition specificPartToAffect, CodeTypeMember member, bool throwIfExists) { bool isChildClass = false; - foreach (var c in cls.GetParts ()) + foreach (var c in cls.Parts) if (c == specificPartToAffect) isChildClass = true; if (!isChildClass) throw new ArgumentException ("Class specificPartToAffect is not a part of class cls"); - ITypeResolveContext dom = TypeSystemService.GetContext (project); - IMember existingMember = GetCompatibleMemberInClass (dom, cls, member); + var dom = TypeSystemService.GetCompilation (project); + var existingMember = GetCompatibleMemberInClass (dom, cls, member); if (existingMember == null) - return CodeGenerationService.AddCodeDomMember (specificPartToAffect, member); + return CodeGenerationService.AddCodeDomMember (project, specificPartToAffect, member); + if (throwIfExists) throw new MemberExistsException (cls.Name, member, MemberType.Method, existingMember.BodyRegion, cls.Region.FileName); @@ -192,14 +201,14 @@ namespace MonoDevelop.DesignerSupport IType[] pars = new IType[matchMeth.Parameters.Count]; List<IType>[] baseTypes = new List<IType>[matchMeth.Parameters.Count]; for (int i = 0; i < matchMeth.Parameters.Count; i++) { - pars[i] = matchMeth.Parameters[i].Type.Resolve (ctx); - baseTypes[i] = new List<IType> (pars[i].GetAllBaseTypes (ctx)); + pars[i] = matchMeth.Parameters[i].Type; + baseTypes[i] = new List<IType> (pars[i].GetAllBaseTypes ()); } - var matchMethType = matchMeth.ReturnType.Resolve (ctx); + var matchMethType = matchMeth.ReturnType; - foreach (IMethod method in cls.GetMethods (ctx)) { - if (method.IsPrivate || method.Parameters.Count != pars.Length || !method.ReturnType.Resolve (ctx).Equals (matchMethType) + foreach (IMethod method in cls.GetMethods ()) { + if (method.IsPrivate || method.Parameters.Count != pars.Length || !method.ReturnType.Equals (matchMethType) || method.IsInternal) continue; @@ -207,7 +216,7 @@ namespace MonoDevelop.DesignerSupport //compare each parameter for (int i = 0; i < pars.Length; i++) { - var pt = method.Parameters[i].Type.Resolve (ctx); + var pt = method.Parameters[i].Type; bool parCompatible = pars[i].Equals (pt); if (!parCompatible && !baseTypes[i].Any (t => t.Equals (pt))) { allCompatible = false; @@ -222,7 +231,7 @@ namespace MonoDevelop.DesignerSupport public static bool IdentifierExistsInClass (ITypeResolveContext parserContext, ITypeDefinition cls, string identifier) { - return cls.GetMembers (parserContext).Any (m => m.Name == identifier); + return cls.GetMembers ().Any (m => m.Name == identifier); } public static string GenerateIdentifierUniqueInClass (ITypeResolveContext parserContext, ITypeDefinition cls, string trialIdentifier) @@ -242,13 +251,13 @@ namespace MonoDevelop.DesignerSupport //opens the code view with the desired method, creating it if it doesn't already exist - public static void CreateAndShowMember (Project project, ITypeDefinition cls, ITypeDefinition specificPartToAffect, CodeTypeMember member) + public static void CreateAndShowMember (Project project, ITypeDefinition cls, IUnresolvedTypeDefinition specificPartToAffect, CodeTypeMember member) { //only adds the method if it doesn't already exist - IMember mem = AddMemberToClass (project, cls, specificPartToAffect, member, false); + var mem = AddMemberToClass (project, cls, specificPartToAffect, member, false); //some tests in case code refactorer returns bad values - int beginline = specificPartToAffect.GetDefinition ().BodyRegion.BeginLine; + int beginline = specificPartToAffect.BodyRegion.BeginLine; if (!mem.BodyRegion.IsEmpty && mem.BodyRegion.BeginLine >= beginline && mem.BodyRegion.BeginLine <= specificPartToAffect.BodyRegion.EndLine) beginline = mem.BodyRegion.BeginLine; @@ -307,26 +316,26 @@ namespace MonoDevelop.DesignerSupport { if (ev.ReturnType == null) return null; - IType cls = ev.ReturnType.Resolve (context); - if (cls == null) + IType cls = ev.ReturnType; + if (cls.Kind == TypeKind.Unknown) return null; - foreach (var m in cls.GetMethods (context)) + foreach (var m in cls.GetMethods ()) if (m.Name == "Invoke") return m; return null; } //TODO: handle generics - public static IMethod CodeDomToMDDomMethod (CodeMemberMethod method) + public static IUnresolvedMethod CodeDomToMDDomMethod (CodeMemberMethod method) { - var meth = new DefaultMethod (null, method.Name); - meth.ReturnType = new GetClassTypeReference (method.ReturnType.BaseType, 0); + var meth = new DefaultUnresolvedMethod (null, method.Name); + meth.ReturnType = new DefaultUnresolvedTypeDefinition (method.ReturnType.BaseType); CodeDomModifiersToMDDom (meth, method.Attributes); foreach (CodeParameterDeclarationExpression dec in method.Parameters) { - var paramType = new GetClassTypeReference (dec.Type.BaseType, 0); - var par = new DefaultParameter (paramType, dec.Name); + var paramType = new DefaultUnresolvedTypeDefinition (dec.Type.BaseType); + var par = new DefaultUnresolvedParameter (paramType, dec.Name); if (dec.Direction == FieldDirection.Ref) par.IsRef = true; else if (dec.Direction == FieldDirection.Out) @@ -343,7 +352,7 @@ namespace MonoDevelop.DesignerSupport return meth != null ? MDDomToCodeDomMethod (context, meth) : null; } - static void CodeDomModifiersToMDDom (DefaultMethod method, MemberAttributes modifiers) + static void CodeDomModifiersToMDDom (DefaultUnresolvedMethod method, MemberAttributes modifiers) { if ((modifiers & MemberAttributes.FamilyOrAssembly) != 0) { method.Accessibility = Accessibility.ProtectedOrInternal; @@ -431,7 +440,7 @@ namespace MonoDevelop.DesignerSupport { CodeMemberMethod newMethod = new CodeMemberMethod (); newMethod.Name = mi.Name; - string returnType = mi.ReturnType.Resolve (ctx).ReflectionName ?? "System.Void"; + string returnType = mi.ReturnType.ReflectionName ?? "System.Void"; newMethod.ReturnType = new System.CodeDom.CodeTypeReference (returnType); newMethod.Attributes = ApplyMDDomModifiersToCodeDom (mi, newMethod.Attributes); diff --git a/main/src/addins/MonoDevelop.DesignerSupport/MonoDevelop.DesignerSupport/ClassOutlineNodeComparer.cs b/main/src/addins/MonoDevelop.DesignerSupport/MonoDevelop.DesignerSupport/ClassOutlineNodeComparer.cs index 2a5ead50e4..b1fb62dc38 100644 --- a/main/src/addins/MonoDevelop.DesignerSupport/MonoDevelop.DesignerSupport/ClassOutlineNodeComparer.cs +++ b/main/src/addins/MonoDevelop.DesignerSupport/MonoDevelop.DesignerSupport/ClassOutlineNodeComparer.cs @@ -46,8 +46,6 @@ namespace MonoDevelop.DesignerSupport { const string DEFAULT_REGION_NAME = "region"; - ITypeResolveContext ctx; - Ambience ambience; TreeModel model; ClassOutlineSettings settings; @@ -63,9 +61,8 @@ namespace MonoDevelop.DesignerSupport /// The model containing the nodes to compare. /// </param> - public ClassOutlineNodeComparer (ITypeResolveContext ctx, Ambience ambience, ClassOutlineSettings settings, TreeModel model) + public ClassOutlineNodeComparer (Ambience ambience, ClassOutlineSettings settings, TreeModel model) { - this.ctx = ctx; this.ambience = ambience; this.settings = settings; this.model = model; @@ -268,7 +265,7 @@ namespace MonoDevelop.DesignerSupport { if (node is IEntity) { // Return the name without type or parameters - return ambience.GetString (ctx, (IEntity)node, 0); + return ambience.GetString ((IEntity)node, 0); } if (node is FoldingRegion) { diff --git a/main/src/addins/MonoDevelop.DesignerSupport/MonoDevelop.DesignerSupport/ClassOutlineTextEditorExtension.cs b/main/src/addins/MonoDevelop.DesignerSupport/MonoDevelop.DesignerSupport/ClassOutlineTextEditorExtension.cs index 67e75d6d53..370761100f 100644 --- a/main/src/addins/MonoDevelop.DesignerSupport/MonoDevelop.DesignerSupport/ClassOutlineTextEditorExtension.cs +++ b/main/src/addins/MonoDevelop.DesignerSupport/MonoDevelop.DesignerSupport/ClassOutlineTextEditorExtension.cs @@ -105,7 +105,7 @@ namespace MonoDevelop.DesignerSupport outlineTreeModelSort = new TreeModelSort (outlineTreeStore); settings = ClassOutlineSettings.Load (); - comparer = new ClassOutlineNodeComparer (document.TypeResolveContext, GetAmbience (), settings, outlineTreeModelSort); + comparer = new ClassOutlineNodeComparer (GetAmbience (), settings, outlineTreeModelSort); outlineTreeModelSort.SetSortFunc (0, comparer.CompareNodes); outlineTreeModelSort.SetSortColumnId (0, SortType.Ascending); @@ -187,7 +187,7 @@ namespace MonoDevelop.DesignerSupport try { if (MonoDevelop.Ide.MessageService.ShowCustomDialog (dialog) == (int)Gtk.ResponseType.Ok) { dialog.SaveSettings (); - comparer = new ClassOutlineNodeComparer (document.TypeResolveContext, GetAmbience (), settings, outlineTreeModelSort); + comparer = new ClassOutlineNodeComparer (GetAmbience (), settings, outlineTreeModelSort); UpdateSorting (); } } finally { @@ -242,7 +242,7 @@ namespace MonoDevelop.DesignerSupport object o = model.GetValue (iter, 0); Ambience am = GetAmbience (); if (o is IEntity) { - txtRenderer.Text = am.GetString (Document.TypeResolveContext, (IEntity)o, OutputFlags.ClassBrowserEntries); + txtRenderer.Text = am.GetString ((IEntity)o, OutputFlags.ClassBrowserEntries); } else if (o is FoldingRegion) { string name = ((FoldingRegion)o).Name.Trim (); if (string.IsNullOrEmpty (name)) @@ -331,7 +331,7 @@ namespace MonoDevelop.DesignerSupport } } - static void AddTreeClassContents (TreeStore store, TreeIter parent, ParsedDocument parsedDocument, ITypeDefinition cls) + static void AddTreeClassContents (TreeStore store, TreeIter parent, ParsedDocument parsedDocument, IUnresolvedTypeDefinition cls) { List<object> items = new List<object> (); foreach (object o in cls.Members) @@ -376,8 +376,8 @@ namespace MonoDevelop.DesignerSupport } TreeIter childIter = store.AppendValues (currentParent, item); - if (item is ITypeDefinition) - AddTreeClassContents (store, childIter, parsedDocument, (ITypeDefinition)item); + if (item is IUnresolvedTypeDefinition) + AddTreeClassContents (store, childIter, parsedDocument, (IUnresolvedTypeDefinition)item); } } diff --git a/main/src/addins/MonoDevelop.DesignerSupport/MonoDevelop.DesignerSupport/CodeBehind.cs b/main/src/addins/MonoDevelop.DesignerSupport/MonoDevelop.DesignerSupport/CodeBehind.cs index b21595407d..7ab9a0288d 100644 --- a/main/src/addins/MonoDevelop.DesignerSupport/MonoDevelop.DesignerSupport/CodeBehind.cs +++ b/main/src/addins/MonoDevelop.DesignerSupport/MonoDevelop.DesignerSupport/CodeBehind.cs @@ -102,29 +102,29 @@ namespace MonoDevelop.DesignerSupport return null; } - public static IType GetDesignerClass (IType cls) + public static IUnresolvedTypeDefinition GetDesignerClass (IType cls) { - if (cls.GetDefinition ().GetParts ().Count == 1) + if (cls.GetDefinition ().Parts.Count == 1) return null; string designerEnding = ".designer" + Path.GetExtension (cls.GetDefinition ().Region.FileName); - foreach (var c in cls.GetDefinition ().GetParts ()) - if (c.GetDefinition ().Region.FileName.EndsWith (designerEnding, StringComparison.OrdinalIgnoreCase)) + foreach (var c in cls.GetDefinition ().Parts) + if (c.Region.FileName.EndsWith (designerEnding, StringComparison.OrdinalIgnoreCase)) return c; return null; } - public static ITypeDefinition GetNonDesignerClass (IType cls) + public static IUnresolvedTypeDefinition GetNonDesignerClass (IType cls) { - if (cls.GetDefinition ().GetParts ().Count == 1) + if (cls.GetDefinition ().Parts.Count == 1) return null; string designerEnding = ".designer" + Path.GetExtension (cls.GetDefinition ().Region.FileName); - foreach (var c in cls.GetDefinition ().GetParts ()) - if (!c.GetDefinition ().Region.FileName.EndsWith (designerEnding, StringComparison.OrdinalIgnoreCase)) + foreach (var c in cls.GetDefinition ().Parts) + if (!c.Region.FileName.EndsWith (designerEnding, StringComparison.OrdinalIgnoreCase)) return c; return null; |