void (HighlighterIterator iterator, List<? extends TextRange> ranges) { int offset = 0; int index = 0; while (!iterator.atEnd() && index < ranges.size()) { TextRange range = ranges.get(index); if (range.getStartOffset() >= iterator.getEnd()) { iterator.advance(); continue; } if (range.getEndOffset() >= iterator.getStart()) { int relativeStart = Math.max(iterator.getStart() - range.getStartOffset(), 0); int relativeEnd = Math.min(iterator.getEnd() - range.getStartOffset(), range.getLength() + 1); addElement(new Element(offset + relativeStart, offset + relativeEnd, iterator.getTokenType(), iterator.getTextAttributes())); } if (range.getEndOffset() < iterator.getEnd()) { offset += range.getLength() + 1 + myAdditionalOffset; // myAdditionalOffset because of extra line - for shown separators int lastEnd = myPieces.isEmpty() ? -1 : myPieces.get(myPieces.size() - 1).getEnd(); addElement(new Element(Math.max(offset - 1 - myAdditionalOffset, lastEnd), offset, null, TextAttributes.ERASE_MARKER)); index++; continue; } iterator.advance(); } }