[mono/monodevelop] 291e15b0: [CSharpBinding] Added protocol members to override completion.

Mike Krüger ([email protected]) <[email protected]>
Newsgroups gmane.comp.gnome.mono.patches
Message-ID <0000014196afdad3-ef7aea89-8dd1-417c-a56e-7a0749eb669f-000000@email.amazonses.com>
   Branch: refs/heads/master
     Home: https://github.com/mono/monodevelop
  Compare: https://github.com/mono/monodevelop/compare/c63513b95836...291e15b0909b

   Commit: 291e15b0909b6c797c921b4a32dd19a8e4f2a4bc
   Author: Mike Krüger <[email protected]> (mkrueger)
     Date: 2013-10-08 06:08:34 GMT
      URL: https://github.com/mono/monodevelop/commit/291e15b0909b6c797c921b4a32dd19a8e4f2a4bc

[CSharpBinding] Added protocol members to override completion.

Changed paths:
  M main/external/nrefactory
  M main/src/addins/CSharpBinding/CSharpBinding.csproj
  M main/src/addins/CSharpBinding/MonoDevelop.CSharp.CodeGeneration/ExportCodeGenerator.cs
  M main/src/addins/CSharpBinding/MonoDevelop.CSharp.Completion/CSharpCompletionTextEditorExtension.cs
Added paths:
  A main/src/addins/CSharpBinding/MonoDevelop.CSharp.Completion/MonoCSharpCompletionEngine.cs
  A main/src/addins/CSharpBinding/MonoDevelop.CSharp.Completion/ProtocolCompletionData.cs

Modified: main/external/nrefactory
===================================================================
@@ -1 +1 @@
-Subproject commit f1c98de0874d430e40a7bbec4a833a25ab30ed03
+Subproject commit 9341dfd1ac29b50269259d0f58e5513bf8c6f24b

Modified: main/src/addins/CSharpBinding/CSharpBinding.csproj
===================================================================
@@ -129,6 +129,10 @@
       <Project>{DC393B66-92ED-4CAD-AB25-CFEF23F3D7C6}</Project>
       <Name>ICSharpCode.NRefactory.Xml</Name>
     </ProjectReference>
+    <ProjectReference Include="..\..\..\external\nrefactory\ICSharpCode.NRefactory.Tests\ICSharpCode.NRefactory.Tests.csproj">
+      <Project>{63D3B27A-D966-4902-90B3-30290E1692F1}</Project>
+      <Name>ICSharpCode.NRefactory.Tests</Name>
+    </ProjectReference>
   </ItemGroup>
   <ItemGroup>
     <Reference Include="System" />
@@ -324,6 +328,8 @@
     <Compile Include="MonoDevelop.CSharp.Refactoring.CodeIssues\Issues\MonoTODOIssue.cs" />
     <Compile Include="MonoDevelop.CSharp.Refactoring\CSharpCodeGenerationService.cs" />
     <Compile Include="MonoDevelop.CSharp.CodeGeneration\ExportCodeGenerator.cs" />
+    <Compile Include="MonoDevelop.CSharp.Completion\MonoCSharpCompletionEngine.cs" />
+    <Compile Include="MonoDevelop.CSharp.Completion\ProtocolCompletionData.cs" />
   </ItemGroup>
   <ItemGroup>
     <None Include="Makefile.am" />

Modified: main/src/addins/CSharpBinding/MonoDevelop.CSharp.CodeGeneration/ExportCodeGenerator.cs
===================================================================
@@ -110,7 +110,7 @@ public static ICSharpCode.NRefactory.CSharp.Attribute GenerateExportAttribute (R
 
 		}
 
-		IMember GetProtocolMember (RefactoringContext ctx, IType protocolType, IMember member)
+		IMember GetProtocolMember (MDRefactoringContext 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))
@@ -171,6 +171,8 @@ protected override IEnumerable<object> GetValidMembers ()
 				}
 			}
 
