Exemplo n.º 1
0
  @Nullable
  private static LineRange getWhenEntryTargetRange(
      @NotNull Editor editor, @NotNull PsiElement sibling, boolean down) {
    if (sibling.getNode().getElementType() == (down ? JetTokens.RBRACE : JetTokens.LBRACE)
        && PsiTreeUtil.getParentOfType(sibling, JetWhenEntry.class) == null) {
      return null;
    }

    return new LineRange(sibling, sibling, editor.getDocument());
  }
Exemplo n.º 2
0
  @Nullable
  private LineRange getValueParamOrArgTargetRange(
      @NotNull Editor editor,
      @NotNull PsiElement elementToCheck,
      @NotNull PsiElement sibling,
      boolean down) {
    PsiElement next = sibling;

    if (next.getNode().getElementType() == JetTokens.COMMA) {
      next = firstNonWhiteSibling(next, down);
    }

    LineRange range =
        (next instanceof JetParameter || next instanceof JetValueArgument)
            ? new LineRange(next, next, editor.getDocument())
            : null;

    if (range != null) {
      parametersOrArgsToMove = new Pair<PsiElement, PsiElement>(elementToCheck, next);
    }

    return range;
  }
Exemplo n.º 3
0
 private static PsiElement getComma(@NotNull PsiElement element) {
   PsiElement sibling = firstNonWhiteSibling(element, true);
   return sibling != null && (sibling.getNode().getElementType() == JetTokens.COMMA)
       ? sibling
       : null;
 }
Exemplo n.º 4
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;
  }