void (@NotNull String newName) { Document document = myEditor.getDocument(); CharSequence documentText = document.getImmutableCharSequence(); if (document.getModificationStamp() != myInitialModificationStamp || !isValid(documentText)) { return; } String result = getNewDocumentText(documentText, newName); WriteAction.run(() -> { int prevCount = (int)myOccurrences.stream() .filter(range -> range.getStartOffset() < myOccurrenceAtCaret.getStartOffset()) .count(); Caret caret = myEditor.getCaretModel().getPrimaryCaret(); int newCaretOffset = caret.getOffset() + (newName.length() - myOldName.length()) * prevCount; CommandProcessor.getInstance().executeCommand(myEditor.getProject(), () -> { document.setText(result); caret.moveToOffset(newCaretOffset); }, null, null, document); }); }