getWordBoundary method Null safety

  1. @override
TextRange getWordBoundary(
  1. TextPosition position

Returns the text range of the word at the given offset. Characters not part of a word, such as spaces, symbols, and punctuation, have word breaks on both sides. In such cases, this method will return a text range that contains the given text position.

Word boundaries are defined more precisely in Unicode Standard Annex #29

The position parameter must be relative to the node's content.

Valid only after layout.


TextRange getWordBoundary(TextPosition position) {
  final child = childAtPosition(position);
  final nodeOffset = child.getContainer().offset;
  final childWord = child
      .getWordBoundary(TextPosition(offset: position.offset - nodeOffset));
  return TextRange(
    start: childWord.start + nodeOffset,
    end: childWord.end + nodeOffset,