private void init(@NotNull HighlighterIterator it1,
                    @NotNull HighlighterIterator it2,
                    @NotNull List<? extends HighlightRange> ranges,
                    int textLength) {
    ApplicationManager.getApplication().assertReadAccessAllowed();

    int i = 0;
    int offset = 0;

    for (HighlightRange range : ranges) {
      TextRange base = range.getBase();
      TextRange changed = range.getChanged();

      if (base.isEmpty()) continue;

      if (base.getStartOffset() > offset) {
        addElement(createEmptyElement(offset, base.getStartOffset()));
        offset = base.getStartOffset();
      }

      HighlighterIterator it = range.getSide().select(it1, it2);
      while (!it.atEnd() && changed.getStartOffset() >= it.getEnd()) {
        if (i++ % 1024 == 0) ProgressManager.checkCanceled();
        it.advance();
      }

      if (it.atEnd()) {
        LOG.error("Unexpected end of highlighter");
        break;
      }

      if (changed.getEndOffset() <= it.getStart()) {
        continue;
      }

      while (true) {
        int relativeStart = Math.max(it.getStart() - changed.getStartOffset(), 0);
        int relativeEnd = Math.min(it.getEnd() - changed.getStartOffset(), changed.getLength() + 1);

        addElement(new Element(offset + relativeStart,
                               offset + relativeEnd,
                               it.getTokenType(),
                               it.getTextAttributes(),
                               it.getTextAttributesKeys()));

        if (changed.getEndOffset() <= it.getEnd()) {
          offset += changed.getLength();
          break;
        }

        if (i++ % 1024 == 0) ProgressManager.checkCanceled();
        it.advance();
        if (it.atEnd()) {
          LOG.error("Unexpected end of highlighter");
          break;
        }
      }
    }

    if (offset < textLength) {
      addElement(createEmptyElement(offset, textLength));
    }
  }
private void init(@NotNull HighlighterIterator it1,
                    @NotNull HighlighterIterator it2,
                    @NotNull List<? extends HighlightRange> ranges,
                    int textLength)