[mono/monodevelop] a89b0685: [CSharpBinding] Filter already implemented protocol members.

Mike Krüger ([email protected]) <[email protected]>
Newsgroups gmane.comp.gnome.mono.patches
Message-ID <0000014198470386-cf9af5ac-3354-47f3-b276-49c69797f76b-000000@email.amazonses.com>
   Branch: refs/heads/master
     Home: https://github.com/mono/monodevelop
  Compare: https://github.com/mono/monodevelop/compare/0203acb68fff...a89b068516a7

   Commit: a89b068516a7b137cd254cf135f8dfac232afce9
   Author: Mike Krüger <[email protected]> (mkrueger)
     Date: 2013-10-08 13:33:01 GMT
      URL: https://github.com/mono/monodevelop/commit/a89b068516a7b137cd254cf135f8dfac232afce9

[CSharpBinding] Filter already implemented protocol members.

Changed paths:
  M main/src/addins/CSharpBinding/MonoDevelop.CSharp.CodeGeneration/ExportCodeGenerator.cs
  M main/src/addins/CSharpBinding/MonoDevelop.CSharp.Completion/MonoCSharpCompletionEngine.cs

Modified: main/src/addins/CSharpBinding/MonoDevelop.CSharp.CodeGeneration/ExportCodeGenerator.cs
===================================================================
@@ -28,11 +28,9 @@
 using ICSharpCode.NRefactory.CSharp;
 using MonoDevelop.Core;
 using ICSharpCode.NRefactory.TypeSystem;
-using System;
 using System.Linq;
 using ICSharpCode.NRefactory.CSharp.Refactoring;
 using MonoDevelop.CSharp.Refactoring.CodeActions;
-using MonoDevelop.CSharp.Refactoring;
 using MonoDevelop.CodeGeneration;
 
 namespace MonoDevelop.CodeGeneration
@@ -88,7 +86,7 @@ public static bool HasProtocolAttribute (IType type, out string name)
 			return false;
 		}
 
