mapSelectionEnds function

TextSelectionSet mapSelectionEnds(
  1. TextSelectionSet selections, {
  2. required int mapEnd(
    1. int activeOffset,
    2. TextSelectionRange range
    ),
  3. required bool forward,
  4. bool extend = false,
})

Moves or extends every range's active edge through mapEnd.

When extend is false and a range is non-collapsed, the range collapses to its start (backward) or end (forward) without further mapping — the usual “arrow key dismisses the selection” rule. Extension retains the anchor and can cross it without losing direction.

Implementation

TextSelectionSet mapSelectionEnds(
  TextSelectionSet selections, {
  required int Function(int activeOffset, TextSelectionRange range) mapEnd,
  required bool forward,
  bool extend = false,
}) {
  return mapSelectionRanges(
    selections,
    transform: (range) {
      if (extend) {
        return TextSelectionRange.directional(
          anchorOffset: range.anchorOffset,
          activeOffset: mapEnd(range.activeOffset, range),
        );
      }
      if (!range.isCollapsed) {
        final offset = forward ? range.endOffset : range.startOffset;
        return TextSelectionRange(startOffset: offset, endOffset: offset);
      }
      final offset = mapEnd(range.activeOffset, range);
      return TextSelectionRange(startOffset: offset, endOffset: offset);
    },
  );
}