//
//
//
//
// $Revision$
//
using System;
namespace ICSharpCode.AvalonEdit.Document
{
///
/// Describes a change of the document text.
///
[Serializable]
public class DocumentChangeEventArgs : EventArgs
{
///
/// The offset at which the change occurs.
///
public int Offset { get; private set; }
///
/// The number of characters removed.
///
public int RemovalLength { get; private set; }
///
/// The text that was inserted.
///
public string InsertedText { get; private set; }
///
/// The number of characters inserted.
///
public int InsertionLength {
get { return InsertedText.Length; }
}
volatile OffsetChangeMap offsetChangeMap;
///
/// Gets the OffsetChangeMap associated with this document change.
///
public OffsetChangeMap OffsetChangeMap {
get {
OffsetChangeMap map = offsetChangeMap;
if (map == null) {
// create OffsetChangeMap on demand
map = new OffsetChangeMap();
if (this.RemovalLength > 0)
map.Add(new OffsetChangeMapEntry(this.Offset, -this.RemovalLength));
if (this.InsertionLength > 0)
map.Add(new OffsetChangeMapEntry(this.Offset, this.InsertionLength));
offsetChangeMap = map;
}
return map;
}
}
///
/// Gets the OffsetChangeMap, or null if the default offset map (=removal followed by insertion) is being used.
///
internal OffsetChangeMap OffsetChangeMapOrNull {
get {
return offsetChangeMap;
}
}
///
/// Gets the new offset where the specified offset moves after this document change.
///
public int GetNewOffset(int offset, AnchorMovementType movementType)
{
if (offsetChangeMap != null)
return offsetChangeMap.GetNewOffset(offset, movementType);
if (offset >= this.Offset) {
if (offset <= this.Offset + this.RemovalLength) {
offset = this.Offset;
if (movementType == AnchorMovementType.AfterInsertion)
offset += this.InsertionLength;
} else {
offset += this.InsertionLength - this.RemovalLength;
}
}
return offset;
}
///
/// Creates a new DocumentChangeEventArgs object.
///
public DocumentChangeEventArgs(int offset, int removalLength, string insertedText)
: this(offset, removalLength, insertedText, null)
{
}
///
/// Creates a new DocumentChangeEventArgs object.
///
public DocumentChangeEventArgs(int offset, int removalLength, string insertedText, OffsetChangeMap offsetChangeMap)
{
if (insertedText == null)
throw new ArgumentNullException("insertedText");
this.Offset = offset;
this.RemovalLength = removalLength;
this.InsertedText = insertedText;
if (offsetChangeMap != null) {
if (!offsetChangeMap.IsValidForDocumentChange(offset, removalLength, insertedText.Length))
throw new ArgumentException("OffsetChangeMap is not valid for this document change", "offsetChangeMap");
this.offsetChangeMap = offsetChangeMap;
}
}
}
}