PageRenderTime 59ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/main/src/addins/CSharpBinding/MonoDevelop.CSharp.CodeGeneration/RaiseEventMethodGenerator.cs

https://bitbucket.org/anicolaspp/monodevelop
C# | 139 lines | 100 code | 14 blank | 25 comment | 11 complexity | 49eacd0c86820251945e2efeffeafaf1 MD5 | raw file
  1. //
  2. // RaiseEventMethodGenerator.cs
  3. //
  4. // Author:
  5. // Mike Krüger <mkrueger@xamarin.com>
  6. //
  7. // Copyright (c) 2012 Xamarin <http://xamarin.com>
  8. //
  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:
  15. //
  16. // The above copyright notice and this permission notice shall be included in
  17. // all copies or substantial portions of the Software.
  18. //
  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
  25. // THE SOFTWARE.
  26. using System;
  27. using ICSharpCode.NRefactory.CSharp;
  28. using MonoDevelop.Core;
  29. using MonoDevelop.Ide.Gui;
  30. using Gtk;
  31. using System.Collections.Generic;
  32. using MonoDevelop.Refactoring;
  33. using System.Text;
  34. using ICSharpCode.NRefactory.TypeSystem;
  35. using System.Linq;
  36. namespace MonoDevelop.CodeGeneration
  37. {
  38. public class RaiseEventMethodGenerator : ICodeGenerator
  39. {
  40. public string Icon {
  41. get {
  42. return "md-event";
  43. }
  44. }
  45. public string Text {
  46. get {
  47. return GettextCatalog.GetString ("Event OnXXX method");
  48. }
  49. }
  50. public string GenerateDescription {
  51. get {
  52. return GettextCatalog.GetString ("Select event to generate the method for.");
  53. }
  54. }
  55. public bool IsValid (CodeGenerationOptions options)
  56. {
  57. return new CreateEventMethod (options).IsValid ();
  58. }
  59. public IGenerateAction InitalizeSelection (CodeGenerationOptions options, Gtk.TreeView treeView)
  60. {
  61. var createEventMethod = new CreateEventMethod (options);
  62. createEventMethod.Initialize (treeView);
  63. return createEventMethod;
  64. }
  65. class CreateEventMethod : AbstractGenerateAction
  66. {
  67. const string handlerName = "handler";
  68. public CreateEventMethod (CodeGenerationOptions options) : base (options)
  69. {
  70. }
  71. static string GetEventMethodName (IMember member)
  72. {
  73. return "On" + member.Name;
  74. }
  75. protected override IEnumerable<object> GetValidMembers ()
  76. {
  77. if (Options.EnclosingType == null || Options.EnclosingMember != null)
  78. yield break;
  79. foreach (var e in Options.EnclosingType.Events) {
  80. if (e.IsSynthetic)
  81. continue;
  82. var invokeMethod = e.ReturnType.GetDelegateInvokeMethod ();
  83. if (invokeMethod == null)
  84. continue;
  85. if (Options.EnclosingType.GetMethods (m => m.Name == GetEventMethodName (e)).Any ())
  86. continue;
  87. yield return e;
  88. }
  89. }
  90. protected override IEnumerable<string> GenerateCode (List<object> includedMembers)
  91. {
  92. foreach (IMember member in includedMembers) {
  93. var invokeMethod = member.ReturnType.GetDelegateInvokeMethod ();
  94. if (invokeMethod == null)
  95. continue;
  96. var methodDeclaration = new MethodDeclaration () {
  97. Name = GetEventMethodName (member),
  98. ReturnType = new PrimitiveType ("void"),
  99. Modifiers = Modifiers.Protected | Modifiers.Virtual,
  100. Parameters = {
  101. new ParameterDeclaration (Options.CreateShortType (invokeMethod.Parameters [1].Type), invokeMethod.Parameters [1].Name)
  102. },
  103. Body = new BlockStatement () {
  104. new VariableDeclarationStatement (
  105. new SimpleType ("var"),//Options.CreateShortType (member.ReturnType),
  106. handlerName,
  107. new MemberReferenceExpression (new ThisReferenceExpression (), member.Name)
  108. ),
  109. new IfElseStatement () {
  110. Condition = new BinaryOperatorExpression (new IdentifierExpression (handlerName), BinaryOperatorType.InEquality, new PrimitiveExpression (null)),
  111. TrueStatement = new ExpressionStatement (new InvocationExpression (new IdentifierExpression (handlerName)) {
  112. Arguments = {
  113. new ThisReferenceExpression (),
  114. new IdentifierExpression (invokeMethod.Parameters [1].Name)
  115. }
  116. })
  117. }
  118. }
  119. };
  120. yield return methodDeclaration.GetText (Options.FormattingOptions);
  121. }
  122. }
  123. }
  124. }
  125. }