mergeResults static method

List<SuggestionSpan> mergeResults(
  1. List<SuggestionSpan> oldResults,
  2. List<SuggestionSpan> newResults
)

Merges two lists of spell check SuggestionSpans.

Used in cases where the text has not changed, but the spell check results received from the shell side have. This case is caused by IMEs (GBoard, for instance) that ignore the composing region when spell checking text.

Assumes that the lists provided as parameters are sorted by range start and that both list of SuggestionSpans apply to the same text.

Implementation

static List<SuggestionSpan> mergeResults(
    List<SuggestionSpan> oldResults, List<SuggestionSpan> newResults) {
  final List<SuggestionSpan> mergedResults = <SuggestionSpan>[];

  SuggestionSpan oldSpan;
  SuggestionSpan newSpan;
  int oldSpanPointer = 0;
  int newSpanPointer = 0;

  while (oldSpanPointer < oldResults.length &&
      newSpanPointer < newResults.length) {
    oldSpan = oldResults[oldSpanPointer];
    newSpan = newResults[newSpanPointer];

    if (oldSpan.range.start == newSpan.range.start) {
      mergedResults.add(oldSpan);
      oldSpanPointer++;
      newSpanPointer++;
    } else {
      if (oldSpan.range.start < newSpan.range.start) {
        mergedResults.add(oldSpan);
        oldSpanPointer++;
      } else {
        mergedResults.add(newSpan);
        newSpanPointer++;
      }
    }
  }

  mergedResults.addAll(oldResults.sublist(oldSpanPointer));
  mergedResults.addAll(newResults.sublist(newSpanPointer));

  return mergedResults;
}