getStartingIndex method

int getStartingIndex(
  1. {required String text,
  2. required int index}
)

Returns the starting index from which the search term should be chopped from, given a clicked index and full text. For a space-delimited language, this will return the starting index of a clicked word. Otherwise, this returns the clicked index itself.

Implementation

int getStartingIndex({
  required String text,
  required int index,
}) {
  if (isSpaceDelimited) {
    final workingBuffer = StringBuffer();

    List<String> words = textToWords(text.replaceAll('\n', ' '));

    for (String word in words) {
      workingBuffer.write(word);
      if (workingBuffer.length > index) {
        return workingBuffer.length - word.length;
      }
    }

    return index;
  } else {
    return index;
  }
}