+
+
 			protected override IEnumerable<string> GenerateCode (List<object> includedMembers)
 			{
 				var generator = Options.CreateCodeGenerator ();
@@ -181,57 +183,61 @@ protected override IEnumerable<string> GenerateCode (List<object> includedMember
 				var builder = ctx.CreateTypeSystemAstBuilder ();
 
 				foreach (IMember member in includedMembers) {
-					var method = builder.ConvertEntity (member) as MethodDeclaration;
-					if (method != null) {
-						method.Body = new BlockStatement () {
-							new ThrowStatement (new ObjectCreateExpression (ctx.CreateShortType ("System", "NotImplementedException")))
-						};
-						method.Modifiers &= ~Modifiers.Virtual;
-						method.Modifiers &= ~Modifiers.Abstract;
-
-						method.Attributes.Add (new AttributeSection {
-							Attributes = { GenerateExportAttribute (ctx, member) }
-						}); 
-						yield return method.ToString ();
-						continue;
+					yield return GenerateMemberCode (ctx, builder, member);
+				}
+			}
+		}
+	
+		internal static string GenerateMemberCode (MDRefactoringContext ctx, TypeSystemAstBuilder builder, IMember member)
+		{
+			var method = builder.ConvertEntity (member) as MethodDeclaration;
+			if (method != null) {
+				method.Body = new BlockStatement () {
+					new ThrowStatement (new ObjectCreateExpression (ctx.CreateShortType ("System", "NotImplementedException")))
+				};
+				method.Modifiers &= ~Modifiers.Virtual;
+				method.Modifiers &= ~Modifiers.Abstract;
+				method.Attributes.Add (new AttributeSection {
+					Attributes =  {
+						GenerateExportAttribute (ctx, member)
 					}
-					var property = builder.ConvertEntity (member) as PropertyDeclaration;
-					if (property != 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 () {
-								new ThrowStatement (new ObjectCreateExpression (ctx.CreateShortType ("System", "NotImplementedException")))
-							};
-
-							property.Getter.Attributes.Add (new AttributeSection {
-								Attributes = { GenerateExportAttribute (ctx, p.Getter) }
-							}); 
-						}
-						if (p.CanSet) {
-							property.Setter.Body = new BlockStatement () {
-								new ThrowStatement (new ObjectCreateExpression (ctx.CreateShortType ("System", "NotImplementedException")))
-							};
-
-							property.Setter.Attributes.Add (new AttributeSection {
-								Attributes = { GenerateExportAttribute (ctx, p.Setter)  }
-							}); 
-						}
-						yield return property.ToString ();
-						continue;
+				});
+				return method.ToString ();
+			}
+			var property = builder.ConvertEntity (member) as PropertyDeclaration;
+			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 () {
+					new ThrowStatement (new ObjectCreateExpression (ctx.CreateShortType ("System", "NotImplementedException")))
+				};
+				property.Getter.Attributes.Add (new AttributeSection {
+					Attributes =  {
+						GenerateExportAttribute (ctx, p.Getter)
 					}
-				}
+				});
+			}
+			if (p.CanSet) {
+				property.Setter.Body = new BlockStatement () {
+					new ThrowStatement (new ObjectCreateExpression (ctx.CreateShortType ("System", "NotImplementedException")))
+				};
+				property.Setter.Attributes.Add (new AttributeSection {
+					Attributes =  {
+						GenerateExportAttribute (ctx, p.Setter)
+					}
+				});
 			}
+			return property.ToString ();
 		}
 	}
 

