TextRange Struct
Represents a zero-based range of two offset values within a text document, inclusive of the start offset and exclusive of the end offset.
public readonly struct TextRange : IComparable, IComparable<TextRange>, IEquatable<TextRange>
- Implements:
- IComparable IComparable<TextRange> IEquatable<TextRange>
Constructors
TextRange(int)
Initializes an instance of the structure using the specified offset for both the start and end values.
public TextRange(int offset)
| Parameter | Type | Description |
|---|---|---|
| offset | int | The start and end offset in the range. |
TextRange(int, int)
Represents a zero-based range of two offset values within a text document, inclusive of the start offset and exclusive of the end offset.
public TextRange(int startOffset, int endOffset)
| Parameter | Type | Description |
|---|---|---|
| startOffset | int | The start offset in the range. |
| endOffset | int | The end offset in the range. |
Properties
AbsoluteLength
The absolute number of characters encompassed by the TextRange.
public int AbsoluteLength { get; }
Property Value
Remarks
This value will always return a positive length, even if the range is not normalized.
EndOffset
The end offset in the range.
public int EndOffset { get; }
Property Value
Remarks
The end offset may occur before the start offset if the range is not normalized. The IsNormalized property indicates if the range is normalized. To get the normalized first offset in the range, use the FirstOffset property. To get the normalized last offset in the range, use the LastOffset property.
FirstOffset
The first offset in the range.
public int FirstOffset { get; }
Property Value
Remarks
This property returns either the StartOffset or EndOffset property, whichever comes first.
IsNormalized
Indicates whether the range is normalized (StartOffset occurs at or before the EndOffset).
public bool IsNormalized { get; }
Property Value
- bool:
trueif the range is normalized; otherwise,false.
IsZeroLength
Indicates whether the range has a zero length.
public bool IsZeroLength { get; }
Property Value
- bool:
trueif the range has a zero length; otherwise,false.
LastOffset
The last offset in the range.
public int LastOffset { get; }
Property Value
Remarks
This property returns either the StartOffset or EndOffset property, whichever comes last.
Length
The number of characters encompassed by the TextRange.
public int Length { get; }
Property Value
Remarks
This value will return a negative length if the range is not normalized.
Normalized
A TextRange that ensures the StartOffset occurs at or before the EndOffset offset.
StartOffset
The start offset in the range.
public int StartOffset { get; }
Property Value
Remarks
The start offset may occur after the end offset if the range is not normalized. The IsNormalized property indicates if the range is normalized. To get the normalized first offset in the range, use the FirstOffset property. To get the normalized last offset in the range, use the LastOffset property.
Methods
BordersOn(TextRange)
public bool BordersOn(TextRange range)
| Parameter | Type | Description |
|---|---|---|
| range | TextRange | The TextRange to examine. |
Returns
BordersOn(int)
Returns whether the TextRange borders on the specified offset.
public bool BordersOn(int offset)
| Parameter | Type | Description |
|---|---|---|
| offset | int | The offset to examine. |
Returns
Remarks
This method checks to see if the specified offset is either the StartOffset or EndOffset.
CompareTo(TextRange)
Compares the current instance with another object of the same type and returns an integer that indicates whether the current instance precedes, follows, or occurs in the same position in the sort order as the other object.
public int CompareTo(TextRange textRange)
| Parameter | Type | Description |
|---|---|---|
| textRange | TextRange |
Returns
- int:
A value that indicates the relative order of the objects being compared. The return value has these meanings:
Value Meaning Less than zero This instance precedes otherin the sort order.Zero This instance occurs in the same position in the sort order as other.Greater than zero This instance follows otherin the sort order.
Contains(TextRange)
public bool Contains(TextRange range)
| Parameter | Type | Description |
|---|---|---|
| range | TextRange | The TextRange to examine. |
Returns
Contains(int)
Returns whether the TextRange contains the specified offset.
public bool Contains(int offset)
| Parameter | Type | Description |
|---|---|---|
| offset | int | The offset to examine. |
Returns
Remarks
This method returns false if the offset falls on the LastOffset offset
since this method indicates if the offset is encompassed by the TextRange.
Equals(TextRange)
Indicates whether the current object is equal to another object of the same type.
public bool Equals(TextRange other)
| Parameter | Type | Description |
|---|---|---|
| other | TextRange | An object to compare with this object. |
Returns
- bool:
trueif the current object is equal to theotherparameter; otherwise,false.
Equals(object?)
Indicates whether this instance and a specified object are equal.
public override bool Equals(object? obj)
| Parameter | Type | Description |
|---|---|---|
| obj | object | The object to compare with the current instance. |
Returns
- bool:
trueifobjand this instance are the same type and represent the same value; otherwise,false.
FromSpan(int, int)
Returns a TextRange that covers the specified offset and length.
public static TextRange FromSpan(int offset, int length)
| Parameter | Type | Description |
|---|---|---|
| offset | int | The start offset. |
| length | int | The length of the range. |
Returns
GetHashCode()
Returns the hash code for this instance.
public override int GetHashCode()
Returns
- int:
A 32-bit signed integer that is the hash code for this instance.
Intersect(TextRange, TextRange)
Returns the intersection of two TextRange objects.
public static TextRange? Intersect(TextRange range1, TextRange range2)
| Parameter | Type | Description |
|---|---|---|
| range1 | TextRange | The first TextRange to examine. |
| range2 | TextRange | The second TextRange to examine. |
Returns
IntersectsWith(TextRange)
public bool IntersectsWith(TextRange range)
| Parameter | Type | Description |
|---|---|---|
| range | TextRange | The TextRange to examine. |
Returns
Remarks
This method returns whether the specified TextRange borders on or overlaps the TextRange.
IntersectsWith(TextRange, bool, bool)
public bool IntersectsWith(TextRange range, bool includeFirstEdge, bool includeLastEdge)
| Parameter | Type | Description |
|---|---|---|
| range | TextRange | The TextRange to examine. |
| includeFirstEdge | bool | Whether to return |
| includeLastEdge | bool | Whether to return |
Returns
Remarks
This method returns whether the specified TextRange borders on or overlaps the TextRange.
IntersectsWith(int)
Returns whether the TextRange intersects with the specified offset.
public bool IntersectsWith(int offset)
| Parameter | Type | Description |
|---|---|---|
| offset | int | The offset to examine. |
Returns
Remarks
This method returns whether the specified offset borders on or lies within the TextRange.
OverlapsWith(TextRange)
public bool OverlapsWith(TextRange range)
| Parameter | Type | Description |
|---|---|---|
| range | TextRange | The TextRange to examine. |
Returns
ToString()
Returns the string representation of this object.
Translate(ITextSnapshot, ITextSnapshot, TextRangeTrackingModes)
Translates this text range from one ITextSnapshot to another in the same ITextDocument.
public TextRange? Translate(ITextSnapshot fromSnapshot, ITextSnapshot toSnapshot, TextRangeTrackingModes trackingModes)
| Parameter | Type | Description |
|---|---|---|
| fromSnapshot | ITextSnapshot | The ITextSnapshot to which to text range belongs. |
| toSnapshot | ITextSnapshot | The ITextSnapshot that is the target of the translation. |
| trackingModes | TextRangeTrackingModes | A TextRangeTrackingModes indicating the tracking modes to use. |
Returns
Translate(ITextVersion, ITextVersion, TextRangeTrackingModes)
Translates this text range from one ITextVersion to another in the same ITextDocument.
public TextRange? Translate(ITextVersion fromVersion, ITextVersion toVersion, TextRangeTrackingModes trackingModes)
| Parameter | Type | Description |
|---|---|---|
| fromVersion | ITextVersion | The ITextVersion to which to text range belongs. |
| toVersion | ITextVersion | The ITextVersion that is the target of the translation. |
| trackingModes | TextRangeTrackingModes | A TextRangeTrackingModes indicating the tracking modes to use. |
Returns
Union(TextRange, TextRange)
Returns the normalized union of two TextRange objects.
public static TextRange Union(TextRange range1, TextRange range2)
| Parameter | Type | Description |
|---|---|---|
| range1 | TextRange | The first TextRange to examine. |
| range2 | TextRange | The second TextRange to examine. |
Returns
Operators
operator ==(TextRange, TextRange)
Compares two TextRange objects. The result specifies whether the values of the StartOffset and EndOffset properties of the two TextRange objects are equal.
public static bool operator ==(TextRange left, TextRange right)
| Parameter | Type | Description |
|---|---|---|
| left | TextRange | A TextRange to compare. |
| right | TextRange | A TextRange to compare. |
Returns
- bool:
trueif the StartOffset and EndOffset values ofleftandrightare equal; otherwise,false.
operator >(TextRange, TextRange)
Compares whether one TextRange is greater than the other.
public static bool operator >(TextRange left, TextRange right)
| Parameter | Type | Description |
|---|---|---|
| left | TextRange | A TextRange to compare. |
| right | TextRange | A TextRange to compare. |
Returns
operator >=(TextRange, TextRange)
Compares whether one TextRange is greater than or equal to the other.
public static bool operator >=(TextRange left, TextRange right)
| Parameter | Type | Description |
|---|---|---|
| left | TextRange | A TextRange to compare. |
| right | TextRange | A TextRange to compare. |
Returns
operator !=(TextRange, TextRange)
Compares two TextRange objects. The result specifies whether the values of the StartOffset and EndOffset properties of the two TextRange objects are unequal.
public static bool operator !=(TextRange left, TextRange right)
| Parameter | Type | Description |
|---|---|---|
| left | TextRange | A TextRange to compare. |
| right | TextRange | A TextRange to compare. |
Returns
- bool:
trueif the StartOffset and EndOffset values ofleftandrightare unequal; otherwise,false.
operator <(TextRange, TextRange)
Compares whether one TextRange is less than the other.
public static bool operator <(TextRange left, TextRange right)
| Parameter | Type | Description |
|---|---|---|
| left | TextRange | A TextRange to compare. |
| right | TextRange | A TextRange to compare. |
Returns
operator <=(TextRange, TextRange)
Compares whether one TextRange is less than or equal to the other.
public static bool operator <=(TextRange left, TextRange right)
| Parameter | Type | Description |
|---|---|---|
| left | TextRange | A TextRange to compare. |
| right | TextRange | A TextRange to compare. |