Skip to main content

TextRange Class

Definition​

Assembly:Avalonia.Controls.Documents
Package:Avalonia.Controls.Documents

Represents a range of text between two pointers.

public class TextRange

Inheritance: object -> TextRange

Derived types:TextSelection

Constructors​

NameDescription
TextRangeInitializes a new instance of the Avalonia.Controls.Documents.TextModel.TextRange class.

TextRange Constructor​

Initializes a new instance of the Avalonia.Controls.Documents.TextModel.TextRange class.

public TextRange(Avalonia.Controls.Documents.TextModel.TextPointer start, Avalonia.Controls.Documents.TextModel.TextPointer end)

Parameters​

start Avalonia.Controls.Documents.TextModel.TextPointer

The start position.

end Avalonia.Controls.Documents.TextModel.TextPointer

The end position.

Methods​

NameDescription
ApplyPropertyValueNo summary available.
ClearAllPropertiesClears all inline formatting properties from the range. Content is preserved; only formatting is removed. Produces a single Avalonia.Controls.Documents.Undo.BlockSnapshotUndoUnit for atomic undo/redo.
ClearPropertyValueNo summary available.
Contains (2 overloads)Checks if the range contains the specified pointer.
CreateSnapshotCreates an immutable, thread-safe snapshot of this range's document structure. The caller chooses a serializer to convert the snapshot to a specific format.
DeleteDeletes the content of the range.
DeleteCurrentBlock (2 overloads)Deletes the block this range starts in, and collapses the range onto the deletion point.
DeleteTextDeletes the content of this range. Removes whatever the range covers — characters, inline runs, paragraphs, list items, tables, sections — depending on which nodes the range fully encloses. When the deletion crosses block boundaries and both surviving boundary blocks are paragraphs, mergeBlocks controls whether the surviving fragments fuse into one paragraph. Manages transaction automatically.
DeleteTextAtDeletes text at the specified range relative to this range. Manages transaction automatically.
GetPropertyValueNo summary available.
GetTextReturns the text content covered by this range.
InsertFootnoteReplaces the range content with a footnote: the anchor (its number as ordinary text) lands at the range position, splitting a run when it sits mid-run, and the note body is created in the document's trailing footnote container with a fresh id - selection delete, anchor, body, and the anchor renumbering are one undo unit. Manages transaction automatically.
InsertImageNo summary available.
InsertPageNumberFieldReplaces the range content with a page-number field of kind: the field lands at the range position, splitting a run when it sits mid-run, and shows its cached result until a page layout resolves it (a band on a sheet renders the sheet's number) - selection delete and insertion are one undo unit. Manages transaction automatically.
InsertSnapshotReplaces the range content with the document tree captured by snapshot. Block, inline, and formatting structure from the snapshot is preserved — the snapshot's rope tree is spliced directly into the live document with no plain-text lowering.
InsertTextAtInserts text at the specified offset relative to the range start. Manages transaction automatically.
LoadReplaces this range with the content of a stream.
OverlapsChecks if this range overlaps with another range.
ReplaceText (2 overloads)No summary available.
ReplaceTextAtReplaces text at the specified range relative to this range. Manages transaction automatically.
SaveWrites this range to a stream.
SetImageAltTextSets the alternative text of an existing Avalonia.Controls.Documents.RichImage in a single undoable change.
SetImageSizeSets the display size of an existing Avalonia.Controls.Documents.RichImage in a single undoable change. Pass double.NaN for a dimension to use the image's intrinsic pixel size.
ToStringNo summary available.
TryGetPropertyValueNo summary available.

ApplyPropertyValue Method​

public void ApplyPropertyValue<T>(Avalonia.StyledProperty<T> property, T value)

Parameters​

property Avalonia.StyledProperty<T>

value T

Type Parameters​

T

ClearAllProperties Method​

Clears all inline formatting properties from the range. Content is preserved; only formatting is removed. Produces a single Avalonia.Controls.Documents.Undo.BlockSnapshotUndoUnit for atomic undo/redo.

public void ClearAllProperties()

ClearPropertyValue Method​

public void ClearPropertyValue<T>(Avalonia.StyledProperty<T> property)

Parameters​

property Avalonia.StyledProperty<T>

Type Parameters​

T

Contains overloads​

Contains Method​

Checks if the range contains the specified pointer.

public bool Contains(Avalonia.Controls.Documents.TextModel.TextPointer pointer)
Parameters​

pointer Avalonia.Controls.Documents.TextModel.TextPointer

The pointer to check.

Returns​

bool

True if the pointer is within the range.

Contains Method​

Checks if the range contains the specified offset.

public bool Contains(int offset)
Parameters​

offset int

The offset to check.

Returns​

bool

True if the offset is within the range.

CreateSnapshot Method​

Creates an immutable, thread-safe snapshot of this range's document structure. The caller chooses a serializer to convert the snapshot to a specific format.

public Avalonia.Controls.Documents.Serialization.Snapshot.DocumentSnapshot CreateSnapshot()

Returns​

Avalonia.Controls.Documents.Serialization.Snapshot.DocumentSnapshot

A Avalonia.Controls.Documents.Serialization.Snapshot.DocumentSnapshot covering this range, or null if the range is empty or detached.

Delete Method​

Deletes the content of the range.

public Avalonia.Controls.Documents.TextModel.TextPointer Delete()

Returns​

Avalonia.Controls.Documents.TextModel.TextPointer

A pointer to the position after deletion.

DeleteCurrentBlock overloads​

DeleteCurrentBlock Method​

Deletes the block this range starts in, and collapses the range onto the deletion point.

public bool DeleteCurrentBlock()
Returns​

bool

True if a block was deleted; false otherwise.

Remarks​

To delete the block at some other position, build the range there: new TextRange(position, position).DeleteCurrentBlock(). That range gives up its own endpoints for the deletion, which is what makes the operation positional rather than a verb on an unrelated range.

Exceptions​

DeleteCurrentBlock Method​

Deletes the block at position.

public bool DeleteCurrentBlock(Avalonia.Controls.Documents.TextModel.TextPointer position)
Parameters​

position Avalonia.Controls.Documents.TextModel.TextPointer

Where to look. When null, the range's own start is used.

Returns​

bool

Whether a block was deleted.

Remarks​

Build a range at the position instead: new TextRange(position, position) reads as what it does and cannot quietly take a pointer into another document.

DeleteText Method​

Deletes the content of this range. Removes whatever the range covers — characters, inline runs, paragraphs, list items, tables, sections — depending on which nodes the range fully encloses. When the deletion crosses block boundaries and both surviving boundary blocks are paragraphs, mergeBlocks controls whether the surviving fragments fuse into one paragraph. Manages transaction automatically.

public Avalonia.Controls.Documents.TextModel.TextPointer DeleteText(bool mergeBlocks)

Parameters​

mergeBlocks bool

When true, surviving start/end paragraph fragments are merged into a single paragraph (replace-selection semantics — typing-over- selection, paste-over-selection). When false (the default), the surviving fragments are left as separate paragraphs (delete- selection semantics — Backspace, Delete, Cut). Silently a no-op when the deletion does not cross block boundaries, fully consumes one boundary block, or the surviving blocks are non-paragraph (Table, Section, List, etc.). Structural detach of fully-enclosed blocks is not gated by this flag.

Returns​

Avalonia.Controls.Documents.TextModel.TextPointer

A TextPointer at the deletion position.

DeleteTextAt Method​

Deletes text at the specified range relative to this range. Manages transaction automatically.

public Avalonia.Controls.Documents.TextModel.TextPointer DeleteTextAt(int relativeOffset, int length)

Parameters​

relativeOffset int

The starting offset relative to range start.

length int

The number of characters to delete.

Returns​

Avalonia.Controls.Documents.TextModel.TextPointer

A TextPointer at the deletion position.

GetPropertyValue Method​

public T GetPropertyValue<T>(Avalonia.StyledProperty<T> property)

Parameters​

property Avalonia.StyledProperty<T>

Type Parameters​

T

Returns​

T

GetText Method​

Returns the text content covered by this range.

public string GetText()

Returns​

string

Remarks​

Each call performs a rope slice and allocates a fresh string. Cache the result in a local for hot paths. To replace the text content, call Avalonia.Controls.Documents.TextModel.TextRange.ReplaceText(string).

InsertFootnote Method​

Replaces the range content with a footnote: the anchor (its number as ordinary text) lands at the range position, splitting a run when it sits mid-run, and the note body is created in the document's trailing footnote container with a fresh id - selection delete, anchor, body, and the anchor renumbering are one undo unit. Manages transaction automatically.

public Avalonia.Controls.Documents.Footnote InsertFootnote(Avalonia.Controls.Documents.RichFootnoteReference anchor)

Parameters​

anchor Avalonia.Controls.Documents.RichFootnoteReference

The anchor element to insert - create it to pre-configure formatting or to keep the reference; (the default) creates one. Must not currently belong to a document (move an attached anchor through the element collections instead - its note travels with it); a detached anchor that carries a note from an earlier removal is re-paired with it. The Avalonia.Controls.Documents.RichFootnoteReference.NoteId is assigned by the pairing.

Returns​

Avalonia.Controls.Documents.Footnote

The created Avalonia.Controls.Documents.Footnote (one empty paragraph, ready to receive the caret), or when the range start cannot host an anchor: inside a page band or another note (Word forbids both), or in a document without a Avalonia.Controls.Documents.FlowDocument root.

Exceptions​

InsertImage Method​

public Avalonia.Controls.Documents.TextModel.TextPointer InsertImage(ReadOnlyMemory<byte> data, string mimeType, double width, double height, string altText)

Parameters​

data ReadOnlyMemory<byte>

mimeType string

width double

height double

altText string

Returns​

Avalonia.Controls.Documents.TextModel.TextPointer

InsertPageNumberField Method​

Replaces the range content with a page-number field of kind: the field lands at the range position, splitting a run when it sits mid-run, and shows its cached result until a page layout resolves it (a band on a sheet renders the sheet's number) - selection delete and insertion are one undo unit. Manages transaction automatically.

public Avalonia.Controls.Documents.RichPageNumberField InsertPageNumberField(Avalonia.Controls.Documents.PageNumberFieldKind kind)

Parameters​

kind Avalonia.Controls.Documents.PageNumberFieldKind

The page number or the page count.

Returns​

Avalonia.Controls.Documents.RichPageNumberField

The inserted field, or when the range start cannot host one: inside a footnote body, which has no page of its own.

Exceptions​

InsertSnapshot Method​

Replaces the range content with the document tree captured by snapshot. Block, inline, and formatting structure from the snapshot is preserved — the snapshot's rope tree is spliced directly into the live document with no plain-text lowering.

public void InsertSnapshot(Avalonia.Controls.Documents.Serialization.Snapshot.DocumentSnapshot snapshot)

Parameters​

snapshot Avalonia.Controls.Documents.Serialization.Snapshot.DocumentSnapshot

The snapshot whose tree is grafted into this range.

Remarks​

Used by paste pipelines that already hold a Avalonia.Controls.Documents.Serialization.Snapshot.DocumentSnapshot (e.g. RTF clipboard payload) — avoids the stream + deserializer round-trip that Avalonia.Controls.Documents.TextModel.TextRange.CreateSnapshot plus a deserializer round-trip performs.

Insertion goes through Avalonia.Controls.Documents.TextModel.TextDocument.InsertSnapshotStructural(Avalonia.Controls.Documents.TextModel.TextPointer,Avalonia.Controls.Documents.Serialization.Snapshot.DocumentSnapshot), which splices the snapshot's rope zero-copy and grafts its block structure into the live tree under a structural change scope. Together with the preceding Avalonia.Controls.Documents.TextModel.TextDocument.DeleteText(int,int) for the existing range content, everything nests in the outer Avalonia.Controls.Documents.TextModel.TextDocument.BeginChange scope and collapses into one undo entry.

Exceptions​

InsertTextAt Method​

Inserts text at the specified offset relative to the range start. Manages transaction automatically.

public Avalonia.Controls.Documents.TextModel.TextPointer InsertTextAt(int relativeOffset, string text)

Parameters​

relativeOffset int

The offset relative to range start (0 = at start, Length = at end).

text string

The text to insert.

Returns​

Avalonia.Controls.Documents.TextModel.TextPointer

A TextPointer at the insertion position.

Load Method​

Replaces this range with the content of a stream.

public void Load(System.IO.Stream stream, Avalonia.Controls.Documents.Serialization.IDocumentSerializer serializer)

Parameters​

stream System.IO.Stream

The stream to read from.

serializer Avalonia.Controls.Documents.Serialization.IDocumentSerializer

The serializer for the format.

Remarks​

The decomposition is public and takes a cancellation token, which this does not: range.InsertSnapshot(serializer.Deserialize(stream, cancellationToken)).

Overlaps Method​

Checks if this range overlaps with another range.

public bool Overlaps(Avalonia.Controls.Documents.TextModel.TextRange other)

Parameters​

other Avalonia.Controls.Documents.TextModel.TextRange

The other range.

Returns​

bool

True if the ranges overlap.

ReplaceText overloads​

ReplaceText Method​

public Avalonia.Controls.Documents.TextModel.TextPointer ReplaceText(ReadOnlyMemory<char> newText)
Parameters​

newText ReadOnlyMemory<char>

Returns​

Avalonia.Controls.Documents.TextModel.TextPointer

ReplaceText Method​

Replaces the content of this range with new text. Manages transaction automatically.

public Avalonia.Controls.Documents.TextModel.TextPointer ReplaceText(string newText)
Parameters​

newText string

The replacement text.

Returns​

Avalonia.Controls.Documents.TextModel.TextPointer

A TextPointer after the replaced text.

ReplaceTextAt Method​

Replaces text at the specified range relative to this range. Manages transaction automatically.

public Avalonia.Controls.Documents.TextModel.TextPointer ReplaceTextAt(int relativeOffset, int length, string newText)

Parameters​

relativeOffset int

The starting offset relative to range start.

length int

The number of characters to replace.

newText string

The replacement text.

Returns​

Avalonia.Controls.Documents.TextModel.TextPointer

A TextPointer after the replaced text.

Save Method​

Writes this range to a stream.

public void Save(System.IO.Stream stream, Avalonia.Controls.Documents.Serialization.IDocumentSerializer serializer)

Parameters​

stream System.IO.Stream

The stream to write to.

serializer Avalonia.Controls.Documents.Serialization.IDocumentSerializer

The serializer for the format.

Remarks​

The decomposition is public and takes a cancellation token, which this does not: serializer.Serialize(range.CreateSnapshot()!, stream, cancellationToken). Going through the snapshot also lets one capture serve several formats.

SetImageAltText Method​

Sets the alternative text of an existing Avalonia.Controls.Documents.RichImage in a single undoable change.

public void SetImageAltText(Avalonia.Controls.Documents.RichImage image, string altText)

Parameters​

image Avalonia.Controls.Documents.RichImage

The image whose alt text to set. Must be attached to this range's document.

altText string

The alternative text, or null to clear it.

height

Display height in device-independent pixels, or double.NaN for intrinsic.

width

Display width in device-independent pixels, or double.NaN for intrinsic.

Remarks​

Write the image's own properties inside Avalonia.Controls.Documents.TextModel.TextDocument.BeginChange, which is what this does and takes any set of properties rather than these two.

Exceptions​

SetImageSize Method​

Sets the display size of an existing Avalonia.Controls.Documents.RichImage in a single undoable change. Pass double.NaN for a dimension to use the image's intrinsic pixel size.

public void SetImageSize(Avalonia.Controls.Documents.RichImage image, double width, double height)

Parameters​

image Avalonia.Controls.Documents.RichImage

The image whose size to set. Must be attached to this range's document.

width double

Display width in device-independent pixels, or double.NaN for intrinsic.

height double

Display height in device-independent pixels, or double.NaN for intrinsic.

Remarks​

Write the image's own properties inside Avalonia.Controls.Documents.TextModel.TextDocument.BeginChange, which is what this does and takes any set of properties rather than these two.

Exceptions​

ToString Method​

public string ToString()

Returns​

string

TryGetPropertyValue Method​

public bool TryGetPropertyValue<T>(Avalonia.StyledProperty<T> property, T& value)

Parameters​

property Avalonia.StyledProperty<T>

value T&

Type Parameters​

T

Returns​

bool

Properties​

NameDescription
EndGets the end position of the range.
IsEmptyGets a value indicating whether the range is empty. A range is empty only when Avalonia.Controls.Documents.TextModel.TextRange.Start and Avalonia.Controls.Documents.TextModel.TextRange.End refer to the exact same document position (node-aware). Two distinct positions that happen to share the same integer offset — for example pointers into consecutive empty paragraphs — are not considered empty.
LengthGets the length of the range in characters.
StartGets the start position of the range.
TextDocumentGets the document containing this range.

End Property​

Gets the end position of the range.

public Avalonia.Controls.Documents.TextModel.TextPointer End { get; set; }

IsEmpty Property​

Gets a value indicating whether the range is empty. A range is empty only when Avalonia.Controls.Documents.TextModel.TextRange.Start and Avalonia.Controls.Documents.TextModel.TextRange.End refer to the exact same document position (node-aware). Two distinct positions that happen to share the same integer offset — for example pointers into consecutive empty paragraphs — are not considered empty.

public bool IsEmpty { get; set; }

Length Property​

Gets the length of the range in characters.

public int Length { get; set; }

Start Property​

Gets the start position of the range.

public Avalonia.Controls.Documents.TextModel.TextPointer Start { get; set; }

TextDocument Property​

Gets the document containing this range.

public Avalonia.Controls.Documents.TextModel.TextDocument TextDocument { get; set; }

Events​

NameDescription
ChangedOccurs when the range changes.

Changed Event​

Occurs when the range changes.

public event EventHandler Changed