Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.ExtLibs/HeuristicLab.AvalonEdit/5.0.1/AvalonEdit-5.0.1/Document/ITextSource.cs

Last change on this file was 11700, checked in by jkarder, 10 years ago

#2077: created branch and added first version

File size: 12.8 KB
Line 
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
19using System.Collections.Generic;
20using System;
21using System.IO;
22
23namespace ICSharpCode.AvalonEdit.Document
24{
25  #if !NREFACTORY
26  /// <summary>
27  /// A read-only view on a (potentially mutable) text source.
28  /// The IDocument interface derives from this interface.
29  /// </summary>
30  public interface ITextSource
31  {
32    /// <summary>
33    /// Gets a version identifier for this text source.
34    /// Returns null for unversioned text sources.
35    /// </summary>
36    ITextSourceVersion Version { get; }
37   
38    /// <summary>
39    /// Creates an immutable snapshot of this text source.
40    /// Unlike all other methods in this interface, this method is thread-safe.
41    /// </summary>
42    ITextSource CreateSnapshot();
43   
44    /// <summary>
45    /// Creates an immutable snapshot of a part of this text source.
46    /// Unlike all other methods in this interface, this method is thread-safe.
47    /// </summary>
48    ITextSource CreateSnapshot(int offset, int length);
49   
50    /// <summary>
51    /// Creates a new TextReader to read from this text source.
52    /// </summary>
53    TextReader CreateReader();
54   
55    /// <summary>
56    /// Creates a new TextReader to read from this text source.
57    /// </summary>
58    TextReader CreateReader(int offset, int length);
59   
60    /// <summary>
61    /// Gets the total text length.
62    /// </summary>
63    /// <returns>The length of the text, in characters.</returns>
64    /// <remarks>This is the same as Text.Length, but is more efficient because
65    ///  it doesn't require creating a String object.</remarks>
66    int TextLength { get; }
67   
68    /// <summary>
69    /// Gets the whole text as string.
70    /// </summary>
71    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1721:PropertyNamesShouldNotMatchGetMethods")]
72    string Text { get; }
73   
74    /// <summary>
75    /// Gets a character at the specified position in the document.
76    /// </summary>
77    /// <paramref name="offset">The index of the character to get.</paramref>
78    /// <exception cref="ArgumentOutOfRangeException">Offset is outside the valid range (0 to TextLength-1).</exception>
79    /// <returns>The character at the specified position.</returns>
80    /// <remarks>This is the same as Text[offset], but is more efficient because
81    ///  it doesn't require creating a String object.</remarks>
82    char GetCharAt(int offset);
83   
84    /// <summary>
85    /// Retrieves the text for a portion of the document.
86    /// </summary>
87    /// <exception cref="ArgumentOutOfRangeException">offset or length is outside the valid range.</exception>
88    /// <remarks>This is the same as Text.Substring, but is more efficient because
89    ///  it doesn't require creating a String object for the whole document.</remarks>
90    string GetText(int offset, int length);
91   
92    /// <summary>
93    /// Retrieves the text for a portion of the document.
94    /// </summary>
95    /// <exception cref="ArgumentOutOfRangeException">offset or length is outside the valid range.</exception>
96    string GetText(ISegment segment);
97   
98    /// <summary>
99    /// Writes the text from this document into the TextWriter.
100    /// </summary>
101    void WriteTextTo(TextWriter writer);
102   
103    /// <summary>
104    /// Writes the text from this document into the TextWriter.
105    /// </summary>
106    void WriteTextTo(TextWriter writer, int offset, int length);
107   
108    /// <summary>
109    /// Gets the index of the first occurrence of the character in the specified array.
110    /// </summary>
111    /// <param name="c">Character to search for</param>
112    /// <param name="startIndex">Start index of the area to search.</param>
113    /// <param name="count">Length of the area to search.</param>
114    /// <returns>The first index where the character was found; or -1 if no occurrence was found.</returns>
115    int IndexOf(char c, int startIndex, int count);
116   
117    /// <summary>
118    /// Gets the index of the first occurrence of any character in the specified array.
119    /// </summary>
120    /// <param name="anyOf">Characters to search for</param>
121    /// <param name="startIndex">Start index of the area to search.</param>
122    /// <param name="count">Length of the area to search.</param>
123    /// <returns>The first index where any character was found; or -1 if no occurrence was found.</returns>
124    int IndexOfAny(char[] anyOf, int startIndex, int count);
125   
126    /// <summary>
127    /// Gets the index of the first occurrence of the specified search text in this text source.
128    /// </summary>
129    /// <param name="searchText">The search text</param>
130    /// <param name="startIndex">Start index of the area to search.</param>
131    /// <param name="count">Length of the area to search.</param>
132    /// <param name="comparisonType">String comparison to use.</param>
133    /// <returns>The first index where the search term was found; or -1 if no occurrence was found.</returns>
134    int IndexOf(string searchText, int startIndex, int count, StringComparison comparisonType);
135   
136    /// <summary>
137    /// Gets the index of the last occurrence of the specified character in this text source.
138    /// </summary>
139    /// <param name="c">The search character</param>
140    /// <param name="startIndex">Start index of the area to search.</param>
141    /// <param name="count">Length of the area to search.</param>
142    /// <returns>The last index where the search term was found; or -1 if no occurrence was found.</returns>
143    /// <remarks>The search proceeds backwards from (startIndex+count) to startIndex.
144    /// This is different than the meaning of the parameters on string.LastIndexOf!</remarks>
145    int LastIndexOf(char c, int startIndex, int count);
146   
147    /// <summary>
148    /// Gets the index of the last occurrence of the specified search text in this text source.
149    /// </summary>
150    /// <param name="searchText">The search text</param>
151    /// <param name="startIndex">Start index of the area to search.</param>
152    /// <param name="count">Length of the area to search.</param>
153    /// <param name="comparisonType">String comparison to use.</param>
154    /// <returns>The last index where the search term was found; or -1 if no occurrence was found.</returns>
155    /// <remarks>The search proceeds backwards from (startIndex+count) to startIndex.
156    /// This is different than the meaning of the parameters on string.LastIndexOf!</remarks>
157    int LastIndexOf(string searchText, int startIndex, int count, StringComparison comparisonType);
158   
159    /* What about:
160    void Insert (int offset, string value);
161    void Remove (int offset, int count);
162    void Remove (ISegment segment);
163   
164    void Replace (int offset, int count, string value);
165   
166    Or more search operations:
167   
168    IEnumerable<int> SearchForward (string pattern, int startIndex);
169    IEnumerable<int> SearchForwardIgnoreCase (string pattern, int startIndex);
170   
171    IEnumerable<int> SearchBackward (string pattern, int startIndex);
172    IEnumerable<int> SearchBackwardIgnoreCase (string pattern, int startIndex);
173    */
174  }
175 
176  /// <summary>
177  /// Represents a version identifier for a text source.
178  /// </summary>
179  /// <remarks>
180  /// Verions can be used to efficiently detect whether a document has changed and needs reparsing;
181  /// or even to implement incremental parsers.
182  /// It is a separate class from ITextSource to allow the GC to collect the text source while
183  /// the version checkpoint is still in use.
184  /// </remarks>
185  public interface ITextSourceVersion
186  {
187    /// <summary>
188    /// Gets whether this checkpoint belongs to the same document as the other checkpoint.
189    /// </summary>
190    /// <remarks>
191    /// Returns false when given <c>null</c>.
192    /// </remarks>
193    bool BelongsToSameDocumentAs(ITextSourceVersion other);
194   
195    /// <summary>
196    /// Compares the age of this checkpoint to the other checkpoint.
197    /// </summary>
198    /// <remarks>This method is thread-safe.</remarks>
199    /// <exception cref="ArgumentException">Raised if 'other' belongs to a different document than this version.</exception>
200    /// <returns>-1 if this version is older than <paramref name="other"/>.
201    /// 0 if <c>this</c> version instance represents the same version as <paramref name="other"/>.
202    /// 1 if this version is newer than <paramref name="other"/>.</returns>
203    int CompareAge(ITextSourceVersion other);
204   
205    /// <summary>
206    /// Gets the changes from this checkpoint to the other checkpoint.
207    /// If 'other' is older than this checkpoint, reverse changes are calculated.
208    /// </summary>
209    /// <remarks>This method is thread-safe.</remarks>
210    /// <exception cref="ArgumentException">Raised if 'other' belongs to a different document than this checkpoint.</exception>
211    IEnumerable<TextChangeEventArgs> GetChangesTo(ITextSourceVersion other);
212   
213    /// <summary>
214    /// Calculates where the offset has moved in the other buffer version.
215    /// </summary>
216    /// <exception cref="ArgumentException">Raised if 'other' belongs to a different document than this checkpoint.</exception>
217    int MoveOffsetTo(ITextSourceVersion other, int oldOffset, AnchorMovementType movement = AnchorMovementType.Default);
218  }
219 
220  /// <summary>
221  /// Implements the ITextSource interface using a string.
222  /// </summary>
223  [Serializable]
224  public class StringTextSource : ITextSource
225  {
226    /// <summary>
227    /// Gets a text source containing the empty string.
228    /// </summary>
229    public static readonly StringTextSource Empty = new StringTextSource(string.Empty);
230   
231    readonly string text;
232    readonly ITextSourceVersion version;
233   
234    /// <summary>
235    /// Creates a new StringTextSource with the given text.
236    /// </summary>
237    public StringTextSource(string text)
238    {
239      if (text == null)
240        throw new ArgumentNullException("text");
241      this.text = text;
242    }
243   
244    /// <summary>
245    /// Creates a new StringTextSource with the given text.
246    /// </summary>
247    public StringTextSource(string text, ITextSourceVersion version)
248    {
249      if (text == null)
250        throw new ArgumentNullException("text");
251      this.text = text;
252      this.version = version;
253    }
254   
255    /// <inheritdoc/>
256    public ITextSourceVersion Version {
257      get { return version; }
258    }
259   
260    /// <inheritdoc/>
261    public int TextLength {
262      get { return text.Length; }
263    }
264   
265    /// <inheritdoc/>
266    public string Text {
267      get { return text; }
268    }
269   
270    /// <inheritdoc/>
271    public ITextSource CreateSnapshot()
272    {
273      return this; // StringTextSource is immutable
274    }
275   
276    /// <inheritdoc/>
277    public ITextSource CreateSnapshot(int offset, int length)
278    {
279      return new StringTextSource(text.Substring(offset, length));
280    }
281   
282    /// <inheritdoc/>
283    public TextReader CreateReader()
284    {
285      return new StringReader(text);
286    }
287   
288    /// <inheritdoc/>
289    public TextReader CreateReader(int offset, int length)
290    {
291      return new StringReader(text.Substring(offset, length));
292    }
293   
294    /// <inheritdoc/>
295    public void WriteTextTo(TextWriter writer)
296    {
297      writer.Write(text);
298    }
299   
300    /// <inheritdoc/>
301    public void WriteTextTo(TextWriter writer, int offset, int length)
302    {
303      writer.Write(text.Substring(offset, length));
304    }
305   
306    /// <inheritdoc/>
307    public char GetCharAt(int offset)
308    {
309      return text[offset];
310    }
311   
312    /// <inheritdoc/>
313    public string GetText(int offset, int length)
314    {
315      return text.Substring(offset, length);
316    }
317   
318    /// <inheritdoc/>
319    public string GetText(ISegment segment)
320    {
321      if (segment == null)
322        throw new ArgumentNullException("segment");
323      return text.Substring(segment.Offset, segment.Length);
324    }
325   
326    /// <inheritdoc/>
327    public int IndexOf(char c, int startIndex, int count)
328    {
329      return text.IndexOf(c, startIndex, count);
330    }
331   
332    /// <inheritdoc/>
333    public int IndexOfAny(char[] anyOf, int startIndex, int count)
334    {
335      return text.IndexOfAny(anyOf, startIndex, count);
336    }
337   
338    /// <inheritdoc/>
339    public int IndexOf(string searchText, int startIndex, int count, StringComparison comparisonType)
340    {
341      return text.IndexOf(searchText, startIndex, count, comparisonType);
342    }
343   
344    /// <inheritdoc/>
345    public int LastIndexOf(char c, int startIndex, int count)
346    {
347      return text.LastIndexOf(c, startIndex + count - 1, count);
348    }
349   
350    /// <inheritdoc/>
351    public int LastIndexOf(string searchText, int startIndex, int count, StringComparison comparisonType)
352    {
353      return text.LastIndexOf(searchText, startIndex + count - 1, count, comparisonType);
354    }
355  }
356  #endif
357}
Note: See TracBrowser for help on using the repository browser.