void (@NotNull Project project, @NotNull PsiElement element, @NotNull ModPsiUpdater updater) { final PsiReferenceExpression referenceExpression = (PsiReferenceExpression)element; final PsiLocalVariable variable = (PsiLocalVariable)referenceExpression.resolve(); if (variable == null) return; final PsiAssignmentExpression assignment = (PsiAssignmentExpression)PsiUtil.skipParenthesizedExprUp(referenceExpression.getParent()); if (assignment == null) return; PsiExpressionStatement assignmentStatement = (PsiExpressionStatement)assignment.getParent(); final PsiExpression lExpression = PsiUtil.skipParenthesizedExprDown(assignment.getLExpression()); if (lExpression == null) return; final String originalVariableName = lExpression.getText(); final PsiType type = variable.getType(); final PsiExpression rhs = assignment.getRExpression(); final PsiCodeBlock variableBlock = Objects.requireNonNull(PsiTreeUtil.getParentOfType(variable, PsiCodeBlock.class)); List<String> names = new VariableNameGenerator(variableBlock, VariableKind.LOCAL_VARIABLE) .byExpression(rhs).byType(type).byName(originalVariableName).generateAll(true); final String newVariableName = names.get(0); final SearchScope scope = new LocalSearchScope(assignmentStatement.getParent()); final Query<PsiReference> query = ReferencesSearch.search(variable, scope, false); final JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(project); final PsiElementFactory factory = psiFacade.getElementFactory(); for (PsiReference reference : query) { final PsiElement referenceElement = reference.getElement(); final TextRange textRange = assignmentStatement.getTextRange(); if (referenceElement.getTextOffset() <= textRange.getEndOffset()) { continue; } final PsiExpression newExpression = factory.createExpressionFromText(newVariableName, referenceElement); referenceElement.replace(newExpression); } CommentTracker commentTracker = new CommentTracker(); final String rhsText = rhs == null ? "" : commentTracker.text(rhs); @NonNls final String newStatementText = type.getCanonicalText() + ' ' + newVariableName + " = " + rhsText + ';'; final PsiDeclarationStatement declarationStatement = (PsiDeclarationStatement)BlockUtils.addAfter(assignmentStatement, factory.createStatementFromText(newStatementText, assignmentStatement)); assignmentStatement = PsiTreeUtil.getPrevSiblingOfType(declarationStatement, PsiExpressionStatement.class); commentTracker.deleteAndRestoreComments(Objects.requireNonNull(assignmentStatement)); final PsiElement[] elements = declarationStatement.getDeclaredElements(); final PsiLocalVariable newVariable = (PsiLocalVariable)elements[0]; updater.rename(newVariable, names); }