void SetOffsetChangeMap(OffsetChangeMap offsetChangeMap)
		{
			if (offsetChangeMap != null) {
				if (!offsetChangeMap.IsFrozen)
					throw new ArgumentException("The OffsetChangeMap must be frozen before it can be used in DocumentChangeEventArgs");
				if (!offsetChangeMap.IsValidForDocumentChange(this.Offset, this.RemovalLength, this.InsertionLength))
					throw new ArgumentException("OffsetChangeMap is not valid for this document change", "offsetChangeMap");
				this.offsetChangeMap = offsetChangeMap;
			}
		}
Beispiel #2
0
		public static OffsetChangeMap ToOffsetChangeMap(this IEnumerable<Edit> edits)
		{
			var map = new OffsetChangeMap();
			int diff = 0;
			foreach (var edit in edits) {
				Debug.Assert(edit.EditType != ChangeType.None && edit.EditType != ChangeType.Unsaved);
				int offset = edit.BeginA + diff;
				int removalLength = edit.EndA - edit.BeginA;
				int insertionLength = edit.EndB - edit.BeginB;
				
				diff += (insertionLength - removalLength);
				map.Add(new OffsetChangeMapEntry(offset, removalLength, insertionLength));
			}
			return map;
		}
        /// <summary>
        /// Creates a new DocumentChangeEventArgs object.
        /// </summary>
        public DocumentChangeEventArgs(int offset, string removedText, string insertedText, OffsetChangeMap offsetChangeMap)
        {
            ThrowUtil.CheckNotNegative(offset, "offset");
            ThrowUtil.CheckNotNull(removedText, "removedText");
            ThrowUtil.CheckNotNull(insertedText, "insertedText");

            this.Offset = offset;
            this.RemovedText = removedText;
            this.InsertedText = insertedText;

            if (offsetChangeMap != null) {
                if (!offsetChangeMap.IsFrozen)
                    throw new ArgumentException("The OffsetChangeMap must be frozen before it can be used in DocumentChangeEventArgs");
                if (!offsetChangeMap.IsValidForDocumentChange(offset, removedText.Length, insertedText.Length))
                    throw new ArgumentException("OffsetChangeMap is not valid for this document change", "offsetChangeMap");
                this.offsetChangeMap = offsetChangeMap;
            }
        }
Beispiel #4
0
		/// <summary>
		/// Updates the start and end offsets of all segments stored in this collection.
		/// </summary>
		/// <param name="change">OffsetChangeMap instance describing the change to the document.</param>
		public void UpdateOffsets(OffsetChangeMap change)
		{
			if (change == null)
				throw new ArgumentNullException("change");
			UpdateOffsets(change.GetNewOffset);
		}
Beispiel #5
0
		void DoReplace(int offset, int length, string newText, OffsetChangeMap offsetChangeMap)
		{
			if (length == 0 && newText.Length == 0)
				return;
			
			// trying to replace a single character in 'Normal' mode?
			// for single characters, 'CharacterReplace' mode is equivalent, but more performant
			// (we don't have to touch the anchorTree at all in 'CharacterReplace' mode)
			if (length == 1 && newText.Length == 1 && offsetChangeMap == null)
				offsetChangeMap = OffsetChangeMap.Empty;
			
			string removedText = rope.ToString(offset, length);
			DocumentChangeEventArgs args = new DocumentChangeEventArgs(offset, removedText, newText, offsetChangeMap);
			
			// fire DocumentChanging event
			if (Changing != null)
				Changing(this, args);
			
			cachedText = null; // reset cache of complete document text
			fireTextChanged = true;
			DelayedEvents delayedEvents = new DelayedEvents();
			
			lock (lockObject) {
				// create linked list of checkpoints, if required
				if (currentCheckpoint != null) {
					currentCheckpoint = currentCheckpoint.Append(args);
				}
				
				// now update the textBuffer and lineTree
				if (offset == 0 && length == rope.Length) {
					// optimize replacing the whole document
					rope.Clear();
					rope.InsertText(0, newText);
					lineManager.Rebuild();
				} else {
					rope.RemoveRange(offset, length);
					lineManager.Remove(offset, length);
					#if DEBUG
					lineTree.CheckProperties();
					#endif
					rope.InsertText(offset, newText);
					lineManager.Insert(offset, newText);
					#if DEBUG
					lineTree.CheckProperties();
					#endif
				}
			}
			
			// update text anchors
			if (offsetChangeMap == null) {
				anchorTree.HandleTextChange(args.CreateSingleChangeMapEntry(), delayedEvents);
			} else {
				foreach (OffsetChangeMapEntry entry in offsetChangeMap) {
					anchorTree.HandleTextChange(entry, delayedEvents);
				}
			}
			
			// raise delayed events after our data structures are consistent again
			delayedEvents.RaiseEvents();
			
			// fire DocumentChanged event
			if (Changed != null)
				Changed(this, args);
		}
