void (PsiStatement[] statements, @NotNull PsiElement anchor, @NotNull PsiLambdaExpression lambda) { Predicate<PsiVariable> allowedVar = variable -> PsiTreeUtil.isAncestor(lambda, variable, true); Project project = anchor.getProject(); JavaCodeStyleManager manager = JavaCodeStyleManager.getInstance(project); StreamEx.of(statements).select(PsiDeclarationStatement.class) .flatArray(PsiDeclarationStatement::getDeclaredElements) .select(PsiVariable.class) .forEach(e -> { PsiIdentifier identifier = e.getNameIdentifier(); if (identifier == null) return; String name = identifier.getText(); String newName = manager.suggestUniqueVariableName(name, anchor, allowedVar); if (!name.equals(newName)) { List<PsiReferenceExpression> refs = VariableAccessUtils.getVariableReferences(e, anchor); refs.forEach(ref -> ref.handleElementRename(newName)); identifier.replace(JavaPsiFacade.getElementFactory(project).createIdentifier(newName)); } }); }