Free cookie consent management tool by TermsFeed Policy Generator

source: branches/WebJobManager/HeuristicLab.ExtLibs/HeuristicLab.AvalonEdit/5.0.1/AvalonEdit-5.0.1/Document/TextAnchor.cs @ 17399

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

#2077: created branch and added first version

File size: 5.2 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 ICSharpCode.AvalonEdit.Utils;
21using ICSharpCode.NRefactory;
22using ICSharpCode.NRefactory.Editor;
23using ICSharpCode.AvalonEdit.Document;
24
25namespace ICSharpCode.AvalonEdit.Document
26{
27  /// <summary>
28  /// The TextAnchor class references an offset (a position between two characters).
29  /// It automatically updates the offset when text is inserted/removed in front of the anchor.
30  /// </summary>
31  /// <remarks>
32  /// <para>Use the <see cref="Offset"/> property to get the offset from a text anchor.
33  /// Use the <see cref="TextDocument.CreateAnchor"/> method to create an anchor from an offset.
34  /// </para>
35  /// <para>
36  /// The document will automatically update all text anchors; and because it uses weak references to do so,
37  /// the garbage collector can simply collect the anchor object when you don't need it anymore.
38  /// </para>
39  /// <para>Moreover, the document is able to efficiently update a large number of anchors without having to look
40  /// at each anchor object individually. Updating the offsets of all anchors usually only takes time logarithmic
41  /// to the number of anchors. Retrieving the <see cref="Offset"/> property also runs in O(lg N).</para>
42  /// <inheritdoc cref="IsDeleted" />
43  /// <inheritdoc cref="MovementType" />
44  /// <para>If you want to track a segment, you can use the <see cref="AnchorSegment"/> class which
45  /// implements <see cref="ISegment"/> using two text anchors.</para>
46  /// </remarks>
47  /// <example>
48  /// Usage:
49  /// <code>TextAnchor anchor = document.CreateAnchor(offset);
50  /// ChangeMyDocument();
51  /// int newOffset = anchor.Offset;
52  /// </code>
53  /// </example>
54  public sealed class TextAnchor : ITextAnchor
55  {
56    readonly TextDocument document;
57    internal TextAnchorNode node;
58   
59    internal TextAnchor(TextDocument document)
60    {
61      this.document = document;
62    }
63   
64    /// <summary>
65    /// Gets the document owning the anchor.
66    /// </summary>
67    public TextDocument Document {
68      get { return document; }
69    }
70   
71    /// <inheritdoc/>
72    public AnchorMovementType MovementType { get; set; }
73   
74    /// <inheritdoc/>
75    public bool SurviveDeletion { get; set; }
76   
77    /// <inheritdoc/>
78    public bool IsDeleted {
79      get {
80        document.DebugVerifyAccess();
81        return node == null;
82      }
83    }
84   
85    /// <inheritdoc/>
86    public event EventHandler Deleted;
87   
88    internal void OnDeleted(DelayedEvents delayedEvents)
89    {
90      node = null;
91      delayedEvents.DelayedRaise(Deleted, this, EventArgs.Empty);
92    }
93   
94    /// <summary>
95    /// Gets the offset of the text anchor.
96    /// </summary>
97    /// <exception cref="InvalidOperationException">Thrown when trying to get the Offset from a deleted anchor.</exception>
98    public int Offset {
99      get {
100        document.DebugVerifyAccess();
101       
102        TextAnchorNode n = this.node;
103        if (n == null)
104          throw new InvalidOperationException();
105       
106        int offset = n.length;
107        if (n.left != null)
108          offset += n.left.totalLength;
109        while (n.parent != null) {
110          if (n == n.parent.right) {
111            if (n.parent.left != null)
112              offset += n.parent.left.totalLength;
113            offset += n.parent.length;
114          }
115          n = n.parent;
116        }
117        return offset;
118      }
119    }
120   
121    /// <summary>
122    /// Gets the line number of the anchor.
123    /// </summary>
124    /// <exception cref="InvalidOperationException">Thrown when trying to get the Offset from a deleted anchor.</exception>
125    public int Line {
126      get {
127        return document.GetLineByOffset(this.Offset).LineNumber;
128      }
129    }
130   
131    /// <summary>
132    /// Gets the column number of this anchor.
133    /// </summary>
134    /// <exception cref="InvalidOperationException">Thrown when trying to get the Offset from a deleted anchor.</exception>
135    public int Column {
136      get {
137        int offset = this.Offset;
138        return offset - document.GetLineByOffset(offset).Offset + 1;
139      }
140    }
141   
142    /// <summary>
143    /// Gets the text location of this anchor.
144    /// </summary>
145    /// <exception cref="InvalidOperationException">Thrown when trying to get the Offset from a deleted anchor.</exception>
146    public TextLocation Location {
147      get {
148        return document.GetLocation(this.Offset);
149      }
150    }
151   
152    /// <inheritdoc/>
153    public override string ToString()
154    {
155      return "[TextAnchor Offset=" + Offset + "]";
156    }
157  }
158}
Note: See TracBrowser for help on using the repository browser.