mergeTextSyntaxDecorationPatch function

List<TextDecorationRange> mergeTextSyntaxDecorationPatch(
  1. List<TextDecorationRange> previousDecorations,
  2. TextSyntaxDecorationPatch patch
)

Implementation

List<TextDecorationRange> mergeTextSyntaxDecorationPatch(
  List<TextDecorationRange> previousDecorations,
  TextSyntaxDecorationPatch patch,
) {
  final merged = <TextDecorationRange>[
    for (final decoration in previousDecorations) ...[
      if (decoration.endOffset <= patch.previousStartOffset)
        decoration
      else if (decoration.startOffset >= patch.previousEndOffset)
        TextDecorationRange(
          startOffset: decoration.startOffset + patch.offsetDelta,
          endOffset: decoration.endOffset + patch.offsetDelta,
          styleKey: decoration.styleKey,
        )
      else ...[
        if (decoration.startOffset < patch.previousStartOffset)
          TextDecorationRange(
            startOffset: decoration.startOffset,
            endOffset: patch.previousStartOffset,
            styleKey: decoration.styleKey,
          ),
        if (decoration.endOffset > patch.previousEndOffset)
          TextDecorationRange(
            startOffset: patch.nextEndOffset,
            endOffset: decoration.endOffset + patch.offsetDelta,
            styleKey: decoration.styleKey,
          ),
      ],
    ],
    ...patch.decorations,
  ]..sort((a, b) {
        final startComparison = a.startOffset.compareTo(b.startOffset);
        if (startComparison != 0) {
          return startComparison;
        }
        final endComparison = a.endOffset.compareTo(b.endOffset);
        if (endComparison != 0) {
          return endComparison;
        }
        return a.styleKey.compareTo(b.styleKey);
      });
  return List<TextDecorationRange>.unmodifiable(merged);
}