void (@NotNull ActionContext context, @NotNull PsiIdentifier identifier, @NotNull ModPsiUpdater updater) { final PsiMethodCallExpression filterCall = PsiTreeUtil.getParentOfType(identifier, PsiMethodCallExpression.class); LOG.assertTrue(filterCall != null); final PsiMethodCallExpression filterToMerge = getFilterToMerge(filterCall); LOG.assertTrue(filterToMerge != null); final PsiMethodCallExpression callToStay = filterCall.getTextLength() < filterToMerge.getTextLength() ? filterCall : filterToMerge; final PsiMethodCallExpression callToEliminate = callToStay == filterCall ? filterToMerge : filterCall; String resultingOperation = callToEliminate.getMethodExpression().getReferenceName(); LOG.assertTrue(resultingOperation != null); final PsiLambdaExpression targetLambda = getLambda(callToStay); LOG.assertTrue(targetLambda != null, callToStay); final PsiParameter[] parameters = targetLambda.getParameterList().getParameters(); final String name = parameters.length > 0 ? parameters[0].getName() : null; final PsiLambdaExpression sourceLambda = getLambda(callToEliminate); LOG.assertTrue(sourceLambda != null, callToEliminate); if (name != null) { final PsiParameter[] sourceLambdaParams = sourceLambda.getParameterList().getParameters(); if (sourceLambdaParams.length > 0 && !name.equals(sourceLambdaParams[0].getName())) { for (PsiReference reference : ReferencesSearch.search(sourceLambdaParams[0]).findAll()) { final PsiElement referenceElement = reference.getElement(); if (referenceElement instanceof PsiReferenceExpression) { ExpressionUtils.bindReferenceTo((PsiReferenceExpression)referenceElement, name); } } } } PsiElementFactory factory = JavaPsiFacade.getElementFactory(context.project()); PsiElement nameElement = callToStay.getMethodExpression().getReferenceNameElement(); LOG.assertTrue(nameElement != null); if (!resultingOperation.equals(nameElement.getText())) { nameElement.replace(factory.createIdentifier(resultingOperation)); } PsiExpression targetBody = LambdaUtil.extractSingleExpressionFromBody(targetLambda.getBody()); LOG.assertTrue(targetBody != null); final PsiExpression sourceLambdaBody = LambdaUtil.extractSingleExpressionFromBody(sourceLambda.getBody()); LOG.assertTrue(sourceLambdaBody != null); String newFilter = ParenthesesUtils.getText(targetBody, ParenthesesUtils.OR_PRECEDENCE) + " && " + ParenthesesUtils.getText(sourceLambdaBody, ParenthesesUtils.OR_PRECEDENCE); final PsiExpression compoundExpression = factory.createExpressionFromText(newFilter, sourceLambda); targetBody = (PsiExpression)targetBody.replace(compoundExpression); CodeStyleManager.getInstance(context.project()).reformat(targetBody); final PsiExpression qualifierExpression = callToEliminate.getMethodExpression().getQualifierExpression(); LOG.assertTrue(qualifierExpression != null, callToEliminate); final Collection<PsiComment> comments = PsiTreeUtil.findChildrenOfType(callToEliminate, PsiComment.class); for (PsiComment comment : comments) { final TextRange commentRange = comment.getTextRange(); if (!sourceLambdaBody.getTextRange().contains(commentRange) && !qualifierExpression.getTextRange().contains(commentRange)) { targetBody.add(comment); } } callToEliminate.replace(qualifierExpression); }