Exemple #1
0
  protected static PsiElement adjustSibling(
      @NotNull Editor editor,
      @NotNull LineRange sourceRange,
      @NotNull MoveInfo info,
      boolean down) {
    PsiElement element = down ? sourceRange.lastElement : sourceRange.firstElement;
    PsiElement sibling = down ? element.getNextSibling() : element.getPrevSibling();

    PsiElement whiteSpaceTestSubject = sibling;
    if (sibling == null) {
      PsiElement parent = element.getParent();
      if (parent != null && isBracelessBlock(parent)) {
        whiteSpaceTestSubject = down ? parent.getNextSibling() : parent.getPrevSibling();
      }
    }

    if (whiteSpaceTestSubject instanceof PsiWhiteSpace) {
      if (getElementLineCount(whiteSpaceTestSubject, editor) > 1) {
        int nearLine = down ? sourceRange.endLine : sourceRange.startLine - 1;

        info.toMove = sourceRange;
        info.toMove2 = new LineRange(nearLine, nearLine + 1);
        info.indentTarget = false;

        return null;
      }

      if (sibling != null) {
        sibling = firstNonWhiteElement(sibling, down);
      }
    }

    if (sibling == null) {
      JetCallExpression callExpression =
          PsiTreeUtil.getParentOfType(element, JetCallExpression.class);
      if (callExpression != null) {
        JetBlockExpression dslBlock = getDSLLambdaBlock(callExpression, down);
        if (PsiTreeUtil.isAncestor(dslBlock, element, false)) {
          //noinspection ConstantConditions
          PsiElement blockParent = dslBlock.getParent();
          return down
              ? JetPsiUtil.findChildByType(blockParent, JetTokens.RBRACE)
              : JetPsiUtil.findChildByType(blockParent, JetTokens.LBRACE);
        }
      }

      info.toMove2 = null;
      return null;
    }

    return sibling;
  }
Exemple #2
0
  @Nullable
  private static LineRange getExpressionTargetRange(
      @NotNull Editor editor, @NotNull PsiElement sibling, boolean down) {
    if (sibling instanceof JetIfExpression && !down) {
      JetExpression elseBranch = ((JetIfExpression) sibling).getElse();
      if (elseBranch instanceof JetBlockExpression) {
        sibling = elseBranch;
      }
    }

    PsiElement start = sibling;
    PsiElement end = sibling;

    // moving out of code block
    if (sibling.getNode().getElementType() == (down ? JetTokens.RBRACE : JetTokens.LBRACE)) {
      PsiElement parent = sibling.getParent();
      if (!(parent instanceof JetBlockExpression || parent instanceof JetFunctionLiteral))
        return null;

      JetBlockExpression newBlock;
      if (parent instanceof JetFunctionLiteral) {
        //noinspection ConstantConditions
        newBlock = findClosestBlock(((JetFunctionLiteral) parent).getBodyExpression(), down, false);

        if (!down) {
          ASTNode arrow = ((JetFunctionLiteral) parent).getArrowNode();
          if (arrow != null) {
            end = arrow.getPsi();
          }
        }
      } else {
        newBlock = findClosestBlock(sibling, down, true);
      }

      if (newBlock == null) return null;

      if (PsiTreeUtil.isAncestor(newBlock, parent, true)) {
        PsiElement outermostParent = JetPsiUtil.getOutermostParent(parent, newBlock, true);

        if (down) {
          end = outermostParent;
        } else {
          start = outermostParent;
        }
      } else {
        if (down) {
          end = newBlock.getLBrace();
        } else {
          start = newBlock.getRBrace();
        }
      }
    }
    // moving into code block
    else {
      PsiElement blockLikeElement;

      JetBlockExpression dslBlock = getDSLLambdaBlock(sibling, down);
      if (dslBlock != null) {
        // Use JetFunctionLiteral (since it contains braces)
        blockLikeElement = dslBlock.getParent();
      } else {
        // JetBlockExpression and other block-like elements
        blockLikeElement =
            JetPsiUtil.getOutermostDescendantElement(sibling, down, CHECK_BLOCK_LIKE_ELEMENT);
      }

      if (blockLikeElement != null) {
        if (down) {
          end = JetPsiUtil.findChildByType(blockLikeElement, JetTokens.LBRACE);
          if (blockLikeElement instanceof JetFunctionLiteral) {
            ASTNode arrow = ((JetFunctionLiteral) blockLikeElement).getArrowNode();
            if (arrow != null) {
              end = arrow.getPsi();
            }
          }
        } else {
          start = JetPsiUtil.findChildByType(blockLikeElement, JetTokens.RBRACE);
        }
      }
    }

    return start != null && end != null ? new LineRange(start, end, editor.getDocument()) : null;
  }