Class TextChangeSet
An ordered set of non-overlapping TextChanges, all expressed against the same original text.
public sealed class TextChangeSet : IReadOnlyList<TextChange>, IReadOnlyCollection<TextChange>, IEnumerable<TextChange>, IEnumerable
- Inheritance
-
TextChangeSet
- Implements
- Inherited Members
- Extension Methods
Remarks
The changes are kept sorted by start offset. Two changes may touch, but they can't overlap, and at most one pure insertion may sit at any offset, so applying the set is unambiguous.
A change set is the unit every edit of a design document produces, and the unit a future text editor receives to update its buffer without losing its caret or its own undo history.
Constructors
TextChangeSet(IEnumerable<TextChange>)
Initializes a new instance of the TextChangeSet class.
public TextChangeSet(IEnumerable<TextChange> changes)
Parameters
changesIEnumerable<TextChange>The changes, in any order.
Exceptions
- ArgumentNullException
changesis null.- ArgumentException
A change has null new text, two changes overlap, or two insertions share an offset.
Properties
Count
Gets the number of elements in the collection.
public int Count { get; }
Property Value
- int
The number of elements in the collection.
Empty
Gets a change set that changes nothing.
public static TextChangeSet Empty { get; }
Property Value
this[int]
Gets the element at the specified index in the read-only list.
public TextChange this[int index] { get; }
Parameters
indexintThe zero-based index of the element to get.
Property Value
- TextChange
The element at the specified index in the read-only list.
Methods
Apply(string)
Applies every change to text in one pass.
public string Apply(string text)
Parameters
textstringThe original text the changes were expressed against.
Returns
- string
The changed text.
Exceptions
- ArgumentNullException
textis null.- ArgumentException
A change lies outside
text.
GetEnumerator()
Returns an enumerator that iterates through the collection.
public IEnumerator<TextChange> GetEnumerator()
Returns
- IEnumerator<TextChange>
An enumerator that can be used to iterate through the collection.
Invert(string)
Creates the change set that turns the changed text back into originalText.
public TextChangeSet Invert(string originalText)
Parameters
originalTextstringThe text this set was expressed against.
Returns
- TextChangeSet
The inverse change set, expressed against the changed text.
Exceptions
- ArgumentNullException
originalTextis null.
MapPosition(int)
Maps an offset in the original text to the same character's offset in the changed text.
public int? MapPosition(int position)
Parameters
positionintAn offset in the original text.