StringBuilder (int startOffset, int endOffset) { boolean firstTimer = false; PlaceInfo currentPlace = myPlaceInfos.get(shredNo); if (hostText == null) { hostText = currentPlace.myHostText; rangeInHost = currentPlace.getRelevantRangeInsideHost(); currentTextEscaper = currentPlace.myEscaper; firstTimer = true; } StringBuilder text = tempLeafBuffer; text.setLength(0); while (startOffset < endOffset) { TextRange shredRange = currentPlace.rangeInDecodedPSI; String prefix = currentPlace.prefix; if (startOffset >= shredRange.getEndOffset()) { currentPlace = myPlaceInfos.get(++shredNo); hostText = currentPlace.myHostText; currentTextEscaper = currentPlace.myEscaper; rangeInHost = currentPlace.getRelevantRangeInsideHost(); firstTimer = true; continue; } assert startOffset >= shredRange.getStartOffset(); if (startOffset - shredRange.getStartOffset() < prefix.length()) { // inside prefix TextRange rangeInPrefix = new TextRange(startOffset - shredRange.getStartOffset(), Math.min(prefix.length(), endOffset - shredRange.getStartOffset())); text.append(prefix, rangeInPrefix.getStartOffset(), rangeInPrefix.getEndOffset()); startOffset += rangeInPrefix.getLength(); continue; } String suffix = currentPlace.suffix; if (startOffset < shredRange.getEndOffset() - suffix.length()) { // inside host body, cut out from the host text int startOffsetInHost = currentTextEscaper.getOffsetInHost(startOffset - shredRange.getStartOffset() - prefix.length(), rangeInHost); int endOffsetCut = Math.min(endOffset, shredRange.getEndOffset() - suffix.length()); int endOffsetInHost = currentTextEscaper.getOffsetInHost(endOffsetCut - shredRange.getStartOffset() - prefix.length(), rangeInHost); if (endOffsetInHost != -1) { if (firstTimer) text.append(hostText, rangeInHost.getStartOffset(), startOffsetInHost); text.append(hostText, startOffsetInHost, endOffsetInHost); startOffset = endOffsetCut; // todo what about lastTimer? } else { LOG.error("Text escaper " + currentTextEscaper +" ("+currentTextEscaper.getClass()+") " + "returned -1 in 'getOffsetInHost("+(endOffsetCut - shredRange.getStartOffset() - prefix.length())+", new TextRange"+rangeInHost+")' " + "for "+currentPlace.host.getClass(), new Attachment("host", StringUtil.first(currentPlace.host.getText(), 100, true))); } continue; } // inside suffix TextRange rangeInSuffix = new TextRange(suffix.length() - shredRange.getEndOffset() + startOffset, Math.min(suffix.length(), endOffset + suffix.length() - shredRange.getEndOffset())); text.append(suffix, rangeInSuffix.getStartOffset(), rangeInSuffix.getEndOffset()); startOffset += rangeInSuffix.getLength(); } return text; }