int (DocumentWindow window, int hostOffset) { Place shreds = ((DocumentWindowImpl)window).getShreds(); Segment hostRangeMarker = shreds.get(0).getHostRangeMarker(); if (hostRangeMarker == null || hostOffset < hostRangeMarker.getStartOffset()) { return shreds.get(0).getPrefix().length(); } StringBuilder chars = new StringBuilder(); int unescaped = 0; for (int i = 0; i < shreds.size(); i++, chars.setLength(0)) { PsiLanguageInjectionHost.Shred shred = shreds.get(i); int prefixLength = shred.getPrefix().length(); int suffixLength = shred.getSuffix().length(); PsiLanguageInjectionHost host = shred.getHost(); TextRange rangeInsideHost = shred.getRangeInsideHost(); LiteralTextEscaper<? extends PsiLanguageInjectionHost> escaper = host == null ? null : host.createLiteralTextEscaper(); unescaped += prefixLength; Segment currentRange = shred.getHostRangeMarker(); if (currentRange == null) continue; Segment nextRange = i == shreds.size() - 1 ? null : shreds.get(i + 1).getHostRangeMarker(); if (nextRange == null || hostOffset < nextRange.getStartOffset()) { hostOffset = Math.min(hostOffset, currentRange.getEndOffset()); int inHost = hostOffset - currentRange.getStartOffset(); if (escaper != null && escaper.decode(rangeInsideHost, chars)) { int found = ObjectUtils.binarySearch( 0, inHost, index -> Integer.compare(escaper.getOffsetInHost(index, TextRange.create(0, host.getTextLength())), inHost)); return unescaped + (found >= 0 ? found : -found - 1); } return unescaped + inHost; } if (escaper != null && escaper.decode(rangeInsideHost, chars)) { unescaped += chars.length(); } else { unescaped += currentRange.getEndOffset() - currentRange.getStartOffset(); } unescaped += suffixLength; } return unescaped - shreds.get(shreds.size() - 1).getSuffix().length(); }