/NRefactory/ICSharpCode.NRefactory.CSharp/Refactoring/ContextAction/InsertAnonymousMethodSignature.cs

http://github.com/icsharpcode/ILSpy · C# · 94 lines · 55 code · 13 blank · 26 comment · 16 complexity · 868f66d16994f58f0f0a2c53b617de09 MD5 · raw file

  1. //
  2. // InsertAnonymousMethodSignature.cs
  3. //
  4. // Author:
  5. // Mike Krüger <mkrueger@novell.com>
  6. //
  7. // Copyright (c) 2011 Mike Krüger <mkrueger@novell.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.TypeSystem;
  28. using System.Linq;
  29. using System.Text;
  30. namespace ICSharpCode.NRefactory.CSharp.Refactoring
  31. {
  32. public class InsertAnonymousMethodSignature : IContextAction
  33. {
  34. public bool IsValid (RefactoringContext context)
  35. {
  36. IType type;
  37. return GetAnonymousMethodExpression (context, out type) != null;
  38. }
  39. public void Run (RefactoringContext context)
  40. {
  41. IType type;
  42. var anonymousMethodExpression = GetAnonymousMethodExpression (context, out type);
  43. var delegateMethod = type.GetDelegateInvokeMethod ();
  44. var sb = new StringBuilder ("(");
  45. for (int k = 0; k < delegateMethod.Parameters.Count; k++) {
  46. if (k > 0)
  47. sb.Append (", ");
  48. var paramType = delegateMethod.Parameters [k].Type;
  49. sb.Append (paramType.ToString ());
  50. sb.Append (" ");
  51. sb.Append (delegateMethod.Parameters [k].Name);
  52. }
  53. sb.Append (")");
  54. using (var script = context.StartScript ()) {
  55. script.InsertText (context.GetOffset (anonymousMethodExpression.DelegateToken.EndLocation), sb.ToString ());
  56. }
  57. }
  58. static AnonymousMethodExpression GetAnonymousMethodExpression (RefactoringContext context, out IType delegateType)
  59. {
  60. delegateType = null;
  61. var anonymousMethodExpression = context.GetNode<AnonymousMethodExpression> ();
  62. if (anonymousMethodExpression == null || !anonymousMethodExpression.DelegateToken.Contains (context.Location.Line, context.Location.Column) || anonymousMethodExpression.HasParameterList)
  63. return null;
  64. IType resolvedType = null;
  65. var parent = anonymousMethodExpression.Parent;
  66. if (parent is AssignmentExpression) {
  67. resolvedType = context.Resolve (((AssignmentExpression)parent).Left).Type;
  68. } else if (parent is VariableDeclarationStatement) {
  69. resolvedType = context.Resolve (((VariableDeclarationStatement)parent).Type).Type;
  70. } else if (parent is InvocationExpression) {
  71. // TODO: handle invocations
  72. }
  73. if (resolvedType == null)
  74. return null;
  75. delegateType = resolvedType;
  76. if (delegateType.Kind != TypeKind.Delegate)
  77. return null;
  78. return anonymousMethodExpression;
  79. }
  80. }
  81. }