void (@NotNull Project project, @NotNull PsiMethodCallExpression call, @NotNull Context context) { PsiMethodCallExpression mapBefore = context.myMapBefore; CommentTracker ct = new CommentTracker(); StringBuilder sb = new StringBuilder(); PsiExpression qualifier = call.getMethodExpression().getQualifierExpression(); assert qualifier != null; sb.append(ct.text(qualifier)).append("."); if (mapBefore != null) { PsiExpression mapArgument = mapBefore.getArgumentList().getExpressions()[0]; sb.append("flatMap(").append(ct.text(mapArgument)).append(")."); } PsiExpression lambdaBodyAfter = context.myMapLambdaBodyAfter; if (lambdaBodyAfter != null) { sb.append("flatMap(").append(context.myOuterIfPresentVarName).append("->").append(ct.text(lambdaBodyAfter)).append(")."); } sb.append("ifPresent(").append(ct.text(context.myInnerIfPresentArgument)).append(")"); PsiElement result = ct.replaceAndRestoreComments(call, sb.toString()); LambdaCanBeMethodReferenceInspection.replaceAllLambdasWithMethodReferences(result); }