C# Class ICSharpCode.AvalonEdit.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: ITextSource, INotifyPropertyChanged
Afficher le fichier Open project: gilgame/SEWorkbench Class Usage Examples

Méthodes publiques

Méthode 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 ( ) : System.IO.TextReader
CreateReader ( int offset, int length ) : System.IO.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 ( System writer ) : void
WriteTextTo ( System writer, int offset, int length ) : void

Private Methods

Méthode 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 méthode

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
Résultat void

CreateAnchor() public méthode

Creates a new TextAnchor at the specified offset.
public CreateAnchor ( int offset ) : TextAnchor
offset int
Résultat TextAnchor

CreateDocumentSnapshot() public méthode

public CreateDocumentSnapshot ( ) : IDocument
Résultat IDocument

CreateReader() public méthode

public CreateReader ( ) : System.IO.TextReader
Résultat System.IO.TextReader

CreateReader() public méthode

public CreateReader ( int offset, int length ) : System.IO.TextReader
offset int
length int
Résultat System.IO.TextReader

CreateSnapshot() public méthode

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
Résultat ITextSource

CreateSnapshot() public méthode

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

EndUpdate() public méthode

Ends a group of document changes.
public EndUpdate ( ) : void
Résultat void

GetCharAt() public méthode

public GetCharAt ( int offset ) : char
offset int
Résultat char

GetLineByNumber() public méthode

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

GetLocation() public méthode

Gets the location from an offset.
public GetLocation ( int offset ) : TextLocation
offset int
Résultat TextLocation

GetOffset() public méthode

Gets the offset from a text location.
public GetOffset ( TextLocation location ) : int
location TextLocation
Résultat int

GetOffset() public méthode

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

GetText() public méthode

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

GetText() public méthode

public GetText ( int offset, int length ) : string
offset int
length int
Résultat string

IndexOf() public méthode

public IndexOf ( char c, int startIndex, int count ) : int
c char
startIndex int
count int
Résultat int

IndexOf() public méthode

public IndexOf ( string searchText, int startIndex, int count, System.StringComparison comparisonType ) : int
searchText string
startIndex int
count int
comparisonType System.StringComparison
Résultat int

IndexOfAny() public méthode

public IndexOfAny ( char anyOf, int startIndex, int count ) : int
anyOf char
startIndex int
count int
Résultat int

Insert() public méthode

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.
Résultat void

Insert() public méthode

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. ///
Résultat void

Insert() public méthode

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.
Résultat void

Insert() public méthode

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. ///
Résultat void

LastIndexOf() public méthode

public LastIndexOf ( char c, int startIndex, int count ) : int
c char
startIndex int
count int
Résultat int

LastIndexOf() public méthode

public LastIndexOf ( string searchText, int startIndex, int count, System.StringComparison comparisonType ) : int
searchText string
startIndex int
count int
comparisonType System.StringComparison
Résultat int

Remove() public méthode

Removes text.
public Remove ( ISegment segment ) : void
segment ISegment
Résultat void

Remove() public méthode

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.
Résultat void

Replace() public méthode

Replaces text.
public Replace ( ISegment segment, ITextSource text ) : void
segment ISegment
text ITextSource
Résultat void

Replace() public méthode

Replaces text.
public Replace ( ISegment segment, string text ) : void
segment ISegment
text string
Résultat void

Replace() public méthode

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.
Résultat void

Replace() public méthode

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. ///
Résultat void

Replace() public méthode

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.
Résultat void

Replace() public méthode

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.
Résultat void

Replace() public méthode

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. ///
Résultat void

Replace() public méthode

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.
Résultat void

RunUpdate() public méthode

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

SetOwnerThread() public méthode

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
Résultat void

TextDocument() public méthode

Create an empty text document.
public TextDocument ( ) : System
Résultat System

TextDocument() public méthode

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

TextDocument() public méthode

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

VerifyAccess() public méthode

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
Résultat void

WriteTextTo() public méthode

public WriteTextTo ( System writer ) : void
writer System
Résultat void

WriteTextTo() public méthode

public WriteTextTo ( System writer, int offset, int length ) : void
writer System
offset int
length int
Résultat void