FragmentOperations class

Utility functions for fragment operations.

Constructors

FragmentOperations()

Properties

hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited

Static Methods

adjustIndex(String s, int index) int
Adjusts an index to avoid cutting through a grapheme cluster (e.g., emoji surrogate pairs, CJK variation selectors, combining marks). If the index falls inside a multi-code-unit grapheme cluster, moves it back to the start of that cluster.
cloneFragment(Fragment source, {String? text}) Fragment
Creates a new fragment with the same styles as the source fragment. If text is provided, uses that text; otherwise uses the source fragment's text.
collectLeafFragments(FNode node) List<Fragment>
Recursively collects all leaf fragments in order. A leaf fragment is a Fragment that is not an InlineContainerNode.
createFragmentWithPendingStyles(FluentDocument document, String text) Fragment
Creates a new fragment with styles from the document's pending styles. Used when inserting new text at the cursor position.
deleteTextInFragment(Fragment fragment, int offset, {int count = 1}) bool
Deletes count characters from fragment starting from offset. Returns false if the range is invalid. Optimised edge cases avoid temp substring allocations.
getGraphemeLengthAt(String s, int offset) int
Calculates the number of UTF-16 code units to delete starting from offset to delete one complete grapheme cluster. Returns 1 for regular characters, 2 for surrogate pairs (emoji), and potentially more for complex clusters (e.g., CJK + variation selector, ZWJ sequences).
getPreviousGraphemeOffset(String s, int currentOffset) int
Calculates the previous grapheme cluster offset in a string. Returns the start offset of the grapheme cluster that ends at currentOffset, handling surrogate pairs, CJK variation selectors, combining marks, and ZWJ sequences correctly.
insertTextInFragment(Fragment fragment, int offset, String text) bool
Inserts text in fragment at offset. Returns false if offset is out of range. Optimised edge cases (prepend / append) avoid temp substring allocations.
mergeFragments(Fragment fragment, Fragment next) → void
Merges next into fragment (concatenates the text, then next must be removed from the container by the caller).
safeSubstring(String s, int start, [int? end]) String
Safely extracts a substring, adjusting indices to avoid cutting through UTF-16 surrogate pairs (e.g., emoji). If end is not provided, extracts from start to the end of the string.
splitFragment(Fragment fragment, int offset) → ({Fragment left, Fragment right})
Splits fragment at offset: returns (left, right). The original fragment is mutated (becomes the left part). The right part is a new Fragment with the same style (cloneFragment).