String (@NotNull PsiElementFactory factory, @NotNull CommentTracker ct, PsiReferenceExpression optionalRef, PsiElement trueValue) { PsiType type = optionalRef.getType(); String paramName = new VariableNameGenerator(trueValue, VariableKind.PARAMETER) .byType(OptionalUtil.getOptionalElementType(type)).byName("value").generate(true); if(trueValue instanceof PsiExpressionStatement) { trueValue = ((PsiExpressionStatement)trueValue).getExpression(); } ct.markUnchanged(trueValue); PsiElement copy = trueValue.copy(); for (PsiElement getCall : PsiTreeUtil.collectElements(copy, e -> isOptionalGetCall(e, optionalRef))) { PsiElement result = getCall.replace(factory.createIdentifier(paramName)); if (copy == getCall) copy = result; } if(copy instanceof PsiStatement && !(copy instanceof PsiBlockStatement)) { return paramName + "->{" + copy.getText()+"}"; } return paramName + "->" + copy.getText(); }