List<TextRange> () { List<TextRange> enabledRanges = new ArrayList<>(); myTagInfoList.sort(Comparator.comparingInt(info -> info.offset)); int start = myInitialRange.getStartOffset(); boolean formatterEnabled = true; for (FormatterTagInfo tagInfo: myTagInfoList) { if (tagInfo.tag == FormatterTag.OFF && formatterEnabled) { if (tagInfo.offset > start) { TextRange range = new TextRange(start, tagInfo.offset); enabledRanges.add(range); } formatterEnabled = false; } else if (tagInfo.tag == FormatterTag.ON && !formatterEnabled) { start = Math.max(tagInfo.offset, myInitialRange.getStartOffset()); if (start >= myInitialRange.getEndOffset()) break; formatterEnabled = true; } } if (start < myInitialRange.getEndOffset() && formatterEnabled) { enabledRanges.add(new TextRange(start, myInitialRange.getEndOffset())); } return enabledRanges; }