SourceText Class
Represents immutable source text with line information and text-change application helpers.
public sealed class SourceTextProperties
| Name | Description |
|---|---|
| Length | |
| Lines | |
| Text |
Length
SourceText.csLines
SourceText.csText
SourceText.csMethods
| Name | Description |
|---|---|
| From(string) | |
| GetChangeRanges(SourceText) | Describes how oldText was changed to produce this text, as spans of oldText. |
| GetLine(int) | Gets the line containing the specified character position. |
| GetSubText(TextSpan) | Returns the text covered by span as its own SourceText. |
| ToString() | |
| ToString(TextSpan) | Returns the characters covered by span. |
| WithChanges(IEnumerable<TextChange>) | Applies changes from the end of the text backwards, so the spans of the changes that come earlier in the text stay valid. A change reaching past the end of the text is ignored. |
From(string)
SourceText.cspublic static SourceText From(string text)GetChangeRanges(SourceText)
SourceText.csDescribes how oldText was changed to produce this text, as spans of oldText.
public IReadOnlyList<TextChangeRange> GetChangeRanges(SourceText oldText)Parameters
oldTextSourceText
Returns
IReadOnlyList<TextChangeRange> An empty list when the texts are equal, otherwise the single range covering everything that differs.
Exceptions
ArgumentNullExceptionoldTextisnull.
Remarks
The result is derived from the two texts alone, by trimming the common prefix and the common suffix, so it is always a single range. It does not attempt to recover the individual edits that were originally applied.
GetLine(int)
SourceText.csGets the line containing the specified character position.
Parameters
positionint
Returns
Exceptions
ArgumentOutOfRangeExceptionpositionis negative or past the end.
Remarks
position may be SourceText.Length, which is where a span that runs to the end of the text finishes; anything past that indexes no character and is rejected rather than reported as the last line.
GetSubText(TextSpan)
SourceText.csReturns the text covered by span as its own SourceText.
public SourceText GetSubText(TextSpan span)Parameters
spanTextSpan
Returns
Exceptions
ArgumentOutOfRangeExceptionspanreaches past the end of the text.
ToString overloads
ToString()
SourceText.csToString(TextSpan)
SourceText.csReturns the characters covered by span.
Parameters
spanTextSpan
Returns
Exceptions
ArgumentOutOfRangeExceptionspanreaches past the end of the text.
WithChanges(IEnumerable<TextChange>)
SourceText.csApplies changes from the end of the text backwards, so the spans of the changes that come earlier in the text stay valid. A change reaching past the end of the text is ignored.
public SourceText WithChanges(IEnumerable<TextChange> changes)