collectStyle method

Style collectStyle(
  1. int offset,
  2. int len
)

Returns style for specified text range.

Only attributes applied to all characters within this range are included in the result. Inline and line level attributes are handled separately, e.g.:

  • line attribute X is included in the result only if it exists for every line within this range (partially included lines are counted).
  • inline attribute X is included in the result only if it exists for every character within this range (line-break characters excluded).

In essence, it is INTERSECTION of each individual segment's styles

Implementation

Style collectStyle(int offset, int len) {
  final local = math.min(length - offset, len);
  var result = Style();
  final excluded = <Attribute>{};

  void _handle(Style style) {
    if (result.isEmpty) {
      excluded.addAll(style.values);
    } else {
      for (final attr in result.values) {
        if (!style.containsKey(attr.key)) {
          excluded.add(attr);
        }
      }
    }
    final remaining = style.removeAll(excluded);
    result = result.removeAll(excluded);
    result = result.mergeAll(remaining);
  }

  final data = queryChild(offset, true);
  var node = data.node as Leaf?;
  if (node != null) {
    result = result.mergeAll(node.style);
    var pos = node.length - data.offset;
    while (!node!.isLast && pos < local) {
      node = node.next as Leaf;
      _handle(node.style);
      pos += node.length;
    }
  }

  result = result.mergeAll(style);
  if (parent is Block) {
    final block = parent as Block;
    result = result.mergeAll(block.style);
  }

  final remaining = len - local;
  if (remaining > 0 && nextLine != null) {
    final rest = nextLine!.collectStyle(0, remaining);
    _handle(rest);
  }

  return result;
}