void (@NotNull Project project, @NotNull PsiElement element, @NotNull ModPsiUpdater updater) { if (!(element instanceof PsiJavaCodeReferenceElement referenceElement)) { return; } final PsiElement target = referenceElement.resolve(); if (!(target instanceof PsiClass innerClass)) { return; } final PsiClass containingClass = innerClass.getContainingClass(); if (containingClass == null) { return; } final String qualifiedName = containingClass.getQualifiedName(); if (qualifiedName == null) { return; } final PsiFile containingFile = referenceElement.getContainingFile(); if (!(containingFile instanceof PsiJavaFile javaFile)) { return; } final String innerClassName = innerClass.getQualifiedName(); if (innerClassName == null) { return; } final PsiImportList importList = javaFile.getImportList(); if (importList == null) { return; } final PsiImportStatement[] importStatements = importList.getImportStatements(); final int importStatementsLength = importStatements.length; boolean onDemand = false; PsiImportStatement referenceImportStatement = null; for (int i = importStatementsLength - 1; i >= 0; i--) { final PsiImportStatement importStatement = importStatements[i]; final String importString = importStatement.getQualifiedName(); if (importStatement.isOnDemand()) { if (qualifiedName.equals(importString)) { referenceImportStatement = importStatement; onDemand = true; break; } } else { if (innerClassName.equals(importString)) { referenceImportStatement = importStatement; break; } } } final ReferenceCollector referenceCollector = new ReferenceCollector(onDemand ? qualifiedName : innerClassName, onDemand); javaFile.accept(referenceCollector); final Collection<PsiJavaCodeReferenceElement> references = referenceCollector.getReferences(); final SmartPointerManager pointerManager = SmartPointerManager.getInstance(project); final List<SmartPsiElementPointer<PsiJavaCodeReferenceElement>> pointers = new ArrayList<>(); for (PsiJavaCodeReferenceElement reference : references) { final SmartPsiElementPointer<PsiJavaCodeReferenceElement> pointer = pointerManager.createSmartPsiElementPointer(reference); pointers.add(pointer); } if (referenceImportStatement != null) { referenceImportStatement.delete(); } final PsiClass outerClass = ClassUtils.getOutermostContainingClass(containingClass); ImportUtils.addImportIfNeeded(outerClass, referenceElement); final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project); final Document document = containingFile.getViewProvider().getDocument(); if (document == null) { return; } documentManager.doPostponedOperationsAndUnblockDocument(document); final String text = buildNewText(javaFile, references, containingClass, new StringBuilder()).toString(); document.replaceString(0, document.getTextLength(), text); documentManager.commitDocument(document); if (pointers.size() > 1) { for (var pointer : pointers) { PsiElement psiElement = pointer.getElement(); if (psiElement != null) { updater.highlight(element); } } } }