/AvalonEdit/ICSharpCode.AvalonEdit/Document/DocumentChangeEventArgs.cs

http://github.com/icsharpcode/ILSpy · C# · 123 lines · 70 code · 11 blank · 42 comment · 10 complexity · f6767280c18a08367346e1945f04877f MD5 · raw file

  1. // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy of this
  4. // software and associated documentation files (the "Software"), to deal in the Software
  5. // without restriction, including without limitation the rights to use, copy, modify, merge,
  6. // publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
  7. // to whom the Software is furnished to do so, subject to the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be included in all copies or
  10. // substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  13. // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  14. // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  15. // FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  16. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  17. // DEALINGS IN THE SOFTWARE.
  18. using System;
  19. using ICSharpCode.AvalonEdit.Document;
  20. using ICSharpCode.NRefactory.Editor;
  21. namespace ICSharpCode.AvalonEdit.Document
  22. {
  23. /// <summary>
  24. /// Describes a change of the document text.
  25. /// This class is thread-safe.
  26. /// </summary>
  27. [Serializable]
  28. public class DocumentChangeEventArgs : TextChangeEventArgs
  29. {
  30. volatile OffsetChangeMap offsetChangeMap;
  31. /// <summary>
  32. /// Gets the OffsetChangeMap associated with this document change.
  33. /// </summary>
  34. /// <remarks>The OffsetChangeMap instance is guaranteed to be frozen and thus thread-safe.</remarks>
  35. public OffsetChangeMap OffsetChangeMap {
  36. get {
  37. OffsetChangeMap map = offsetChangeMap;
  38. if (map == null) {
  39. // create OffsetChangeMap on demand
  40. map = OffsetChangeMap.FromSingleElement(CreateSingleChangeMapEntry());
  41. offsetChangeMap = map;
  42. }
  43. return map;
  44. }
  45. }
  46. internal OffsetChangeMapEntry CreateSingleChangeMapEntry()
  47. {
  48. return new OffsetChangeMapEntry(this.Offset, this.RemovalLength, this.InsertionLength);
  49. }
  50. /// <summary>
  51. /// Gets the OffsetChangeMap, or null if the default offset map (=single replacement) is being used.
  52. /// </summary>
  53. internal OffsetChangeMap OffsetChangeMapOrNull {
  54. get {
  55. return offsetChangeMap;
  56. }
  57. }
  58. /// <summary>
  59. /// Gets the new offset where the specified offset moves after this document change.
  60. /// </summary>
  61. public override int GetNewOffset(int offset, AnchorMovementType movementType = AnchorMovementType.Default)
  62. {
  63. if (offsetChangeMap != null)
  64. return offsetChangeMap.GetNewOffset(offset, movementType);
  65. else
  66. return CreateSingleChangeMapEntry().GetNewOffset(offset, movementType);
  67. }
  68. /// <summary>
  69. /// Creates a new DocumentChangeEventArgs object.
  70. /// </summary>
  71. public DocumentChangeEventArgs(int offset, string removedText, string insertedText)
  72. : this(offset, removedText, insertedText, null)
  73. {
  74. }
  75. /// <summary>
  76. /// Creates a new DocumentChangeEventArgs object.
  77. /// </summary>
  78. public DocumentChangeEventArgs(int offset, string removedText, string insertedText, OffsetChangeMap offsetChangeMap)
  79. : base(offset, removedText, insertedText)
  80. {
  81. SetOffsetChangeMap(offsetChangeMap);
  82. }
  83. /// <summary>
  84. /// Creates a new DocumentChangeEventArgs object.
  85. /// </summary>
  86. public DocumentChangeEventArgs(int offset, ITextSource removedText, ITextSource insertedText, OffsetChangeMap offsetChangeMap)
  87. : base(offset, removedText, insertedText)
  88. {
  89. SetOffsetChangeMap(offsetChangeMap);
  90. }
  91. void SetOffsetChangeMap(OffsetChangeMap offsetChangeMap)
  92. {
  93. if (offsetChangeMap != null) {
  94. if (!offsetChangeMap.IsFrozen)
  95. throw new ArgumentException("The OffsetChangeMap must be frozen before it can be used in DocumentChangeEventArgs");
  96. if (!offsetChangeMap.IsValidForDocumentChange(this.Offset, this.RemovalLength, this.InsertionLength))
  97. throw new ArgumentException("OffsetChangeMap is not valid for this document change", "offsetChangeMap");
  98. this.offsetChangeMap = offsetChangeMap;
  99. }
  100. }
  101. /// <inheritdoc/>
  102. public override TextChangeEventArgs Invert()
  103. {
  104. OffsetChangeMap map = this.OffsetChangeMapOrNull;
  105. if (map != null) {
  106. map = map.Invert();
  107. map.Freeze();
  108. }
  109. return new DocumentChangeEventArgs(this.Offset, this.InsertedText, this.RemovedText, map);
  110. }
  111. }
  112. }