/// <summary> Inserts the given content into this instance. </summary> /// <param name="caret"> The caret which represents the point at which the content should be /// inserted. </param> /// <param name="text"> The text that should be inserted into the text fragment </param> public TextCaret Insert(TextCaret caret, string text) { if (caret.Content != this) { throw new ArgumentException("Caret does not refer to this Content instance", nameof(caret)); } if (text == null) { throw new ArgumentNullException(nameof(text)); } // easy, it's empty if (text == "") { return(caret); } var originalNumGraphemes = _buffer.GraphemeLength; _buffer.InsertText(caret.Offset.CharOffset, text); var nowNumGraphemes = _buffer.GraphemeLength; NotifyChanged(); return(TextCaret.FromOffset(this, caret.Offset.GraphemeOffset + nowNumGraphemes - originalNumGraphemes)); }
/// <summary> Retrieves a cursor that points at the given character. </summary> /// <exception cref="Exception"> Thrown when an exception error condition occurs. </exception> /// <param name="graphemeIndex"> The index of the grapheme to point at. </param> /// <returns> A TextBlockValueCursor that is pointing at the given grapheme. </returns> public TextCaret CursorFromGraphemeIndex(int graphemeIndex) { if (graphemeIndex < 0 || graphemeIndex > _buffer.GraphemeLength) { throw new ArgumentException($"Invalid index for cursor; index={graphemeIndex}; maximum={_buffer.GraphemeLength}", nameof(graphemeIndex)); } return(TextCaret.FromOffset(this, graphemeIndex)); }
/// <summary> Deletes text at the given position. </summary> public static TextCaret DeleteText(this TextCaret caret, int numberOfGraphemes) { var endCaret = caret; while (numberOfGraphemes > 0) { var next = endCaret.GetNextPosition(); if (!next.IsValid) { break; } endCaret = next; numberOfGraphemes--; } // TODO special case when we're deleting the entire fragment caret.Content.DeleteText(caret.Offset, endCaret.Offset); return(TextCaret.FromOffset(caret.Content, caret.Offset.GraphemeOffset)); }