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

http://github.com/icsharpcode/ILSpy · C# · 89 lines · 51 code · 10 blank · 28 comment · 17 complexity · 5cd6190deabd6928b049efdf8dfa6ecf MD5 · raw file

  1. //
  2. // AddAnotherAccessor.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. namespace ICSharpCode.NRefactory.CSharp.Refactoring
  27. {
  28. /// <summary>
  29. /// Add another accessor to a property declaration that has only one.
  30. /// </summary>
  31. public class AddAnotherAccessor : IContextAction
  32. {
  33. public bool IsValid (RefactoringContext context)
  34. {
  35. var pdecl = GetPropertyDeclaration (context);
  36. if (pdecl == null)
  37. return false;
  38. var type = pdecl.Parent as TypeDeclaration;
  39. if (type != null && type.ClassType == ClassType.Interface)
  40. return false;
  41. return pdecl.Setter.IsNull || pdecl.Getter.IsNull;
  42. }
  43. public void Run (RefactoringContext context)
  44. {
  45. var pdecl = GetPropertyDeclaration (context);
  46. var accessorStatement = BuildAccessorStatement (context, pdecl);
  47. Accessor accessor = new Accessor () {
  48. Body = new BlockStatement { accessorStatement }
  49. };
  50. pdecl.AddChild (accessor, pdecl.Setter.IsNull ? PropertyDeclaration.SetterRole : PropertyDeclaration.GetterRole);
  51. using (var script = context.StartScript ()) {
  52. script.InsertBefore (pdecl.RBraceToken, accessor);
  53. script.Select (accessorStatement);
  54. script.FormatText (ctx => GetPropertyDeclaration (context));
  55. }
  56. }
  57. static Statement BuildAccessorStatement (RefactoringContext context, PropertyDeclaration pdecl)
  58. {
  59. if (pdecl.Setter.IsNull && !pdecl.Getter.IsNull) {
  60. var field = RemoveBackingStore.ScanGetter (context, pdecl);
  61. if (field != null)
  62. return new ExpressionStatement (new AssignmentExpression (new IdentifierExpression (field.Name), AssignmentOperatorType.Assign, new IdentifierExpression ("value")));
  63. }
  64. if (!pdecl.Setter.IsNull && pdecl.Getter.IsNull) {
  65. var field = RemoveBackingStore.ScanSetter (context, pdecl);
  66. if (field != null)
  67. return new ReturnStatement (new IdentifierExpression (field.Name));
  68. }
  69. return new ThrowStatement (new ObjectCreateExpression (context.CreateShortType ("System", "NotImplementedException")));
  70. }
  71. static PropertyDeclaration GetPropertyDeclaration (RefactoringContext context)
  72. {
  73. var node = context.GetNode ();
  74. if (node == null)
  75. return null;
  76. return node.Parent as PropertyDeclaration;
  77. }
  78. }
  79. }