SourceText Class

Represents immutable source text with line information and text-change application helpers.

public sealed class SourceText

Properties

NameDescription
Length
Lines
Text
public int Length { get; }

Property value

int

No documentation is available.

public IReadOnlyList<TextLine> Lines { get; }

No documentation is available.

public string Text { get; }

Property value

string

No documentation is available.

Methods

NameDescription
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.cs
public static SourceText From(string text)

Parameters

text string

Returns

SourceText

Exceptions

ArgumentNullException
text is null.

GetChangeRanges(SourceText)

SourceText.cs

Describes how oldText was changed to produce this text, as spans of oldText.

public IReadOnlyList<TextChangeRange> GetChangeRanges(SourceText oldText)

Parameters

oldText SourceText

Returns

IReadOnlyList<TextChangeRange> An empty list when the texts are equal, otherwise the single range covering everything that differs.

Exceptions

ArgumentNullException
oldText is null.

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.cs

Gets the line containing the specified character position.

public TextLine GetLine(int position)

Parameters

position int

Returns

TextLine

Exceptions

ArgumentOutOfRangeException
position is 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.cs

Returns the text covered by span as its own SourceText.

public SourceText GetSubText(TextSpan span)

Parameters

span TextSpan

Returns

SourceText

Exceptions

ArgumentOutOfRangeException
span reaches past the end of the text.

ToString overloads

ToString()

SourceText.cs
public override string ToString()
Returns

string

No documentation is available.

ToString(TextSpan)

SourceText.cs

Returns the characters covered by span.

public string ToString(TextSpan span)
Parameters
span TextSpan
Returns

string

Exceptions
ArgumentOutOfRangeException
span reaches past the end of the text.

WithChanges(IEnumerable<TextChange>)

SourceText.cs

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.

public SourceText WithChanges(IEnumerable<TextChange> changes)

Parameters

changes IEnumerable<TextChange>

Returns

SourceText