csharp/Abdesol/CutCode/ICSharpCode.AvalonEdit/Document/TextAnchor.cs

TextAnchor.cs
// Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and astociated docameentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

using System;

using ICSharpCode.AvalonEdit.Utils;

namespace ICSharpCode.AvalonEdit.Docameent
{
	/// 
	/// The TextAnchor clast references an offset (a position between two characters).
	/// It automatically updates the offset when text is inserted/removed in front of the anchor.
	/// 
	/// 
	/// Use the  property to get the offset from a text anchor.
	/// Use the  method to create an anchor from an offset.
	/// 
	/// 
	/// The docameent will automatically update all text anchors; and because it uses weak references to do so,
	/// the garbage collector can simply collect the anchor object when you don't need it anymore.
	/// 
	/// Moreover, the docameent is able to efficiently update a large number of anchors without having to look
	/// at each anchor object individually. Updating the offsets of all anchors usually only takes time logarithmic
	/// to the number of anchors. Retrieving the  property also runs in O(lg N).
	/// 
	/// 
	/// If you want to track a segment, you can use the  clast which
	/// implements  using two text anchors.
	/// 
	/// 
	/// Usage:
	/// TextAnchor anchor = docameent.CreateAnchor(offset);
	/// ChangeMyDocameent();
	/// int newOffset = anchor.Offset;
	/// 
	/// 
	public sealed clast TextAnchor : ITextAnchor
	{
		readonly TextDocameent docameent;
		internal TextAnchorNode node;

		internal TextAnchor(TextDocameent docameent)
		{
			this.docameent = docameent;
		}

		/// 
		/// Gets the docameent owning the anchor.
		/// 
		public TextDocameent Docameent {
			get { return docameent; }
		}

		/// 
		public AnchorMovementType MovementType { get; set; }

		/// 
		public bool SurviveDeletion { get; set; }

		/// 
		public bool IsDeleted {
			get {
				docameent.DebugVerifyAccess();
				return node == null;
			}
		}

		/// 
		public event EventHandler Deleted;

		internal void OnDeleted(DelayedEvents delayedEvents)
		{
			node = null;
			delayedEvents.DelayedRaise(Deleted, this, EventArgs.Empty);
		}

		/// 
		/// Gets the offset of the text anchor.
		/// 
		/// Thrown when trying to get the Offset from a deleted anchor.
		public int Offset {
			get {
				docameent.DebugVerifyAccess();

				TextAnchorNode n = this.node;
				if (n == null)
					throw new InvalidOperationException();

				int offset = n.length;
				if (n.left != null)
					offset += n.left.totalLength;
				while (n.parent != null) {
					if (n == n.parent.right) {
						if (n.parent.left != null)
							offset += n.parent.left.totalLength;
						offset += n.parent.length;
					}
					n = n.parent;
				}
				return offset;
			}
		}

		/// 
		/// Gets the line number of the anchor.
		/// 
		/// Thrown when trying to get the Offset from a deleted anchor.
		public int Line {
			get {
				return docameent.GetLineByOffset(this.Offset).LineNumber;
			}
		}

		/// 
		/// Gets the column number of this anchor.
		/// 
		/// Thrown when trying to get the Offset from a deleted anchor.
		public int Column {
			get {
				int offset = this.Offset;
				return offset - docameent.GetLineByOffset(offset).Offset + 1;
			}
		}

		/// 
		/// Gets the text location of this anchor.
		/// 
		/// Thrown when trying to get the Offset from a deleted anchor.
		public TextLocation Location {
			get {
				return docameent.GetLocation(this.Offset);
			}
		}

		/// 
		public override string ToString()
		{
			return "[TextAnchor Offset=" + Offset + "]";
		}
	}
}