Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.ExtLibs/HeuristicLab.AvalonEdit/5.0.1/AvalonEdit-5.0.1/Document/DocumentLine.cs @ 18242

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

#2077: created branch and added first version

File size: 7.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;
20using System.Diagnostics;
21using System.Globalization;
22#if NREFACTORY
23using ICSharpCode.NRefactory.Editor;
24#endif
25
26namespace ICSharpCode.AvalonEdit.Document
27{
28  /// <summary>
29  /// Represents a line inside a <see cref="TextDocument"/>.
30  /// </summary>
31  /// <remarks>
32  /// <para>
33  /// The <see cref="TextDocument.Lines"/> collection contains one DocumentLine instance
34  /// for every line in the document. This collection is read-only to user code and is automatically
35  /// updated to reflect the current document content.
36  /// </para>
37  /// <para>
38  /// Internally, the DocumentLine instances are arranged in a binary tree that allows for both efficient updates and lookup.
39  /// Converting between offset and line number is possible in O(lg N) time,
40  /// and the data structure also updates all offsets in O(lg N) whenever a line is inserted or removed.
41  /// </para>
42  /// </remarks>
43  public sealed partial class DocumentLine : IDocumentLine
44  {
45    #region Constructor
46    #if DEBUG
47    // Required for thread safety check which is done only in debug builds.
48    // To save space, we don't store the document reference in release builds as we don't need it there.
49    readonly TextDocument document;
50    #endif
51   
52    internal bool isDeleted;
53   
54    internal DocumentLine(TextDocument document)
55    {
56      #if DEBUG
57      Debug.Assert(document != null);
58      this.document = document;
59      #endif
60    }
61   
62    [Conditional("DEBUG")]
63    void DebugVerifyAccess()
64    {
65      #if DEBUG
66      document.DebugVerifyAccess();
67      #endif
68    }
69    #endregion
70   
71    #region Events
72//    /// <summary>
73//    /// Is raised when the line is deleted.
74//    /// </summary>
75//    public event EventHandler Deleted;
76//
77//    /// <summary>
78//    /// Is raised when the line's text changes.
79//    /// </summary>
80//    public event EventHandler TextChanged;
81//
82//    /// <summary>
83//    /// Raises the Deleted or TextChanged event.
84//    /// </summary>
85//    internal void RaiseChanged()
86//    {
87//      if (IsDeleted) {
88//        if (Deleted != null)
89//          Deleted(this, EventArgs.Empty);
90//      } else {
91//        if (TextChanged != null)
92//          TextChanged(this, EventArgs.Empty);
93//      }
94//    }
95    #endregion
96   
97    #region Properties stored in tree
98    /// <summary>
99    /// Gets if this line was deleted from the document.
100    /// </summary>
101    public bool IsDeleted {
102      get {
103        DebugVerifyAccess();
104        return isDeleted;
105      }
106    }
107   
108    /// <summary>
109    /// Gets the number of this line.
110    /// Runtime: O(log n)
111    /// </summary>
112    /// <exception cref="InvalidOperationException">The line was deleted.</exception>
113    public int LineNumber {
114      get {
115        if (IsDeleted)
116          throw new InvalidOperationException();
117        return DocumentLineTree.GetIndexFromNode(this) + 1;
118      }
119    }
120   
121    /// <summary>
122    /// Gets the starting offset of the line in the document's text.
123    /// Runtime: O(log n)
124    /// </summary>
125    /// <exception cref="InvalidOperationException">The line was deleted.</exception>
126    public int Offset {
127      get {
128        if (IsDeleted)
129          throw new InvalidOperationException();
130        return DocumentLineTree.GetOffsetFromNode(this);
131      }
132    }
133   
134    /// <summary>
135    /// Gets the end offset of the line in the document's text (the offset before the line delimiter).
136    /// Runtime: O(log n)
137    /// </summary>
138    /// <exception cref="InvalidOperationException">The line was deleted.</exception>
139    /// <remarks>EndOffset = <see cref="Offset"/> + <see cref="Length"/>.</remarks>
140    public int EndOffset {
141      get { return this.Offset + this.Length; }
142    }
143    #endregion
144   
145    #region Length
146    int totalLength;
147    byte delimiterLength;
148   
149    /// <summary>
150    /// Gets the length of this line. The length does not include the line delimiter. O(1)
151    /// </summary>
152    /// <remarks>This property is still available even if the line was deleted;
153    /// in that case, it contains the line's length before the deletion.</remarks>
154    public int Length {
155      get {
156        DebugVerifyAccess();
157        return totalLength - delimiterLength;
158      }
159    }
160   
161    /// <summary>
162    /// Gets the length of this line, including the line delimiter. O(1)
163    /// </summary>
164    /// <remarks>This property is still available even if the line was deleted;
165    /// in that case, it contains the line's length before the deletion.</remarks>
166    public int TotalLength {
167      get {
168        DebugVerifyAccess();
169        return totalLength;
170      }
171      internal set {
172        // this is set by DocumentLineTree
173        totalLength = value;
174      }
175    }
176   
177    /// <summary>
178    /// <para>Gets the length of the line delimiter.</para>
179    /// <para>The value is 1 for single <c>"\r"</c> or <c>"\n"</c>, 2 for the <c>"\r\n"</c> sequence;
180    /// and 0 for the last line in the document.</para>
181    /// </summary>
182    /// <remarks>This property is still available even if the line was deleted;
183    /// in that case, it contains the line delimiter's length before the deletion.</remarks>
184    public int DelimiterLength {
185      get {
186        DebugVerifyAccess();
187        return delimiterLength;
188      }
189      internal set {
190        Debug.Assert(value >= 0 && value <= 2);
191        delimiterLength = (byte)value;
192      }
193    }
194    #endregion
195   
196    #region Previous / Next Line
197    /// <summary>
198    /// Gets the next line in the document.
199    /// </summary>
200    /// <returns>The line following this line, or null if this is the last line.</returns>
201    public DocumentLine NextLine {
202      get {
203        DebugVerifyAccess();
204       
205        if (right != null) {
206          return right.LeftMost;
207        } else {
208          DocumentLine node = this;
209          DocumentLine oldNode;
210          do {
211            oldNode = node;
212            node = node.parent;
213            // we are on the way up from the right part, don't output node again
214          } while (node != null && node.right == oldNode);
215          return node;
216        }
217      }
218    }
219   
220    /// <summary>
221    /// Gets the previous line in the document.
222    /// </summary>
223    /// <returns>The line before this line, or null if this is the first line.</returns>
224    public DocumentLine PreviousLine {
225      get {
226        DebugVerifyAccess();
227       
228        if (left != null) {
229          return left.RightMost;
230        } else {
231          DocumentLine node = this;
232          DocumentLine oldNode;
233          do {
234            oldNode = node;
235            node = node.parent;
236            // we are on the way up from the left part, don't output node again
237          } while (node != null && node.left == oldNode);
238          return node;
239        }
240      }
241    }
242   
243    IDocumentLine IDocumentLine.NextLine {
244      get { return this.NextLine; }
245    }
246   
247    IDocumentLine IDocumentLine.PreviousLine {
248      get { return this.PreviousLine; }
249    }
250    #endregion
251   
252    #region ToString
253    /// <summary>
254    /// Gets a string with debug output showing the line number and offset.
255    /// Does not include the line's text.
256    /// </summary>
257    public override string ToString()
258    {
259      if (IsDeleted)
260        return "[DocumentLine deleted]";
261      else
262        return string.Format(
263          CultureInfo.InvariantCulture,
264          "[DocumentLine Number={0} Offset={1} Length={2}]", LineNumber, Offset, Length);
265    }
266    #endregion
267  }
268}
Note: See TracBrowser for help on using the repository browser.