Class SourceText
An abstraction of source text.
Inheritance
Inherited Members
Namespace: Loretta.CodeAnalysis.Text
Assembly: Loretta.CodeAnalysis.dll
Syntax
public abstract class SourceText
Properties
| Improve this Doc View SourceChecksumAlgorithm
Hash algorithm to use to calculate checksum of the text that's saved to PDB.
Declaration
public SourceHashAlgorithm ChecksumAlgorithm { get; }
Property Value
Type | Description |
---|---|
SourceHashAlgorithm |
Container
The container of this SourceText.
Declaration
public virtual SourceTextContainer Container { get; }
Property Value
Type | Description |
---|---|
SourceTextContainer |
Encoding
Encoding of the file that the text was read from or is going to be saved to.
null
if the encoding is unspecified.
Declaration
public abstract Encoding Encoding { get; }
Property Value
Type | Description |
---|---|
System.Text.Encoding |
Remarks
If the encoding is not specified the source isn't debuggable. If an encoding-less SourceText is written to a file a System.Text.Encoding.UTF8 shall be used as a default.
Item[Int32]
Returns a character at given position.
Declaration
public abstract char this[int position] { get; }
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | position | The position to get the character from. |
Property Value
Type | Description |
---|---|
System.Char | The character. |
Length
The length of the text in characters.
Declaration
public abstract int Length { get; }
Property Value
Type | Description |
---|---|
System.Int32 |
Lines
The collection of individual text lines.
Declaration
public TextLineCollection Lines { get; }
Property Value
Type | Description |
---|---|
TextLineCollection |
Methods
| Improve this Doc View SourceContentEquals(SourceText)
Compares the content with content of another SourceText.
Declaration
public bool ContentEquals(SourceText other)
Parameters
Type | Name | Description |
---|---|---|
SourceText | other |
Returns
Type | Description |
---|---|
System.Boolean |
CopyTo(Int32, Char[], Int32, Int32)
Copy a range of characters from this SourceText to a destination array.
Declaration
public abstract void CopyTo(int sourceIndex, char[] destination, int destinationIndex, int count)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | sourceIndex | |
System.Char[] | destination | |
System.Int32 | destinationIndex | |
System.Int32 | count |
From(Stream, Encoding, SourceHashAlgorithm, Boolean)
Constructs a SourceText from stream content.
Declaration
public static SourceText From(Stream stream, Encoding encoding = null, SourceHashAlgorithm checksumAlgorithm = SourceHashAlgorithm.Sha1, bool throwIfBinaryDetected = false)
Parameters
Type | Name | Description |
---|---|---|
Stream | stream | Stream. The stream must be seekable. |
System.Text.Encoding | encoding | Data encoding to use if the stream doesn't start with Byte Order Mark specifying the encoding. System.Text.Encoding.UTF8 if not specified. |
SourceHashAlgorithm | checksumAlgorithm | Hash algorithm to use to calculate checksum of the text that's saved to PDB. |
System.Boolean | throwIfBinaryDetected | If the decoded text contains at least two consecutive NUL
characters, then an |
Returns
Type | Description |
---|---|
SourceText |
Remarks
Reads from the beginning of the stream. Leaves the stream open.
Exceptions
Type | Condition |
---|---|
System.Text.DecoderFallbackException | If the given encoding is set to use a throwing decoder as a fallback |
From(Byte[], Int32, Encoding, SourceHashAlgorithm, Boolean)
Constructs a SourceText from a byte array.
Declaration
public static SourceText From(byte[] buffer, int length, Encoding encoding = null, SourceHashAlgorithm checksumAlgorithm = SourceHashAlgorithm.Sha1, bool throwIfBinaryDetected = false)
Parameters
Type | Name | Description |
---|---|---|
System.Byte[] | buffer | The encoded source buffer. |
System.Int32 | length | The number of bytes to read from the buffer. |
System.Text.Encoding | encoding | Data encoding to use if the encoded buffer doesn't start with Byte Order Mark. System.Text.Encoding.UTF8 if not specified. |
SourceHashAlgorithm | checksumAlgorithm | Hash algorithm to use to calculate checksum of the text that's saved to PDB. |
System.Boolean | throwIfBinaryDetected | If the decoded text contains at least two consecutive NUL
characters, then an |
Returns
Type | Description |
---|---|
SourceText | The decoded text. |
Exceptions
Type | Condition |
---|---|
System.Text.DecoderFallbackException | If the given encoding is set to use a throwing decoder as a fallback |
From(String, Encoding, SourceHashAlgorithm)
Constructs a SourceText from text in a string.
Declaration
public static SourceText From(string text, Encoding encoding = null, SourceHashAlgorithm checksumAlgorithm = SourceHashAlgorithm.Sha1)
Parameters
Type | Name | Description |
---|---|---|
System.String | text | Text. |
System.Text.Encoding | encoding | Encoding of the file that the |
SourceHashAlgorithm | checksumAlgorithm | Hash algorithm to use to calculate checksum of the text that's saved to PDB. |
Returns
Type | Description |
---|---|
SourceText |
From(TextReader, Int32, Encoding, SourceHashAlgorithm)
Constructs a SourceText from text in a string.
Declaration
public static SourceText From(TextReader reader, int length, Encoding encoding = null, SourceHashAlgorithm checksumAlgorithm = SourceHashAlgorithm.Sha1)
Parameters
Type | Name | Description |
---|---|---|
TextReader | reader | TextReader |
System.Int32 | length | length of content from |
System.Text.Encoding | encoding | Encoding of the file that the |
SourceHashAlgorithm | checksumAlgorithm | Hash algorithm to use to calculate checksum of the text that's saved to PDB. |
Returns
Type | Description |
---|---|
SourceText |
GetChangeRanges(SourceText)
Gets the set of TextChangeRange that describe how the text changed between this text an older version. This may be multiple detailed changes or a single change encompassing the entire text.
Declaration
public virtual IReadOnlyList<TextChangeRange> GetChangeRanges(SourceText oldText)
Parameters
Type | Name | Description |
---|---|---|
SourceText | oldText |
Returns
Type | Description |
---|---|
IReadOnlyList<TextChangeRange> |
GetChecksum()
Returns the checksum for this text.
Declaration
public ImmutableArray<byte> GetChecksum()
Returns
Type | Description |
---|---|
ImmutableArray<System.Byte> |
GetSubText(TextSpan)
Gets a SourceText that contains the characters in the specified span of this text.
Declaration
public virtual SourceText GetSubText(TextSpan span)
Parameters
Type | Name | Description |
---|---|---|
TextSpan | span |
Returns
Type | Description |
---|---|
SourceText |
GetSubText(Int32)
Returns a SourceText that has the contents of this text including and after the start position.
Declaration
public SourceText GetSubText(int start)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | start |
Returns
Type | Description |
---|---|
SourceText |
GetTextChanges(SourceText)
Gets the set of TextChange that describe how the text changed between this text and an older version. This may be multiple detailed changes or a single change encompassing the entire text.
Declaration
public virtual IReadOnlyList<TextChange> GetTextChanges(SourceText oldText)
Parameters
Type | Name | Description |
---|---|---|
SourceText | oldText |
Returns
Type | Description |
---|---|
IReadOnlyList<TextChange> |
Replace(TextSpan, String)
Returns a new SourceText with the specified span of characters replaced by the new text.
Declaration
public SourceText Replace(TextSpan span, string newText)
Parameters
Type | Name | Description |
---|---|---|
TextSpan | span | |
System.String | newText |
Returns
Type | Description |
---|---|
SourceText |
Replace(Int32, Int32, String)
Returns a new SourceText with the specified range of characters replaced by the new text.
Declaration
public SourceText Replace(int start, int length, string newText)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | start | |
System.Int32 | length | |
System.String | newText |
Returns
Type | Description |
---|---|
SourceText |
ToString()
Provides a string representation of the SourceText.
Declaration
public override string ToString()
Returns
Type | Description |
---|---|
System.String |
Overrides
ToString(TextSpan)
Gets a string containing the characters in specified span.
Declaration
public virtual string ToString(TextSpan span)
Parameters
Type | Name | Description |
---|---|---|
TextSpan | span |
Returns
Type | Description |
---|---|
System.String |
WithChanges(IEnumerable<TextChange>)
Constructs a new SourceText from this text with the specified changes.
Declaration
public virtual SourceText WithChanges(IEnumerable<TextChange> changes)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<TextChange> | changes |
Returns
Type | Description |
---|---|
SourceText |
WithChanges(TextChange[])
Constructs a new SourceText from this text with the specified changes.
Declaration
public SourceText WithChanges(params TextChange[] changes)
Parameters
Type | Name | Description |
---|---|---|
TextChange[] | changes |
Returns
Type | Description |
---|---|
SourceText |
Remarks
Changes do not have to be in sorted order. However, WithChanges(IEnumerable<TextChange>) will perform better if they are.
Write(TextWriter, CancellationToken)
Write this SourceText to a text writer.
Declaration
public void Write(TextWriter textWriter, CancellationToken cancellationToken = null)
Parameters
Type | Name | Description |
---|---|---|
TextWriter | textWriter | |
CancellationToken | cancellationToken |
Write(TextWriter, TextSpan, CancellationToken)
Write a span of text to a text writer.
Declaration
public virtual void Write(TextWriter writer, TextSpan span, CancellationToken cancellationToken = null)
Parameters
Type | Name | Description |
---|---|---|
TextWriter | writer | |
TextSpan | span | |
CancellationToken | cancellationToken |