int (int offset, boolean preferLeftFragment, boolean skipEmptyShreds) { synchronized (myLock) { if (offset < myShreds.get(0).getPrefix().length()) { Segment hostRangeMarker = myShreds.get(0).getHostRangeMarker(); return hostRangeMarker == null ? 0 : hostRangeMarker.getStartOffset(); } int prevEnd = 0; //noinspection ForLoopReplaceableByForEach for (int i = 0; i < myShreds.size(); i++) { PsiLanguageInjectionHost.Shred shred = myShreds.get(i); Segment currentRange = shred.getHostRangeMarker(); if (currentRange == null) continue; int currentStart = currentRange.getStartOffset(); int currentEnd = currentRange.getEndOffset(); if (skipEmptyShreds && !preferLeftFragment && currentStart == currentEnd && shred.getPrefix().isEmpty() && shred.getSuffix().isEmpty()) { continue; } offset -= shred.getPrefix().length(); if (offset < 0) { return preferLeftFragment ? prevEnd : currentStart - 1; } if (offset == 0) { return currentStart; } int length = currentEnd - currentStart; if (offset < length || offset == length && preferLeftFragment) { return currentStart + offset; } offset -= length; offset -= shred.getSuffix().length(); prevEnd = currentEnd; } Segment hostRangeMarker = myShreds.get(myShreds.size() - 1).getHostRangeMarker(); return hostRangeMarker == null ? 0 : hostRangeMarker.getEndOffset(); } }