C# Class AvalonStudio.TextEditor.Document.TextDocument

This class is the main class of the text model. Basically, it is a System.Text.StringBuilder with events.
Thread safety:

However, there is a single method that is thread-safe: CreateSnapshot() (and its overloads).

Inheritance: IDocument, INotifyPropertyChanged
Show file Open project: VitalElement/AvalonStudio Class Usage Examples

Public Methods

Method Description
BeginUpdate ( ) : void

Begins a group of document changes.

Some events are suspended until EndUpdate is called, and the UndoStack will group all changes into a single action.

Calling BeginUpdate several times increments a counter, only after the appropriate number of EndUpdate calls the events resume their work.

CreateAnchor ( int offset ) : TextAnchor

Creates a new TextAnchor at the specified offset.

CreateDocumentSnapshot ( ) : IDocument
CreateReader ( ) : TextReader
CreateReader ( int offset, int length ) : TextReader
CreateSnapshot ( ) : ITextSource

Creates a snapshot of the current text.

This method returns an immutable snapshot of the document, and may be safely called even when the document's owner thread is concurrently modifying the document.

This special thread-safety guarantee is valid only for TextDocument.CreateSnapshot(), not necessarily for other classes implementing ITextSource.CreateSnapshot().

CreateSnapshot ( int offset, int length ) : ITextSource

Creates a snapshot of a part of the current text.

EndUpdate ( ) : void

Ends a group of document changes.

GetCharAt ( int offset ) : char
GetLineByNumber ( int number ) : DocumentLine

Gets a line by the line number: O(log n)

GetLocation ( int offset ) : TextLocation

Gets the location from an offset.

GetOffset ( TextLocation location ) : int

Gets the offset from a text location.

GetOffset ( int line, int column ) : int

Gets the offset from a text location.

GetText ( ISegment segment ) : string

Retrieves the text for a portion of the document.

GetText ( int offset, int length ) : string
IndexOf ( char c, int startIndex, int count ) : int
IndexOf ( string searchText, int startIndex, int count, System.StringComparison comparisonType ) : int
IndexOfAny ( char anyOf, int startIndex, int count ) : int
Insert ( int offset, ITextSource text ) : void

Inserts text.

Anchors positioned exactly at the insertion offset will move according to their movement type. For AnchorMovementType.Default, they will move behind the inserted text. The caret will also move behind the inserted text.

Insert ( int offset, ITextSource text, AnchorMovementType defaultAnchorMovementType ) : void

Inserts text.

Insert ( int offset, string text ) : void

Inserts text.

Anchors positioned exactly at the insertion offset will move according to their movement type. For AnchorMovementType.Default, they will move behind the inserted text. The caret will also move behind the inserted text.

Insert ( int offset, string text, AnchorMovementType defaultAnchorMovementType ) : void

Inserts text.

LastIndexOf ( char c, int startIndex, int count ) : int
LastIndexOf ( string searchText, int startIndex, int count, System.StringComparison comparisonType ) : int
Remove ( ISegment segment ) : void

Removes text.

Remove ( int offset, int length ) : void

Removes text.

Replace ( ISegment segment, ITextSource text ) : void

Replaces text.

Replace ( ISegment segment, string text ) : void

Replaces text.

Replace ( int offset, int length, ITextSource text ) : void

Replaces text.

Replace ( int offset, int length, ITextSource text, OffsetChangeMap offsetChangeMap ) : void

Replaces text.

Replace ( int offset, int length, ITextSource text, OffsetChangeMappingType offsetChangeMappingType ) : void

Replaces text.

Replace ( int offset, int length, string text ) : void

Replaces text.

Replace ( int offset, int length, string text, OffsetChangeMap offsetChangeMap ) : void

Replaces text.

Replace ( int offset, int length, string text, OffsetChangeMappingType offsetChangeMappingType ) : void

Replaces text.

RunUpdate ( ) : IDisposable

Immediately calls BeginUpdate(), and returns an IDisposable that calls EndUpdate().

