@Override public void invoke(@NotNull Project project, Editor editor, @NotNull PsiElement element) throws IncorrectOperationException { try { if (!FileModificationService.getInstance().preparePsiElementForWrite(element)) return; final PsiJavaToken token = (PsiJavaToken) element; final PsiPolyadicExpression expression = SplitConditionUtil.findCondition(element, true, false); final PsiLambdaExpression lambdaExpression = PsiTreeUtil.getParentOfType(expression, PsiLambdaExpression.class); LOG.assertTrue(lambdaExpression != null); final String lambdaParameterName = lambdaExpression.getParameterList().getParameters()[0].getName(); final PsiMethodCallExpression methodCallExpression = PsiTreeUtil.getParentOfType(expression, PsiMethodCallExpression.class); LOG.assertTrue(methodCallExpression != null, expression); PsiExpression lOperand = getLOperands(expression, token); PsiExpression rOperand = getROperands(expression, token); final Collection<PsiComment> comments = PsiTreeUtil.findChildrenOfType(expression, PsiComment.class); final PsiMethodCallExpression chainedCall = (PsiMethodCallExpression) JavaPsiFacade.getElementFactory(project) .createExpressionFromText( "a.filter(" + lambdaParameterName + " -> x)", expression); final PsiExpression argExpression = chainedCall.getArgumentList().getExpressions()[0]; final PsiElement rReplaced = ((PsiLambdaExpression) argExpression).getBody().replace(rOperand); final PsiExpression compoundArg = methodCallExpression.getArgumentList().getExpressions()[0]; final int separatorOffset = token.getTextOffset(); for (PsiComment comment : comments) { if (comment.getTextOffset() < separatorOffset) { compoundArg.getParent().add(comment); } else { rReplaced.getParent().add(comment); } } ((PsiLambdaExpression) compoundArg).getBody().replace(lOperand); chainedCall.getMethodExpression().getQualifierExpression().replace(methodCallExpression); methodCallExpression.replace(chainedCall); } catch (IncorrectOperationException e) { LOG.error(e); } }
@Override public boolean isAvailable(@NotNull Project project, Editor editor, @NotNull PsiElement element) { final PsiPolyadicExpression expression = SplitConditionUtil.findCondition(element, true, false); if (expression == null || expression.getOperands().length < 2) return false; PsiElement parent = PsiUtil.skipParenthesizedExprUp(expression.getParent()); if (!(parent instanceof PsiLambdaExpression)) return false; if (((PsiLambdaExpression) parent).getParameterList().getParametersCount() != 1) return false; parent = parent.getParent(); if (!(parent instanceof PsiExpressionList)) return false; final PsiElement gParent = parent.getParent(); if (!(gParent instanceof PsiMethodCallExpression)) return false; if (MergeFilterChainAction.isFilterCall((PsiMethodCallExpression) gParent)) { return true; } return false; }