void (@NotNull Project project, NonCodeUsageInfo @NotNull [] usages) { Map<Document, Int2ObjectMap<UsageOffset>> docsToOffsetsMap = CollectionFactory.createSmallMemoryFootprintMap(); final PsiDocumentManager psiDocumentManager = PsiDocumentManager.getInstance(project); for (NonCodeUsageInfo usage : usages) { PsiElement element = usage.getElement(); if (element == null) continue; element = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(element, true); if (element == null) continue; final ProperTextRange rangeInElement = usage.getRangeInElement(); if (rangeInElement == null) continue; final PsiFile containingFile = element.getContainingFile(); Document document = containingFile.getViewProvider().getDocument(); if (document != null) { psiDocumentManager.commitDocument(document); } final Segment segment = usage.getSegment(); LOG.assertTrue(segment != null); TextRange replaceRange = TextRange.create(segment); // re-map usages to upper host from injected document to avoid duplicated replacements while (document instanceof DocumentWindow documentWindow) { replaceRange = documentWindow.injectedToHost(replaceRange); document = documentWindow.getDelegate(); } int fileOffset = replaceRange.getStartOffset(); Int2ObjectMap<UsageOffset> offsetMap = docsToOffsetsMap.get(document); if (offsetMap == null) { offsetMap = new Int2ObjectOpenHashMap<>(); docsToOffsetsMap.put(document, offsetMap); } final UsageOffset substitution = new UsageOffset(fileOffset, fileOffset + rangeInElement.getLength(), usage.newText); final UsageOffset duplicate = offsetMap.get(fileOffset); if (duplicate != null) { if (!duplicate.equals(substitution)) { LOG.warn("ATTENTION! Unequal renaming in the same place in the document, possibly due to injection (read more in CPP-17316):\n" + " document: " + document + "\n" + " element: " + element + "\n" + " first rename: " + substitution.newText + "(" + substitution.startOffset + ", " + substitution.endOffset + ")\n" + " second rename: " + duplicate.newText + "(" + duplicate.startOffset + ", " + duplicate.endOffset + ")"); } } else { offsetMap.put(fileOffset, substitution); } } for (Document document : docsToOffsetsMap.keySet()) { Map<Integer, UsageOffset> offsetMap = docsToOffsetsMap.get(document); LOG.assertTrue(offsetMap != null, document); UsageOffset[] offsets = offsetMap.values().toArray(new UsageOffset[0]); Arrays.sort(offsets); for (int i = offsets.length - 1; i >= 0; i--) { UsageOffset usageOffset = offsets[i]; document.replaceString(usageOffset.startOffset, usageOffset.endOffset, usageOffset.newText); } psiDocumentManager.commitDocument(document); } }