Beispiel #6
0
		/// <summary>
		/// Replaces text.
		/// </summary>
		/// <param name="offset">The starting offset of the text to be replaced.</param>
		/// <param name="length">The length of the text to be replaced.</param>
		/// <param name="text">The new text.</param>
		/// <param name="offsetChangeMap">The offsetChangeMap determines how offsets inside the old text are mapped to the new text.
		/// This affects how the anchors and segments inside the replaced region behave.
		/// If you pass null (the default when using one of the other overloads), the offsets are changed as
		/// in OffsetChangeMappingType.Normal mode.
		/// If you pass OffsetChangeMap.Empty, then everything will stay in its old place (OffsetChangeMappingType.CharacterReplace mode).
		/// The offsetChangeMap must be a valid 'explanation' for the document change. See <see cref="OffsetChangeMap.IsValidForDocumentChange"/>.
		/// Passing an OffsetChangeMap to the Replace method will automatically freeze it to ensure the thread safety of the resulting
		/// DocumentChangeEventArgs instance.
		/// </param>
		public void Replace(int offset, int length, string text, OffsetChangeMap offsetChangeMap)
		{
			if (text == null)
				throw new ArgumentNullException("text");
			
			if (offsetChangeMap != null)
				offsetChangeMap.Freeze();
			
			// Ensure that all changes take place inside an update group.
			// Will also take care of throwing an exception if inDocumentChanging is set.
			BeginUpdate();
			try {
				// protect document change against corruption by other changes inside the event handlers
				inDocumentChanging = true;
				try {
					// The range verification must wait until after the BeginUpdate() call because the document
					// might be modified inside the UpdateStarted event.
					ThrowIfRangeInvalid(offset, length);
					
					DoReplace(offset, length, text, offsetChangeMap);
				} finally {
					inDocumentChanging = false;
				}
			} finally {
				EndUpdate();
			}
		}
