format method

Delta format (
  1. int index,
  2. int length,
  3. NotusAttribute attribute
)

Formats segment of this document with specified attribute.

Applies heuristic rules before modifying this document and produces a NotusChange with source set to ChangeSource.local.

Returns an instance of Delta actually composed into this document. The returned Delta may be empty in which case this document remains unchanged and no NotusChange is published to changes stream.

Implementation

Delta format(int index, int length, NotusAttribute attribute) {
  assert(index >= 0 && length >= 0 && attribute != null);

  var change = Delta();

  if (attribute is EmbedAttribute && length > 0) {
    // Must delete selected length of text before applying embed attribute
    // since inserting an embed in non-empty selection is essentially a
    // replace operation.
    change = delete(index, length);
    length = 0;
  }

  final formatChange =
      _heuristics.applyFormatRules(this, index, length, attribute);
  if (formatChange.isNotEmpty) {
    compose(formatChange, ChangeSource.local);
    change = change.compose(formatChange);
  }

  return change;
}