/NRefactory/ICSharpCode.NRefactory.CSharp/Refactoring/TextReplaceAction.cs

http://github.com/icsharpcode/ILSpy · C# · 134 lines · 43 code · 7 blank · 84 comment · 4 complexity · acb6471cf05005f4224aff49206b6246 MD5 · raw file

  1. //
  2. // TextReplaceChange.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. namespace ICSharpCode.NRefactory.CSharp.Refactoring
  28. {
  29. /// <summary>
  30. /// This is the base action for changes in a text document.
  31. /// </summary>
  32. public abstract class TextReplaceAction : Action
  33. {
  34. /// <summary>
  35. /// Gets or sets the offset.
  36. /// </summary>
  37. /// <value>
  38. /// The offset of the replace.
  39. /// </value>
  40. public int Offset {
  41. get;
  42. set;
  43. }
  44. int removedChars;
  45. /// <summary>
  46. /// Gets or sets the numer of chars to removed.
  47. /// </summary>
  48. /// <value>
  49. /// The numer of chars to remove.
  50. /// </value>
  51. /// <exception cref='ArgumentOutOfRangeException'>
  52. /// Is thrown when an argument passed to a method is invalid because it is outside the allowable range of values as
  53. /// specified by the method.
  54. /// </exception>
  55. public int RemovedChars {
  56. get {
  57. return removedChars;
  58. }
  59. set {
  60. if (value < 0)
  61. throw new ArgumentOutOfRangeException ("RemovedChars", "needs to be >= 0");
  62. removedChars = value;
  63. }
  64. }
  65. /// <summary>
  66. /// Gets or sets the inserted text.
  67. /// </summary>
  68. /// <value>
  69. /// The text to insert.
  70. /// </value>
  71. public virtual string InsertedText {
  72. get;
  73. set;
  74. }
  75. /// <summary>
  76. /// Initializes a new instance of the <see cref="ICSharpCode.NRefactory.CSharp.Refactoring.TextReplaceAction"/> class.
  77. /// </summary>
  78. /// <param name='offset'>
  79. /// The offset of the replace.
  80. /// </param>
  81. /// <param name='removedChars'>
  82. /// The numer of chars to remove.
  83. /// </param>
  84. /// <exception cref='ArgumentOutOfRangeException'>
  85. /// Is thrown when an argument passed to a method is invalid because it is outside the allowable range of values as
  86. /// specified by the method.
  87. /// </exception>
  88. protected TextReplaceAction (int offset, int removedChars)
  89. {
  90. if (removedChars < 0)
  91. throw new ArgumentOutOfRangeException ("removedChars", "removedChars needs to be >= 0");
  92. if (offset < 0)
  93. throw new ArgumentOutOfRangeException ("offset", "offset needs to be >= 0");
  94. this.removedChars = removedChars;
  95. this.Offset = offset;
  96. }
  97. /// <summary>
  98. /// Initializes a new instance of the <see cref="ICSharpCode.NRefactory.CSharp.Refactoring.TextReplaceAction"/> class.
  99. /// </summary>
  100. /// <param name='offset'>
  101. /// The offset of the replace.
  102. /// </param>
  103. /// <param name='removedChars'>
  104. /// The numer of chars to remove.
  105. /// </param>
  106. /// <param name='insertedText'>
  107. /// The text to insert.
  108. /// </param>
  109. /// <exception cref='ArgumentOutOfRangeException'>
  110. /// Is thrown when an argument passed to a method is invalid because it is outside the allowable range of values as
  111. /// specified by the method.
  112. public TextReplaceAction (int offset, int removedChars, string insertedText) : this (offset, removedChars)
  113. {
  114. this.InsertedText = insertedText;
  115. }
  116. /// <summary>
  117. /// Returns a <see cref="System.String"/> that represents the current <see cref="ICSharpCode.NRefactory.CSharp.Refactoring.TextReplaceAction"/>.
  118. /// </summary>
  119. /// <returns>
  120. /// A <see cref="System.String"/> that represents the current <see cref="ICSharpCode.NRefactory.CSharp.Refactoring.TextReplaceAction"/>.
  121. /// </returns>
  122. public override string ToString ()
  123. {
  124. return string.Format ("[TextReplaceAction: Offset={0}, RemovedChars={1}, InsertedText={2}]", Offset, RemovedChars, InsertedText == null ? "<null>" : InsertedText.Replace ("\t", "\\t").Replace ("\n", "\\n").Replace ("\r", "\\r"));
  125. }
  126. }
  127. }