@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 void visitLambdaExpression(PsiLambdaExpression lambdaExpression) {
   super.visitLambdaExpression(lambdaExpression);
   if (lambdaExpression.getBody() instanceof PsiExpression) {
     registerError(lambdaExpression);
   }
 }
Пример #3
0
 private void registerNestedClosures(
     @NotNull DfaInstructionState instructionState, @NotNull PsiLambdaExpression expr) {
   DfaMemoryState state = instructionState.getMemoryState();
   PsiElement body = expr.getBody();
   if (body != null) {
     myNestedClosures.putValue(body, createClosureState(state));
   }
 }
Пример #4
0
 @Override
 public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
   final PsiElement element = descriptor.getPsiElement();
   if (element != null) {
     if (!FileModificationService.getInstance().preparePsiElementForWrite(element)) return;
     final PsiLambdaExpression lambdaExpression =
         PsiTreeUtil.getParentOfType(element, PsiLambdaExpression.class);
     if (lambdaExpression != null) {
       final PsiElement body = lambdaExpression.getBody();
       if (body != null) {
         PsiExpression expression = LambdaUtil.extractSingleExpressionFromBody(body);
         if (expression != null) {
           body.replace(expression);
         }
       }
     }
   }
 }
Пример #5
0
 public static boolean processDeclarationsInLambda(
     @NotNull final PsiLambdaExpression lambda,
     @NotNull final PsiScopeProcessor processor,
     @NotNull final ResolveState state,
     final PsiElement lastParent,
     @NotNull final PsiElement place) {
   final boolean fromBody = lastParent != null && lastParent == lambda.getBody();
   return processDeclarationsInMethodLike(lambda, processor, state, place, fromBody, null);
 }
Пример #6
0
 @Nullable
 public static PsiElement getFirstElementOnTheLine(
     PsiLambdaExpression lambda, Document document, int line) {
   ApplicationManager.getApplication().assertReadAccessAllowed();
   TextRange lineRange = DocumentUtil.getLineTextRange(document, line);
   if (!intersects(lineRange, lambda)) return null;
   PsiElement body = lambda.getBody();
   if (body == null || !intersects(lineRange, body)) return null;
   if (body instanceof PsiCodeBlock) {
     for (PsiStatement statement : ((PsiCodeBlock) body).getStatements()) {
       if (intersects(lineRange, statement)) {
         return statement;
       }
     }
     return null;
   }
   return body;
 }