예제 #1
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));
        }
예제 #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;
		}
예제 #3
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);
        }
예제 #4
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");
			}
		}
예제 #5
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);
		}