SetOwnerThread ( Thread newOwner ) : void

Transfers ownership of the document to another thread. This method can be used to load a file into a TextDocument on a background thread and then transfer ownership to the UI thread for displaying the document.

The owner can be set to null, which means that no thread can access the document. But, if the document has no owner thread, any thread may take ownership by calling SetOwnerThread.

TextDocument ( ) : System

Create an empty text document.

TextDocument ( IEnumerable initialText ) : System

Create a new text document with the specified initial text.

TextDocument ( ITextSource initialText ) : System

Create a new text document with the specified initial text.

VerifyAccess ( ) : void

Verifies that the current thread is the documents owner thread. Throws an InvalidOperationException if the wrong thread accesses the TextDocument.

The TextDocument class is not thread-safe. A document instance expects to have a single owner thread and will throw an InvalidOperationException when accessed from another thread. It is possible to change the owner thread using the SetOwnerThread method.

WriteTextTo ( TextWriter writer ) : void
WriteTextTo ( TextWriter writer, int offset, int length ) : void

Private Methods

Method Description
DebugVerifyAccess ( ) : void
DoReplace ( int offset, int length, ITextSource newText, OffsetChangeMap offsetChangeMap ) : void
FireChangeEvents ( ) : void

Fires TextChanged, TextLengthChanged, LineCountChanged if required.

GetLineByOffset ( int offset ) : DocumentLine
GetLineTreeAsString ( ) : string
GetTextAnchorTreeAsString ( ) : string
GetTextFromTextSource ( ITextSource textSource ) : IEnumerable
IDocument ( ) : IDisposable
IDocument ( int lineNumber ) : IDocumentLine
IDocument ( int offset ) : ITextAnchor
IDocument ( ) : void
IServiceProvider ( Type serviceType ) : object
OnFileNameChanged ( EventArgs e ) : void
OnPropertyChanged ( string propertyName ) : void
ThrowIfRangeInvalid ( int offset, int length ) : void

Method Details

BeginUpdate() public method

Begins a group of document changes.

Some events are suspended until EndUpdate is called, and the UndoStack will group all changes into a single action.

Calling BeginUpdate several times increments a counter, only after the appropriate number of EndUpdate calls the events resume their work.

public BeginUpdate ( ) : void
return void

CreateAnchor() public method

Creates a new TextAnchor at the specified offset.
public CreateAnchor ( int offset ) : TextAnchor
offset int
return TextAnchor

CreateDocumentSnapshot() public method

public CreateDocumentSnapshot ( ) : IDocument
return IDocument

CreateReader() public method

public CreateReader ( ) : TextReader
return System.IO.TextReader

CreateReader() public method

public CreateReader ( int offset, int length ) : TextReader
offset int
length int
return System.IO.TextReader

CreateSnapshot() public method

Creates a snapshot of the current text.

This method returns an immutable snapshot of the document, and may be safely called even when the document's owner thread is concurrently modifying the document.

This special thread-safety guarantee is valid only for TextDocument.CreateSnapshot(), not necessarily for other classes implementing ITextSource.CreateSnapshot().

public CreateSnapshot ( ) : ITextSource
return ITextSource

CreateSnapshot() public method

Creates a snapshot of a part of the current text.
public CreateSnapshot ( int offset, int length ) : ITextSource
offset int
length int
return ITextSource

EndUpdate() public method

Ends a group of document changes.
public EndUpdate ( ) : void
return void

GetCharAt() public method

public GetCharAt ( int offset ) : char
offset int
return char

GetLineByNumber() public method

Gets a line by the line number: O(log n)
public GetLineByNumber ( int number ) : DocumentLine
number int
return DocumentLine

GetLocation() public method

Gets the location from an offset.
public GetLocation ( int offset ) : TextLocation
offset int
return TextLocation

GetOffset() public method

Gets the offset from a text location.
public GetOffset ( TextLocation location ) : int
location TextLocation
return int

GetOffset() public method

