void (@NotNull Project project, @NotNull ProblemDescriptor descriptor) { final PsiElement element = descriptor.getPsiElement(); if (!(element instanceof PsiExpression)) { return; } final PsiExpression expression = PsiUtil.skipParenthesizedExprDown((PsiExpression)element); if (!(expression instanceof PsiReferenceExpression parameterReference)) { return; } final PsiElement target = parameterReference.resolve(); if (!(target instanceof PsiParameter parameter)) { return; } final PsiElement declarationScope = parameter.getDeclarationScope(); if (declarationScope instanceof PsiLambdaExpression) { CommonJavaRefactoringUtil.expandExpressionLambdaToCodeBlock((PsiLambdaExpression)declarationScope); } else if (declarationScope instanceof PsiForeachStatement) { final PsiStatement body = ((PsiForeachStatement)declarationScope).getBody(); if (body == null) { return; } BlockUtils.expandSingleStatementToBlockStatement(body); } final PsiElement body = BlockUtils.getBody(declarationScope); if (body == null) { return; } assert body instanceof PsiCodeBlock; final String parameterName = parameter.getName(); final PsiExpression rhs = parameterReference.isValid() ? getRightSideIfLeftSideOfSimpleAssignment(parameterReference, body) : null; final JavaCodeStyleManager javaCodeStyleManager = JavaCodeStyleManager.getInstance(project); final String variableName = javaCodeStyleManager.suggestUniqueVariableName(parameterName, body, true); final CommentTracker tracker = new CommentTracker(); final String initializerText = (rhs == null) ? parameterName : tracker.text(rhs); PsiType type = parameter.getType(); if (type instanceof PsiEllipsisType) { type = ((PsiEllipsisType)type).toArrayType(); } if (type instanceof PsiLambdaParameterType) { return; } PsiDeclarationStatement newStatement = (PsiDeclarationStatement) JavaPsiFacade.getElementFactory(project).createStatementFromText( type.getCanonicalText() + ' ' + variableName + '=' + initializerText + ';', body); final PsiCodeBlock codeBlock = (PsiCodeBlock)body; List<PsiReferenceExpression> refs = new ArrayList<>(); PsiStatement anchor = null; for (PsiStatement statement : codeBlock.getStatements()) { if (anchor == null) { if (rhs == null && !JavaHighlightUtil.isSuperOrThisCall(statement, true, true)) { anchor = statement; SyntaxTraverser.psiTraverser(statement).filter(PsiReferenceExpression.class) .filter(ref -> ref.isReferenceTo(parameter)).addAllTo(refs); } else if (statement.getTextRange().contains(parameterReference.getTextRange())) { anchor = statement; } } else { SyntaxTraverser.psiTraverser(statement).filter(PsiReferenceExpression.class) .filter(ref -> ref.isReferenceTo(parameter)).addAllTo(refs); } } assert anchor != null; newStatement = (PsiDeclarationStatement)(rhs == null ? codeBlock.addBefore(newStatement, anchor) : tracker.replaceAndRestoreComments(anchor, newStatement)); replaceReferences(refs, variableName, body); if (isOnTheFly()) { final PsiLocalVariable variable = (PsiLocalVariable)newStatement.getDeclaredElements()[0]; final PsiReference[] references = ReferencesSearch.search(variable, variable.getUseScope()).toArray(PsiReference.EMPTY_ARRAY); HighlightUtils.showRenameTemplate(body, variable, references); } }