getDecorations method

List<Decoration> getDecorations(
  1. String copiedText,
  2. List<DecoratorRule> rules
)

Return the list of decorations with tagged and untagged text

Implementation

List<Decoration> getDecorations(
  String copiedText,
  List<DecoratorRule> rules,
) {
  final List<RegExpMatch> tagsTemp = [];
  for (final rule in rules) {
    final regExp = rule.regExp;
    final localTags = regExp.allMatches(copiedText);
    tagsTemp.addAll(localTags);
  }

  final List<RegExpMatch> tags = [];
  for (final tag in tagsTemp) {
    if (tags.any((e) => tag.start <= e.start && tag.end >= e.end)) {
      tags.removeWhere((e) => tag.start <= e.start && tag.end >= e.end);
      tags.add(tag);
    } else if (!tags.any((e) => tag.start >= e.start && tag.end <= e.end)) {
      tags.add(tag);
    }
  }

  if (tags.isEmpty) {
    return [];
  }

  tags.sort((a, b) {
    return a.start < b.start ? -1 : 1;
  });

  final Set<RegExpMatch> toRemoveTags = {};
  for (final tagA in tags) {
    for (final tagB in tags) {
      if (tagA.start > tagB.start && tagA.end < tagB.end) {
        toRemoveTags.add(tagA);
      }
    }
  }
  for (final tag in toRemoveTags) {
    tags.remove(tag);
  }

  return _getSourceDecorations(tags, copiedText);
}