Beispiel #7
0
		/// <summary>
		/// Replaces text.
		/// </summary>
		/// <param name="offset">The starting offset of the text to be replaced.</param>
		/// <param name="length">The length of the text to be replaced.</param>
		/// <param name="text">The new text.</param>
		/// <param name="offsetChangeMappingType">The offsetChangeMappingType determines how offsets inside the old text are mapped to the new text.
		/// This affects how the anchors and segments inside the replaced region behave.</param>
		public void Replace(int offset, int length, string text, OffsetChangeMappingType offsetChangeMappingType)
		{
			if (text == null)
				throw new ArgumentNullException("text");
			// Please see OffsetChangeMappingType XML comments for details on how these modes work.
			switch (offsetChangeMappingType) {
				case OffsetChangeMappingType.Normal:
					Replace(offset, length, text, null);
					break;
				case OffsetChangeMappingType.KeepAnchorBeforeInsertion:
					Replace(offset, length, text, OffsetChangeMap.FromSingleElement(
						new OffsetChangeMapEntry(offset, length, text.Length, false, true)));
					break;
				case OffsetChangeMappingType.RemoveAndInsert:
					if (length == 0 || text.Length == 0) {
						// only insertion or only removal?
						// OffsetChangeMappingType doesn't matter, just use Normal.
						Replace(offset, length, text, null);
					} else {
						OffsetChangeMap map = new OffsetChangeMap(2);
						map.Add(new OffsetChangeMapEntry(offset, length, 0));
						map.Add(new OffsetChangeMapEntry(offset, 0, text.Length));
						map.Freeze();
						Replace(offset, length, text, map);
					}
					break;
				case OffsetChangeMappingType.CharacterReplace:
					if (length == 0 || text.Length == 0) {
						// only insertion or only removal?
						// OffsetChangeMappingType doesn't matter, just use Normal.
						Replace(offset, length, text, null);
					} else if (text.Length > length) {
						// look at OffsetChangeMappingType.CharacterReplace XML comments on why we need to replace
						// the last character
						OffsetChangeMapEntry entry = new OffsetChangeMapEntry(offset + length - 1, 1, 1 + text.Length - length);
						Replace(offset, length, text, OffsetChangeMap.FromSingleElement(entry));
					} else if (text.Length < length) {
						OffsetChangeMapEntry entry = new OffsetChangeMapEntry(offset + text.Length, length - text.Length, 0, true, false);
						Replace(offset, length, text, OffsetChangeMap.FromSingleElement(entry));
					} else {
						Replace(offset, length, text, OffsetChangeMap.Empty);
					}
					break;
				default:
					throw new ArgumentOutOfRangeException("offsetChangeMappingType", offsetChangeMappingType, "Invalid enum value");
			}
		}
		/// <summary>
		/// Creates a new DocumentChangeEventArgs object.
		/// </summary>
		public DocumentChangeEventArgs(int offset, ITextSource removedText, ITextSource insertedText, OffsetChangeMap offsetChangeMap)
			: base(offset, removedText, insertedText)
		{
			SetOffsetChangeMap(offsetChangeMap);
		}
		/// <summary>
		/// Creates a new DocumentChangeEventArgs object.
		/// </summary>
		public DocumentChangeEventArgs(int offset, string removedText, string insertedText, OffsetChangeMap offsetChangeMap)
			: base(offset, removedText, insertedText)
		{
			SetOffsetChangeMap(offsetChangeMap);
		}
Beispiel #10
0
		/// <summary>
		/// Creates a substring of this rich text.
		/// </summary>
		public RichText Substring(int offset, int length)
		{
			if (offset == 0 && length == this.Length)
				return this;
			string newText = text.Substring(offset, length);
			RichTextModel model = ToRichTextModel();
			OffsetChangeMap map = new OffsetChangeMap(2);
			map.Add(new OffsetChangeMapEntry(offset + length, text.Length - offset - length, 0));
			map.Add(new OffsetChangeMapEntry(0, offset, 0));
			model.UpdateOffsets(map);
			return new RichText(newText, model);
		}
        public void OnTextChanged(OffsetChangeMap map)
        {
            if (DoNotFireEvent)
            {
                return;
            }

            if (TextChanged != null)
            {
                List<MyTextChange> textChangeList = new List<MyTextChange>();

                foreach (var change in map)
                {
                    textChangeList.Add(new MyTextChange()
                    {
                        AddedLength = change.InsertionLength,
                        Offset = change.Offset,
                        RemovedLength = change.RemovalLength,
                    });
                }

                TextChanged(this,
                    new ModernizedAlice.IPlugin.ModuleInterface.TextChangedEventArgs() {Source = _view, Changes = textChangeList });
            }
        }
