Пример #1
0
  private static boolean isBracelessBlock(@NotNull PsiElement element) {
    if (!(element instanceof JetBlockExpression)) return false;

    JetBlockExpression block = (JetBlockExpression) element;

    return block.getLBrace() == null && block.getRBrace() == null;
  }
Пример #2
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;
  }
Пример #3
0
  @Nullable
  public static JetElement getOutermostLastBlockElement(
      @Nullable JetElement element, @NotNull Predicate<JetElement> checkElement) {
    if (element == null) return null;

    if (!(element instanceof JetBlockExpression))
      return checkElement.apply(element) ? element : null;

    JetBlockExpression block = (JetBlockExpression) element;
    int n = block.getStatements().size();

    if (n == 0) return null;

    JetElement lastElement = block.getStatements().get(n - 1);
    return checkElement.apply(lastElement) ? lastElement : null;
  }
Пример #4
0
  public static boolean checkVariableDeclarationInBlock(
      @NotNull JetBlockExpression block, @NotNull String varName) {
    for (JetElement element : block.getStatements()) {
      if (element instanceof JetVariableDeclaration) {
        if (((JetVariableDeclaration) element).getNameAsSafeName().asString().equals(varName)) {
          return true;
        }
      }
    }

    return false;
  }
Пример #5
0
 public static boolean isImplicitlyUsed(@NotNull JetElement element) {
   PsiElement parent = element.getParent();
   if (!(parent instanceof JetBlockExpression)) return true;
   JetBlockExpression block = (JetBlockExpression) parent;
   List<JetElement> statements = block.getStatements();
   if (statements.get(statements.size() - 1) == element) {
     JetExpression expression = getDirectParentOfTypeForBlock(block, JetIfExpression.class);
     if (expression == null) {
       expression = getDirectParentOfTypeForBlock(block, JetWhenExpression.class);
     }
     if (expression == null) {
       expression = getDirectParentOfTypeForBlock(block, JetFunctionLiteral.class);
     }
     if (expression == null) {
       expression = getDirectParentOfTypeForBlock(block, JetTryExpression.class);
     }
     if (expression != null) {
       return isImplicitlyUsed(expression);
     }
   }
   return false;
 }
Пример #6
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;
  }
Пример #7
0
 @Nullable
 public static JetElement getLastStatementInABlock(@Nullable JetBlockExpression blockExpression) {
   if (blockExpression == null) return null;
   List<JetElement> statements = blockExpression.getStatements();
   return statements.isEmpty() ? null : statements.get(statements.size() - 1);
 }