Gets the offset from a text location.
public GetOffset ( int line, int column ) : int
line int
column int
return int

GetText() public method

Retrieves the text for a portion of the document.
public GetText ( ISegment segment ) : string
segment ISegment
return string

GetText() public method

public GetText ( int offset, int length ) : string
offset int
length int
return string

IndexOf() public method

public IndexOf ( char c, int startIndex, int count ) : int
c char
startIndex int
count int
return int

IndexOf() public method

public IndexOf ( string searchText, int startIndex, int count, System.StringComparison comparisonType ) : int
searchText string
startIndex int
count int
comparisonType System.StringComparison
return int

IndexOfAny() public method

public IndexOfAny ( char anyOf, int startIndex, int count ) : int
anyOf char
startIndex int
count int
return int

Insert() public method

Inserts text.
Anchors positioned exactly at the insertion offset will move according to their movement type. For AnchorMovementType.Default, they will move behind the inserted text. The caret will also move behind the inserted text.
public Insert ( int offset, ITextSource text ) : void
offset int The offset at which the text is inserted.
text ITextSource The new text.
return void

Insert() public method

Inserts text.
public Insert ( int offset, ITextSource text, AnchorMovementType defaultAnchorMovementType ) : void
offset int The offset at which the text is inserted.
text ITextSource The new text.
defaultAnchorMovementType AnchorMovementType /// Anchors positioned exactly at the insertion offset will move according to the anchor's movement type. /// For AnchorMovementType.Default, they will move according to the movement type specified by this parameter. /// The caret will also move according to the parameter. ///
return void

Insert() public method

Inserts text.
Anchors positioned exactly at the insertion offset will move according to their movement type. For AnchorMovementType.Default, they will move behind the inserted text. The caret will also move behind the inserted text.
public Insert ( int offset, string text ) : void
offset int The offset at which the text is inserted.
text string The new text.
return void

Insert() public method

Inserts text.
public Insert ( int offset, string text, AnchorMovementType defaultAnchorMovementType ) : void
offset int The offset at which the text is inserted.
text string The new text.
defaultAnchorMovementType AnchorMovementType /// Anchors positioned exactly at the insertion offset will move according to the anchor's movement type. /// For AnchorMovementType.Default, they will move according to the movement type specified by this parameter. /// The caret will also move according to the parameter. ///
return void

LastIndexOf() public method

public LastIndexOf ( char c, int startIndex, int count ) : int
c char
startIndex int
count int
return int

LastIndexOf() public method

public LastIndexOf ( string searchText, int startIndex, int count, System.StringComparison comparisonType ) : int
searchText string
startIndex int
count int
comparisonType System.StringComparison
return int

Remove() public method

Removes text.
public Remove ( ISegment segment ) : void
segment ISegment
return void

Remove() public method

Removes text.
public Remove ( int offset, int length ) : void
offset int Starting offset of the text to be removed.
length int Length of the text to be removed.
return void

Replace() public method

Replaces text.
public Replace ( ISegment segment, ITextSource text ) : void
segment ISegment
text ITextSource
return void

Replace() public method

Replaces text.
public Replace ( ISegment segment, string text ) : void
segment ISegment
text string
return void

Replace() public method

Replaces text.
public Replace ( int offset, int length, ITextSource text ) : void
offset int The starting offset of the text to be replaced.
length int The length of the text to be replaced.
text ITextSource The new text.
return void

Replace() public method

Replaces text.
public Replace ( int offset, int length, ITextSource text, OffsetChangeMap offsetChangeMap ) : void
offset int The starting offset of the text to be replaced.
length int The length of the text to be replaced.
text ITextSource The new text.
offsetChangeMap OffsetChangeMap /// The offsetChangeMap determines how offsets inside the old text are mapped to the new text. /// This affects how the anchors and segments inside the replaced region behave. /// If you pass null (the default when using one of the other overloads), the offsets are changed as /// in OffsetChangeMappingType.Normal mode. /// If you pass OffsetChangeMap.Empty, then everything will stay in its old place /// (OffsetChangeMappingType.CharacterReplace mode). /// The offsetChangeMap must be a valid 'explanation' for the document change. See /// . /// Passing an OffsetChangeMap to the Replace method will automatically freeze it to ensure the thread safety of the /// resulting /// DocumentChangeEventArgs instance. ///
return void