Beispiel #12
0
 /// <summary>
 /// Replaces text.
 /// </summary>
 /// <param name="offset">The starting offset of the text to be replaced.</param>
 /// <param name="length">The length of the text to be replaced.</param>
 /// <param name="text">The new text.</param>
 /// <param name="offsetChangeMap">The offsetChangeMap determines how offsets inside the old text are mapped to the new text.
 /// This affects how the anchors and segments inside the replaced region behave.
 /// If you pass null (the default when using one of the other overloads), the offsets are changed as
 /// in OffsetChangeMappingType.Normal mode.
 /// If you pass OffsetChangeMap.Empty, then everything will stay in its old place (OffsetChangeMappingType.CharacterReplace mode).
 /// The offsetChangeMap must be a valid 'explanation' for the document change. See <see cref="OffsetChangeMap.IsValidForDocumentChange"/>.
 /// Passing an OffsetChangeMap to the Replace method will automatically freeze it to ensure the thread safety of the resulting
 /// DocumentChangeEventArgs instance.
 /// </param>
 public void Replace(int offset, int length, string text, OffsetChangeMap offsetChangeMap)
 {
     Replace(offset, length, new StringTextSource(text), offsetChangeMap);
 }
Beispiel #13
0
        void DoReplace(int offset, int length, ITextSource newText, OffsetChangeMap offsetChangeMap)
        {
            if (length == 0 && newText.TextLength == 0)
                return;

            // trying to replace a single character in 'Normal' mode?
            // for single characters, 'CharacterReplace' mode is equivalent, but more performant
            // (we don't have to touch the anchorTree at all in 'CharacterReplace' mode)
            if (length == 1 && newText.TextLength == 1 && offsetChangeMap == null)
                offsetChangeMap = OffsetChangeMap.Empty;

            ITextSource removedText;
            if (length == 0) {
                removedText = StringTextSource.Empty;
            } else if (length < 100) {
                removedText = new StringTextSource(rope.ToString(offset, length));
            } else {
                // use a rope if the removed string is long
                removedText = new RopeTextSource(rope.GetRange(offset, length));
            }
            DocumentChangeEventArgs args = new DocumentChangeEventArgs(offset, removedText, newText, offsetChangeMap);

            // fire DocumentChanging event
            if (Changing != null)
                Changing(this, args);
            if (textChanging != null)
                textChanging(this, args);

            undoStack.Push(this, args);

            cachedText = null; // reset cache of complete document text
            fireTextChanged = true;
            DelayedEvents delayedEvents = new DelayedEvents();

            lock (lockObject) {
                // create linked list of checkpoints
                versionProvider.AppendChange(args);

                // now update the textBuffer and lineTree
                if (offset == 0 && length == rope.Length) {
                    // optimize replacing the whole document
                    rope.Clear();
                    var newRopeTextSource = newText as RopeTextSource;
                    if (newRopeTextSource != null)
                        rope.InsertRange(0, newRopeTextSource.GetRope());
                    else
                        rope.InsertText(0, newText.Text);
                    lineManager.Rebuild();
                } else {
                    rope.RemoveRange(offset, length);
                    lineManager.Remove(offset, length);
                    #if DEBUG
                    lineTree.CheckProperties();
                    #endif
                    var newRopeTextSource = newText as RopeTextSource;
                    if (newRopeTextSource != null)
                        rope.InsertRange(offset, newRopeTextSource.GetRope());
                    else
                        rope.InsertText(offset, newText.Text);
                    lineManager.Insert(offset, newText);
                    #if DEBUG
                    lineTree.CheckProperties();
                    #endif
                }
            }

            // update text anchors
            if (offsetChangeMap == null) {
                anchorTree.HandleTextChange(args.CreateSingleChangeMapEntry(), delayedEvents);
            } else {
                foreach (OffsetChangeMapEntry entry in offsetChangeMap) {
                    anchorTree.HandleTextChange(entry, delayedEvents);
                }
            }

            lineManager.ChangeComplete(args);

            // raise delayed events after our data structures are consistent again
            delayedEvents.RaiseEvents();

            // fire DocumentChanged event
            OnChanged(args);                                                  // [DIGITALRUNE] Use protected virtual method to raise Changed event.
            if (textChanged != null)
                textChanged(this, args);
        }