Modified: main/src/addins/CSharpBinding/MonoDevelop.CSharp.Completion/CSharpCompletionTextEditorExtension.cs
===================================================================
@@ -325,7 +325,8 @@ ICompletionDataList InternalHandleCodeCompletion (CodeCompletionContext completi
 			if (ctx == null)
 				return null;
 			var completionDataFactory = new CompletionDataFactory (this, new CSharpResolver (ctx));
-			var engine = new CSharpCompletionEngine (
+			var engine = new MonoCSharpCompletionEngine (
+				this,
 				data.Document,
 				CreateContextProvider (),
 				completionDataFactory,

Added: main/src/addins/CSharpBinding/MonoDevelop.CSharp.Completion/MonoCSharpCompletionEngine.cs
===================================================================
@@ -0,0 +1,83 @@
+//
+// MonoCSharpCompletionEngine.cs
+//
+// Author:
+//       Mike Krüger <[email protected]>
+//
+// Copyright (c) 2013 Xamarin Inc. (http://xamarin.com)
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+using System;
+using ICSharpCode.NRefactory.CSharp.Completion;
+using System.Collections.Generic;
+using MonoDevelop.CodeGeneration;
+using ICSharpCode.NRefactory.TypeSystem;
+using System.Linq;
+
+namespace MonoDevelop.CSharp.Completion
+{
+	class MonoCSharpCompletionEngine : CSharpCompletionEngine
+	{
+		CSharpCompletionTextEditorExtension ext;
+
+		public MonoCSharpCompletionEngine (CSharpCompletionTextEditorExtension ext, ICSharpCode.NRefactory.Editor.IDocument document, ICompletionContextProvider completionContextProvider, ICompletionDataFactory factory, ICSharpCode.NRefactory.TypeSystem.IProjectContent content, ICSharpCode.NRefactory.CSharp.TypeSystem.CSharpTypeResolveContext ctx) : base (document, completionContextProvider, factory, content, ctx)
+		{
+			this.ext = ext;
+		}
+
+		protected override void AddVirtuals (List<IMember> alreadyInserted, CompletionDataWrapper col, string modifiers, IType curType, int declarationBegin)
+		{
+			base.AddVirtuals (alreadyInserted, col, modifiers, curType, declarationBegin);
+			foreach (var member in GetProtocolMembers (curType)) {
+				if (alreadyInserted.Contains (member))
+					continue;
+				alreadyInserted.Add (member);
+				var data = new ProtocolCompletionData (ext, declarationBegin, this.currentType, member);
+				col.Add (data);
+			}
+		}
+
+		IEnumerable<IMember> GetProtocolMembers (IType curType)
+		{
+			foreach (var t in curType.DirectBaseTypes) {
+				string name;
+				if (!BaseExportCodeGenerator.HasProtocolAttribute (t, out name))
+					continue;
+				var protocolType = Compilation.FindType (new FullTypeName (new TopLevelTypeName (t.Namespace, name)));
+				if (protocolType == null)
+					break;
+				foreach (var member in protocolType.GetMethods (null, GetMemberOptions.IgnoreInheritedMembers)) {
+					if (member.ImplementedInterfaceMembers.Any () || member.IsAbstract || !member.IsVirtual)
+						continue;
+					if (member.Attributes.Any (a => a.AttributeType.Name == "ExportAttribute" &&  a.AttributeType.Namespace == "MonoTouch.Foundation")) {
+						yield return member;
+					}
+				}
+				foreach (var member in protocolType.GetProperties (null, GetMemberOptions.IgnoreInheritedMembers)) {
+					if (member.ImplementedInterfaceMembers.Any () || member.IsAbstract || !member.IsVirtual)
+						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;
+				}
+			}
+		}
+	}
+}
+

Added: main/src/addins/CSharpBinding/MonoDevelop.CSharp.Completion/ProtocolCompletionData.cs
===================================================================
@@ -0,0 +1,101 @@
+//
+// ProtocolCompletionData.cs
+//
+// Author:
+//       Mike Krüger <[email protected]>
+//
+// Copyright (c) 2013 Xamarin Inc. (http://xamarin.com)
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+using System;
+using ICSharpCode.NRefactory.TypeSystem;
+using MonoDevelop.Ide.CodeCompletion;
+using MonoDevelop.Ide.TypeSystem;
+using System.Linq;
+using MonoDevelop.CodeGeneration;
+using MonoDevelop.CSharp.Refactoring.CodeActions;
+
+namespace MonoDevelop.CSharp.Completion
+{
+	public class ProtocolCompletionData : CompletionData
+	{
+		CSharpCompletionTextEditorExtension ext;
+		IMember member;
+		static Ambience ambience = new CSharpAmbience ();
+		int    declarationBegin;
+		IUnresolvedTypeDefinition  type;
+
+		public bool GenerateBody { get; set; }
+
+		public override TooltipInformation CreateTooltipInformation (bool smartWrap)
+		{
+			return MemberCompletionData.CreateTooltipInformation (ext, null, member, smartWrap);
+		}
+
+		public ProtocolCompletionData (CSharpCompletionTextEditorExtension ext, int declarationBegin, IUnresolvedTypeDefinition type, IMember member) : base (null)
+		{
+			this.ext = ext;
+			this.type   = type;
+			this.member = member;
+
+			this.declarationBegin = declarationBegin;
+			this.GenerateBody = true;
+			this.Icon = member.GetStockIcon ();
+			this.DisplayText = ambience.GetString (member, OutputFlags.IncludeParameters | OutputFlags.IncludeParameterName | OutputFlags.IncludeGenerics | OutputFlags.HideExtensionsParameter| OutputFlags.IncludeAccessor);
+			this.CompletionText = member.SymbolKind == SymbolKind.Indexer ? "this" : member.Name;
+		}
+
+		public override void InsertCompletionText (CompletionListWindow window, ref KeyActions ka, Gdk.Key closeChar, char keyChar, Gdk.ModifierType modifier)
+		{
+			var editor = ext.TextEditorData;
+			var generator = CodeGenerator.CreateGenerator (ext.Document);
+			bool isExplicit = false;
+			if (member.DeclaringTypeDefinition.Kind == TypeKind.Interface) {
+				foreach (var m in type.Members) {
+					if (m.Name == member.Name && !m.ReturnType.Equals (member.ReturnType)) {
+						isExplicit = true;
+						break;
+					}
+				}
+			}
+			var resolvedType = type.Resolve (ext.Project).GetDefinition ();
+			if (ext.Project != null)
+				generator.PolicyParent = ext.Project.Policies;
+			var ctx = MDRefactoringContext.Create (ext.Document, ext.Document.Editor.Caret.Location);
+			if (ctx == null)
+				return;
+			var builder = ctx.CreateTypeSystemAstBuilder ();
+
+			string sb = BaseExportCodeGenerator.GenerateMemberCode (ctx, builder, member);
+			sb = sb.TrimEnd ();
+
+			//	var lastRegion = result.BodyRegions.LastOrDefault ();
+			//targetCaretPosition = declarationBegin + sb.Length;
+
+			editor.Replace (declarationBegin, editor.Caret.Offset - declarationBegin, sb);
+			/*			if (selectionEndPosition > 0) {
+				editor.Caret.Offset = selectionEndPosition;
+				editor.SetSelection (targetCaretPosition, selectionEndPosition);
+			} else {
+				editor.Caret.Offset = targetCaretPosition;
+			}*/
+		}
+	}
+}
+
_______________________________________________
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.