Replace() public method

Replaces text.
public Replace ( int offset, int length, ITextSource text, OffsetChangeMappingType offsetChangeMappingType ) : void
offset int The starting offset of the text to be replaced.
length int The length of the text to be replaced.
text ITextSource The new text.
offsetChangeMappingType OffsetChangeMappingType /// The offsetChangeMappingType determines how offsets inside the old text are mapped to the new text. /// This affects how the anchors and segments inside the replaced region behave. ///
return void

Replace() public method

Replaces text.
public Replace ( int offset, int length, string text ) : void
offset int The starting offset of the text to be replaced.
length int The length of the text to be replaced.
text string The new text.
return void

Replace() public method

Replaces text.
public Replace ( int offset, int length, string text, OffsetChangeMap offsetChangeMap ) : void
offset int The starting offset of the text to be replaced.
length int The length of the text to be replaced.
text string The new text.
offsetChangeMap OffsetChangeMap /// The offsetChangeMap determines how offsets inside the old text are mapped to the new text. /// This affects how the anchors and segments inside the replaced region behave. /// If you pass null (the default when using one of the other overloads), the offsets are changed as /// in OffsetChangeMappingType.Normal mode. /// If you pass OffsetChangeMap.Empty, then everything will stay in its old place /// (OffsetChangeMappingType.CharacterReplace mode). /// The offsetChangeMap must be a valid 'explanation' for the document change. See /// . /// Passing an OffsetChangeMap to the Replace method will automatically freeze it to ensure the thread safety of the /// resulting /// DocumentChangeEventArgs instance. ///
return void

Replace() public method

Replaces text.
public Replace ( int offset, int length, string text, OffsetChangeMappingType offsetChangeMappingType ) : void
offset int The starting offset of the text to be replaced.
length int The length of the text to be replaced.
text string The new text.
offsetChangeMappingType OffsetChangeMappingType /// The offsetChangeMappingType determines how offsets inside the old text are mapped to the new text. /// This affects how the anchors and segments inside the replaced region behave. ///
return void

RunUpdate() public method

Immediately calls BeginUpdate(), and returns an IDisposable that calls EndUpdate().
public RunUpdate ( ) : IDisposable
return IDisposable

SetOwnerThread() public method

Transfers ownership of the document to another thread. This method can be used to load a file into a TextDocument on a background thread and then transfer ownership to the UI thread for displaying the document.

The owner can be set to null, which means that no thread can access the document. But, if the document has no owner thread, any thread may take ownership by calling SetOwnerThread.

public SetOwnerThread ( Thread newOwner ) : void
newOwner Thread
return void

TextDocument() public method

Create an empty text document.
public TextDocument ( ) : System
return System

TextDocument() public method

Create a new text document with the specified initial text.
public TextDocument ( IEnumerable initialText ) : System
initialText IEnumerable
return System

TextDocument() public method

Create a new text document with the specified initial text.
public TextDocument ( ITextSource initialText ) : System
initialText ITextSource
return System

VerifyAccess() public method

Verifies that the current thread is the documents owner thread. Throws an InvalidOperationException if the wrong thread accesses the TextDocument.

The TextDocument class is not thread-safe. A document instance expects to have a single owner thread and will throw an InvalidOperationException when accessed from another thread. It is possible to change the owner thread using the SetOwnerThread method.

public VerifyAccess ( ) : void
return void

WriteTextTo() public method

public WriteTextTo ( TextWriter writer ) : void
writer System.IO.TextWriter
return void

WriteTextTo() public method

public WriteTextTo ( TextWriter writer, int offset, int length ) : void
writer System.IO.TextWriter
offset int
length int
return void