Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2389-EpsLexicase/HeuristicLab.ExtLibs/HeuristicLab.AvalonEdit/5.0.1/AvalonEdit-5.0.1/Document/IDocument.cs

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

#2077: created branch and added first version

File size: 11.5 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;
20
21namespace ICSharpCode.AvalonEdit.Document
22{
23  #if !NREFACTORY
24  /// <summary>
25  /// A document representing a source code file for refactoring.
26  /// Line and column counting starts at 1.
27  /// Offset counting starts at 0.
28  /// </summary>
29  public interface IDocument : ITextSource, IServiceProvider
30  {
31    #if NREFACTORY
32    /// <summary>
33    /// Creates an immutable snapshot of this document.
34    /// </summary>
35    IDocument CreateDocumentSnapshot();
36    #endif
37   
38    /// <summary>
39    /// Gets/Sets the text of the whole document..
40    /// </summary>
41    new string Text { get; set; } // hides ITextSource.Text to add the setter
42   
43    /// <summary>
44    /// This event is called directly before a change is applied to the document.
45    /// </summary>
46    /// <remarks>
47    /// It is invalid to modify the document within this event handler.
48    /// Aborting the change (by throwing an exception) is likely to cause corruption of data structures
49    /// that listen to the Changing and Changed events.
50    /// </remarks>
51    event EventHandler<TextChangeEventArgs> TextChanging;
52   
53    /// <summary>
54    /// This event is called directly after a change is applied to the document.
55    /// </summary>
56    /// <remarks>
57    /// It is invalid to modify the document within this event handler.
58    /// Aborting the event handler (by throwing an exception) is likely to cause corruption of data structures
59    /// that listen to the Changing and Changed events.
60    /// </remarks>
61    event EventHandler<TextChangeEventArgs> TextChanged;
62   
63    /// <summary>
64    /// This event is called after a group of changes is completed.
65    /// </summary>
66    /// <seealso cref="EndUndoableAction"/>
67    event EventHandler ChangeCompleted;
68   
69    /// <summary>
70    /// Gets the number of lines in the document.
71    /// </summary>
72    int LineCount { get; }
73   
74    /// <summary>
75    /// Gets the document line with the specified number.
76    /// </summary>
77    /// <param name="lineNumber">The number of the line to retrieve. The first line has number 1.</param>
78    IDocumentLine GetLineByNumber(int lineNumber);
79   
80    /// <summary>
81    /// Gets the document line that contains the specified offset.
82    /// </summary>
83    IDocumentLine GetLineByOffset(int offset);
84   
85    /// <summary>
86    /// Gets the offset from a text location.
87    /// </summary>
88    /// <seealso cref="GetLocation"/>
89    int GetOffset(int line, int column);
90   
91    /// <summary>
92    /// Gets the offset from a text location.
93    /// </summary>
94    /// <seealso cref="GetLocation"/>
95    int GetOffset(TextLocation location);
96   
97    /// <summary>
98    /// Gets the location from an offset.
99    /// </summary>
100    /// <seealso cref="GetOffset(TextLocation)"/>
101    TextLocation GetLocation(int offset);
102   
103    /// <summary>
104    /// Inserts text.
105    /// </summary>
106    /// <param name="offset">The offset at which the text is inserted.</param>
107    /// <param name="text">The new text.</param>
108    /// <remarks>
109    /// Anchors positioned exactly at the insertion offset will move according to their movement type.
110    /// For AnchorMovementType.Default, they will move behind the inserted text.
111    /// The caret will also move behind the inserted text.
112    /// </remarks>
113    void Insert(int offset, string text);
114   
115    /// <summary>
116    /// Inserts text.
117    /// </summary>
118    /// <param name="offset">The offset at which the text is inserted.</param>
119    /// <param name="text">The new text.</param>
120    /// <remarks>
121    /// Anchors positioned exactly at the insertion offset will move according to their movement type.
122    /// For AnchorMovementType.Default, they will move behind the inserted text.
123    /// The caret will also move behind the inserted text.
124    /// </remarks>
125    void Insert(int offset, ITextSource text);
126   
127    /// <summary>
128    /// Inserts text.
129    /// </summary>
130    /// <param name="offset">The offset at which the text is inserted.</param>
131    /// <param name="text">The new text.</param>
132    /// <param name="defaultAnchorMovementType">
133    /// Anchors positioned exactly at the insertion offset will move according to the anchor's movement type.
134    /// For AnchorMovementType.Default, they will move according to the movement type specified by this parameter.
135    /// The caret will also move according to the <paramref name="defaultAnchorMovementType"/> parameter.
136    /// </param>
137    void Insert(int offset, string text, AnchorMovementType defaultAnchorMovementType);
138   
139    /// <summary>
140    /// Inserts text.
141    /// </summary>
142    /// <param name="offset">The offset at which the text is inserted.</param>
143    /// <param name="text">The new text.</param>
144    /// <param name="defaultAnchorMovementType">
145    /// Anchors positioned exactly at the insertion offset will move according to the anchor's movement type.
146    /// For AnchorMovementType.Default, they will move according to the movement type specified by this parameter.
147    /// The caret will also move according to the <paramref name="defaultAnchorMovementType"/> parameter.
148    /// </param>
149    void Insert(int offset, ITextSource text, AnchorMovementType defaultAnchorMovementType);
150   
151    /// <summary>
152    /// Removes text.
153    /// </summary>
154    /// <param name="offset">Starting offset of the text to be removed.</param>
155    /// <param name="length">Length of the text to be removed.</param>
156    void Remove(int offset, int length);
157   
158    /// <summary>
159    /// Replaces text.
160    /// </summary>
161    /// <param name="offset">The starting offset of the text to be replaced.</param>
162    /// <param name="length">The length of the text to be replaced.</param>
163    /// <param name="newText">The new text.</param>
164    void Replace(int offset, int length, string newText);
165   
166    /// <summary>
167    /// Replaces text.
168    /// </summary>
169    /// <param name="offset">The starting offset of the text to be replaced.</param>
170    /// <param name="length">The length of the text to be replaced.</param>
171    /// <param name="newText">The new text.</param>
172    void Replace(int offset, int length, ITextSource newText);
173   
174    /// <summary>
175    /// Make the document combine the following actions into a single
176    /// action for undo purposes.
177    /// </summary>
178    void StartUndoableAction();
179   
180    /// <summary>
181    /// Ends the undoable action started with <see cref="StartUndoableAction"/>.
182    /// </summary>
183    void EndUndoableAction();
184   
185    /// <summary>
186    /// Creates an undo group. Dispose the returned value to close the undo group.
187    /// </summary>
188    /// <returns>An object that closes the undo group when Dispose() is called.</returns>
189    IDisposable OpenUndoGroup();
190   
191    /// <summary>
192    /// Creates a new <see cref="ITextAnchor"/> at the specified offset.
193    /// </summary>
194    /// <inheritdoc cref="ITextAnchor" select="remarks|example"/>
195    ITextAnchor CreateAnchor(int offset);
196   
197    /// <summary>
198    /// Gets the name of the file the document is stored in.
199    /// Could also be a non-existent dummy file name or null if no name has been set.
200    /// </summary>
201    string FileName { get; }
202   
203    /// <summary>
204    /// Fired when the file name of the document changes.
205    /// </summary>
206    event EventHandler FileNameChanged;
207  }
208 
209  /// <summary>
210  /// A line inside a <see cref="IDocument"/>.
211  /// </summary>
212  public interface IDocumentLine : ISegment
213  {
214    /// <summary>
215    /// Gets the length of this line, including the line delimiter.
216    /// </summary>
217    int TotalLength { get; }
218   
219    /// <summary>
220    /// Gets the length of the line terminator.
221    /// Returns 1 or 2; or 0 at the end of the document.
222    /// </summary>
223    int DelimiterLength { get; }
224   
225    /// <summary>
226    /// Gets the number of this line.
227    /// The first line has the number 1.
228    /// </summary>
229    int LineNumber { get; }
230   
231    /// <summary>
232    /// Gets the previous line. Returns null if this is the first line in the document.
233    /// </summary>
234    IDocumentLine PreviousLine { get; }
235   
236    /// <summary>
237    /// Gets the next line. Returns null if this is the last line in the document.
238    /// </summary>
239    IDocumentLine NextLine { get; }
240   
241    /// <summary>
242    /// Gets whether the line was deleted.
243    /// </summary>
244    bool IsDeleted { get; }
245  }
246 
247  /// <summary>
248  /// Describes a change of the document text.
249  /// This class is thread-safe.
250  /// </summary>
251  [Serializable]
252  public class TextChangeEventArgs : EventArgs
253  {
254    readonly int offset;
255    readonly ITextSource removedText;
256    readonly ITextSource insertedText;
257   
258    /// <summary>
259    /// The offset at which the change occurs.
260    /// </summary>
261    public int Offset {
262      get { return offset; }
263    }
264   
265    /// <summary>
266    /// The text that was removed.
267    /// </summary>
268    public ITextSource RemovedText {
269      get { return removedText; }
270    }
271   
272    /// <summary>
273    /// The number of characters removed.
274    /// </summary>
275    public int RemovalLength {
276      get { return removedText.TextLength; }
277    }
278   
279    /// <summary>
280    /// The text that was inserted.
281    /// </summary>
282    public ITextSource InsertedText {
283      get { return insertedText; }
284    }
285   
286    /// <summary>
287    /// The number of characters inserted.
288    /// </summary>
289    public int InsertionLength {
290      get { return insertedText.TextLength; }
291    }
292   
293    /// <summary>
294    /// Creates a new TextChangeEventArgs object.
295    /// </summary>
296    public TextChangeEventArgs(int offset, string removedText, string insertedText)
297    {
298      if (offset < 0)
299        throw new ArgumentOutOfRangeException("offset", offset, "offset must not be negative");
300      this.offset = offset;
301      this.removedText = removedText != null ? new StringTextSource(removedText) : StringTextSource.Empty;
302      this.insertedText = insertedText != null ? new StringTextSource(insertedText) : StringTextSource.Empty;
303    }
304   
305    /// <summary>
306    /// Creates a new TextChangeEventArgs object.
307    /// </summary>
308    public TextChangeEventArgs(int offset, ITextSource removedText, ITextSource insertedText)
309    {
310      if (offset < 0)
311        throw new ArgumentOutOfRangeException("offset", offset, "offset must not be negative");
312      this.offset = offset;
313      this.removedText = removedText ?? StringTextSource.Empty;
314      this.insertedText = insertedText ?? StringTextSource.Empty;
315    }
316   
317    /// <summary>
318    /// Gets the new offset where the specified offset moves after this document change.
319    /// </summary>
320    public virtual int GetNewOffset(int offset, AnchorMovementType movementType = AnchorMovementType.Default)
321    {
322      if (offset >= this.Offset && offset <= this.Offset + this.RemovalLength) {
323        if (movementType == AnchorMovementType.BeforeInsertion)
324          return this.Offset;
325        else
326          return this.Offset + this.InsertionLength;
327      } else if (offset > this.Offset) {
328        return offset + this.InsertionLength - this.RemovalLength;
329      } else {
330        return offset;
331      }
332    }
333   
334    /// <summary>
335    /// Creates TextChangeEventArgs for the reverse change.
336    /// </summary>
337    public virtual TextChangeEventArgs Invert()
338    {
339      return new TextChangeEventArgs(offset, insertedText, removedText);
340    }
341  }
342  #endif
343}
Note: See TracBrowser for help on using the repository browser.