OffsetsInFile (OffsetsInFile offsetMap, int startOffset, int endOffset, String replacement) { offsetMap.getOffsets().addOffset(START_OFFSET, startOffset); offsetMap.getOffsets().addOffset(END_OFFSET, endOffset); Document document = offsetMap.getFile().getViewProvider().getDocument(); assert document != null; if (replacement.isEmpty() && startOffset == endOffset && PsiDocumentManager.getInstance(offsetMap.getFile().getProject()).isCommitted(document)) { return offsetMap; } OffsetsInFile hostOffsets = offsetMap.toTopLevelFile(); OffsetsInFile hostCopy = hostOffsets.copyWithReplacement(getStartOffset(hostOffsets), getEndOffset(hostOffsets), replacement); return hostCopy.toInjectedIfAny(getStartOffset(hostCopy)); }