void (@NotNull ActionContext context, @NotNull PsiMethodCallExpression call, @NotNull ModPsiUpdater updater) { PsiElement outer = call.getParent().getParent(); if(!(outer instanceof PsiMethodCallExpression outerCall)) return; PsiMethod outerMethod = outerCall.resolveMethod(); if(outerMethod == null) return; PsiClass outerClass = outerMethod.getContainingClass(); if(outerClass == null) return; String outerClassName = outerClass.getQualifiedName(); PsiExpression qualifier = call.getMethodExpression().getQualifierExpression(); PsiExpression outerQualifier = outerCall.getMethodExpression().getQualifierExpression(); CommentTracker ct = new CommentTracker(); String reference; if(outerMethod.getName().equals("apply") && CommonClassNames.JAVA_UTIL_FUNCTION_FUNCTION.equals(outerClassName)) { reference = outerQualifier == null ? "this" : ct.text(outerQualifier); } else if(outerMethod.hasModifierProperty(PsiModifier.STATIC)) { reference = outerClassName + "::" + outerMethod.getName(); } else { reference = outerQualifier == null ? "this" : ct.text(outerQualifier)+"::"+outerMethod.getName(); } String resultQualifier = qualifier != null ? ct.text(qualifier) + "." : ""; String replacement = resultQualifier + "andThen(" + reference + ").apply" + ct.text(call.getArgumentList()); PsiElement result = ct.replaceAndRestoreComments(outer, replacement); result = CodeStyleManager.getInstance(context.project()).reformat(result); PsiElement applyElement = ((PsiMethodCallExpression)result).getMethodExpression().getReferenceNameElement(); if(applyElement != null) { updater.moveTo(applyElement.getTextOffset() + applyElement.getTextLength()); } }