-		public static ICSharpCode.NRefactory.CSharp.Attribute GenerateExportAttribute (RefactoringContext ctx, IMember member)
+		public static Attribute GenerateExportAttribute (RefactoringContext ctx, IMember member)
 		{
 			if (member == null)
 				return null;
@@ -99,18 +97,18 @@ public static ICSharpCode.NRefactory.CSharp.Attribute GenerateExportAttribute (R
 				astType = new MemberType (new MemberType (new SimpleType ("MonoTouch"), "Foundation"), "Export");
 			}
 
-			var attr = new ICSharpCode.NRefactory.CSharp.Attribute {
+			var attr = new Attribute {
 				Type = astType,
 			};
 			var exportAttribute = member.GetAttribute (new FullTypeName (new TopLevelTypeName ("MonoTouch.Foundation", "ExportAttribute"))); 
 			if (exportAttribute == null || exportAttribute.PositionalArguments.Count == 0)
 				return null;
-			attr.Arguments.Add (new PrimitiveExpression (exportAttribute.PositionalArguments.First ().ConstantValue)); 
+			attr.Arguments.Add (new PrimitiveExpression (exportAttribute.PositionalArguments [0].ConstantValue)); 
 			return attr;
 
 		}
 
-		IMember GetProtocolMember (MDRefactoringContext ctx, IType protocolType, IMember member)
+		static IMember GetProtocolMember (RefactoringContext ctx, IType protocolType, IMember member)
 		{
 			foreach (var m in protocolType.GetMembers (m => m.SymbolKind == member.SymbolKind && m.Name == member.Name)) {
 				if (!SignatureComparer.Ordinal.Equals (m, member))
@@ -128,6 +126,30 @@ IMember GetProtocolMember (MDRefactoringContext ctx, IType protocolType, IMember
 			return null;
 		}
 
+		static string GetProtocol (IMember member)
+		{
+			var attr = member.Attributes.FirstOrDefault (a => a.AttributeType.Name == "ExportAttribute" && a.AttributeType.Namespace == "MonoTouch.Foundation");
+			if (attr == null || attr.PositionalArguments.Count == 0)
+				return null;
+			return attr.PositionalArguments.First ().ConstantValue.ToString ();
+		}
+
+		public static bool IsImplemented (IType type, IMember protocolMember)
+		{
+			foreach (var m in type.GetMembers (m => m.SymbolKind == protocolMember.SymbolKind && m.Name == protocolMember.Name)) {
+				var p = m as IProperty;
+				if (p != null) {
+					if (p.CanGet && ((IProperty)protocolMember).CanGet && GetProtocol (p.Getter) == GetProtocol (((IProperty)protocolMember).Getter))
+						return true;
+					if (p.CanSet && ((IProperty)protocolMember).CanSet && GetProtocol (p.Setter) == GetProtocol (((IProperty)protocolMember).Setter))
+						return true;
+					continue;
+				}
+				if (GetProtocol (m) == GetProtocol (protocolMember))
+					return true;
+			}
+			return false;
+		}
 
 		class ExportMethods : AbstractGenerateAction
 		{
@@ -156,6 +178,8 @@ protected override IEnumerable<object> GetValidMembers ()
 							continue;
 						if (!cg.IsValidMember (member))
 							continue;
+						if (IsImplemented (type, member))
+							continue;
 						if (member.Attributes.Any (a => a.AttributeType.Name == "ExportAttribute" &&  a.AttributeType.Namespace == "MonoTouch.Foundation"))
 							yield return member;
 					}
@@ -164,6 +188,8 @@ protected override IEnumerable<object> GetValidMembers ()
 							continue;
 						if (!cg.IsValidMember (member))
 							continue;
+						if (IsImplemented (type, member))
+							continue;
 						if (member.CanGet && member.Getter.Attributes.Any (a => a.AttributeType.Name == "ExportAttribute" &&  a.AttributeType.Namespace == "MonoTouch.Foundation") ||
 							member.CanSet && member.Setter.Attributes.Any (a => a.AttributeType.Name == "ExportAttribute" &&  a.AttributeType.Namespace == "MonoTouch.Foundation"))
 							yield return member;
@@ -171,8 +197,6 @@ protected override IEnumerable<object> GetValidMembers ()
 				}
 			}
 
-
-
 			protected override IEnumerable<string> GenerateCode (List<object> includedMembers)
 			{
 				var generator = Options.CreateCodeGenerator ();
@@ -192,7 +216,7 @@ internal static string GenerateMemberCode (MDRefactoringContext ctx, TypeSystemA
 		{
 			var method = builder.ConvertEntity (member) as MethodDeclaration;
 			if (method != null) {
-				method.Body = new BlockStatement () {
+				method.Body = new BlockStatement {
 					new ThrowStatement (new ObjectCreateExpression (ctx.CreateShortType ("System", "NotImplementedException")))
 				};
 				method.Modifiers &= ~Modifiers.Virtual;
@@ -208,17 +232,10 @@ internal static string GenerateMemberCode (MDRefactoringContext ctx, TypeSystemA
 			if (property == null)
 				return null;
 			var p = (IProperty)member;
-			var astType = ctx.CreateShortType ("MonoTouch.Foundation", "ExportAttribute");
-			if (astType is SimpleType) {
-				astType = new SimpleType ("Export");
-			}
-			else {
-				astType = new MemberType (new MemberType (new SimpleType ("MonoTouch"), "Foundation"), "Export");
-			}
 			property.Modifiers &= ~Modifiers.Virtual;
 			property.Modifiers &= ~Modifiers.Abstract;
 			if (p.CanGet) {
-				property.Getter.Body = new BlockStatement () {
+				property.Getter.Body = new BlockStatement {
 					new ThrowStatement (new ObjectCreateExpression (ctx.CreateShortType ("System", "NotImplementedException")))
 				};
 				property.Getter.Attributes.Add (new AttributeSection {
@@ -228,7 +245,7 @@ internal static string GenerateMemberCode (MDRefactoringContext ctx, TypeSystemA
 				});
 			}
 			if (p.CanSet) {
-				property.Setter.Body = new BlockStatement () {
+				property.Setter.Body = new BlockStatement {
 					new ThrowStatement (new ObjectCreateExpression (ctx.CreateShortType ("System", "NotImplementedException")))
 				};
 				property.Setter.Attributes.Add (new AttributeSection {

Modified: main/src/addins/CSharpBinding/MonoDevelop.CSharp.Completion/MonoCSharpCompletionEngine.cs
===================================================================
@@ -64,6 +64,8 @@ protected override void AddVirtuals (List<IMember> alreadyInserted, CompletionDa
 			foreach (var member in GetProtocolMembers (curType)) {
 				if (alreadyInserted.Contains (member))
 					continue;
+				if (BaseExportCodeGenerator.IsImplemented (curType, member))
+					continue;
 				alreadyInserted.Add (member);
 				var data = new ProtocolCompletionData (this, declarationBegin, member);
 				col.Add (data);
_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.