/NRefactory/ICSharpCode.NRefactory/Editor/ITextSource.cs

http://github.com/icsharpcode/ILSpy · C# · 218 lines · 34 code · 23 blank · 161 comment · 0 complexity · 0f33d83a838be908d40de1c7898a8d92 MD5 · raw file

  1. // Copyright (c) 2010-2013 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 System.Collections.Generic;
  20. using System.IO;
  21. namespace ICSharpCode.NRefactory.Editor
  22. {
  23. /// <summary>
  24. /// A read-only view on a (potentially mutable) text source.
  25. /// The IDocument interface derives from this interface.
  26. /// </summary>
  27. public interface ITextSource
  28. {
  29. /// <summary>
  30. /// Gets a version identifier for this text source.
  31. /// Returns null for unversioned text sources.
  32. /// </summary>
  33. ITextSourceVersion Version { get; }
  34. /// <summary>
  35. /// Creates an immutable snapshot of this text source.
  36. /// Unlike all other methods in this interface, this method is thread-safe.
  37. /// </summary>
  38. ITextSource CreateSnapshot();
  39. /// <summary>
  40. /// Creates an immutable snapshot of a part of this text source.
  41. /// Unlike all other methods in this interface, this method is thread-safe.
  42. /// </summary>
  43. ITextSource CreateSnapshot(int offset, int length);
  44. /// <summary>
  45. /// Creates a new TextReader to read from this text source.
  46. /// </summary>
  47. TextReader CreateReader();
  48. /// <summary>
  49. /// Creates a new TextReader to read from this text source.
  50. /// </summary>
  51. TextReader CreateReader(int offset, int length);
  52. /// <summary>
  53. /// Gets the total text length.
  54. /// </summary>
  55. /// <returns>The length of the text, in characters.</returns>
  56. /// <remarks>This is the same as Text.Length, but is more efficient because
  57. /// it doesn't require creating a String object.</remarks>
  58. int TextLength { get; }
  59. /// <summary>
  60. /// Gets the whole text as string.
  61. /// </summary>
  62. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1721:PropertyNamesShouldNotMatchGetMethods")]
  63. string Text { get; }
  64. /// <summary>
  65. /// Gets a character at the specified position in the document.
  66. /// </summary>
  67. /// <paramref name="offset">The index of the character to get.</paramref>
  68. /// <exception cref="ArgumentOutOfRangeException">Offset is outside the valid range (0 to TextLength-1).</exception>
  69. /// <returns>The character at the specified position.</returns>
  70. /// <remarks>This is the same as Text[offset], but is more efficient because
  71. /// it doesn't require creating a String object.</remarks>
  72. char GetCharAt(int offset);
  73. /// <summary>
  74. /// Retrieves the text for a portion of the document.
  75. /// </summary>
  76. /// <exception cref="ArgumentOutOfRangeException">offset or length is outside the valid range.</exception>
  77. /// <remarks>This is the same as Text.Substring, but is more efficient because
  78. /// it doesn't require creating a String object for the whole document.</remarks>
  79. string GetText(int offset, int length);
  80. /// <summary>
  81. /// Retrieves the text for a portion of the document.
  82. /// </summary>
  83. /// <exception cref="ArgumentOutOfRangeException">offset or length is outside the valid range.</exception>
  84. string GetText(ISegment segment);
  85. /// <summary>
  86. /// Writes the text from this document into the TextWriter.
  87. /// </summary>
  88. void WriteTextTo(TextWriter writer);
  89. /// <summary>
  90. /// Writes the text from this document into the TextWriter.
  91. /// </summary>
  92. void WriteTextTo(TextWriter writer, int offset, int length);
  93. /// <summary>
  94. /// Gets the index of the first occurrence of the character in the specified array.
  95. /// </summary>
  96. /// <param name="c">Character to search for</param>
  97. /// <param name="startIndex">Start index of the area to search.</param>
  98. /// <param name="count">Length of the area to search.</param>
  99. /// <returns>The first index where the character was found; or -1 if no occurrence was found.</returns>
  100. int IndexOf(char c, int startIndex, int count);
  101. /// <summary>
  102. /// Gets the index of the first occurrence of any character in the specified array.
  103. /// </summary>
  104. /// <param name="anyOf">Characters to search for</param>
  105. /// <param name="startIndex">Start index of the area to search.</param>
  106. /// <param name="count">Length of the area to search.</param>
  107. /// <returns>The first index where any character was found; or -1 if no occurrence was found.</returns>
  108. int IndexOfAny(char[] anyOf, int startIndex, int count);
  109. /// <summary>
  110. /// Gets the index of the first occurrence of the specified search text in this text source.
  111. /// </summary>
  112. /// <param name="searchText">The search text</param>
  113. /// <param name="startIndex">Start index of the area to search.</param>
  114. /// <param name="count">Length of the area to search.</param>
  115. /// <param name="comparisonType">String comparison to use.</param>
  116. /// <returns>The first index where the search term was found; or -1 if no occurrence was found.</returns>
  117. int IndexOf(string searchText, int startIndex, int count, StringComparison comparisonType);
  118. /// <summary>
  119. /// Gets the index of the last occurrence of the specified character in this text source.
  120. /// </summary>
  121. /// <param name="c">The search character</param>
  122. /// <param name="startIndex">Start index of the area to search.</param>
  123. /// <param name="count">Length of the area to search.</param>
  124. /// <returns>The last index where the search term was found; or -1 if no occurrence was found.</returns>
  125. /// <remarks>The search proceeds backwards from (startIndex+count) to startIndex.
  126. /// This is different than the meaning of the parameters on string.LastIndexOf!</remarks>
  127. int LastIndexOf(char c, int startIndex, int count);
  128. /// <summary>
  129. /// Gets the index of the last occurrence of the specified search text in this text source.
  130. /// </summary>
  131. /// <param name="searchText">The search text</param>
  132. /// <param name="startIndex">Start index of the area to search.</param>
  133. /// <param name="count">Length of the area to search.</param>
  134. /// <param name="comparisonType">String comparison to use.</param>
  135. /// <returns>The last index where the search term was found; or -1 if no occurrence was found.</returns>
  136. /// <remarks>The search proceeds backwards from (startIndex+count) to startIndex.
  137. /// This is different than the meaning of the parameters on string.LastIndexOf!</remarks>
  138. int LastIndexOf(string searchText, int startIndex, int count, StringComparison comparisonType);
  139. /* What about:
  140. void Insert (int offset, string value);
  141. void Remove (int offset, int count);
  142. void Remove (ISegment segment);
  143. void Replace (int offset, int count, string value);
  144. Or more search operations:
  145. IEnumerable<int> SearchForward (string pattern, int startIndex);
  146. IEnumerable<int> SearchForwardIgnoreCase (string pattern, int startIndex);
  147. IEnumerable<int> SearchBackward (string pattern, int startIndex);
  148. IEnumerable<int> SearchBackwardIgnoreCase (string pattern, int startIndex);
  149. */
  150. }
  151. /// <summary>
  152. /// Represents a version identifier for a text source.
  153. /// </summary>
  154. /// <remarks>
  155. /// Verions can be used to efficiently detect whether a document has changed and needs reparsing;
  156. /// or even to implement incremental parsers.
  157. /// It is a separate class from ITextSource to allow the GC to collect the text source while
  158. /// the version checkpoint is still in use.
  159. /// </remarks>
  160. public interface ITextSourceVersion
  161. {
  162. /// <summary>
  163. /// Gets whether this checkpoint belongs to the same document as the other checkpoint.
  164. /// </summary>
  165. /// <remarks>
  166. /// Returns false when given <c>null</c>.
  167. /// </remarks>
  168. bool BelongsToSameDocumentAs(ITextSourceVersion other);
  169. /// <summary>
  170. /// Compares the age of this checkpoint to the other checkpoint.
  171. /// </summary>
  172. /// <remarks>This method is thread-safe.</remarks>
  173. /// <exception cref="ArgumentException">Raised if 'other' belongs to a different document than this version.</exception>
  174. /// <returns>-1 if this version is older than <paramref name="other"/>.
  175. /// 0 if <c>this</c> version instance represents the same version as <paramref name="other"/>.
  176. /// 1 if this version is newer than <paramref name="other"/>.</returns>
  177. int CompareAge(ITextSourceVersion other);
  178. /// <summary>
  179. /// Gets the changes from this checkpoint to the other checkpoint.
  180. /// If 'other' is older than this checkpoint, reverse changes are calculated.
  181. /// </summary>
  182. /// <remarks>This method is thread-safe.</remarks>
  183. /// <exception cref="ArgumentException">Raised if 'other' belongs to a different document than this checkpoint.</exception>
  184. IEnumerable<TextChangeEventArgs> GetChangesTo(ITextSourceVersion other);
  185. /// <summary>
  186. /// Calculates where the offset has moved in the other buffer version.
  187. /// </summary>
  188. /// <exception cref="ArgumentException">Raised if 'other' belongs to a different document than this checkpoint.</exception>
  189. int MoveOffsetTo(ITextSourceVersion other, int oldOffset, AnchorMovementType movement = AnchorMovementType.Default);
  190. }
  191. }