コード例 #1
0
 public static void removeNewLineAfter(@NotNull GrStatement statement) {
   ASTNode parentNode = statement.getParent().getNode();
   ASTNode next = statement.getNode().getTreeNext();
   if (parentNode != null && next != null && mNLS == next.getElementType()) {
     parentNode.removeChild(next);
   }
 }
コード例 #2
0
  /**
   * check whether statement is return (the statement which provides return value) statement of
   * method or closure.
   *
   * @param st
   * @return
   */
  public static boolean isCertainlyReturnStatement(GrStatement st) {
    final PsiElement parent = st.getParent();
    if (parent instanceof GrOpenBlock) {
      if (st != ArrayUtil.getLastElement(((GrOpenBlock) parent).getStatements())) return false;

      PsiElement pparent = parent.getParent();
      if (pparent instanceof GrMethod) {
        return true;
      }

      if (pparent instanceof GrBlockStatement
          || pparent instanceof GrCatchClause
          || pparent instanceof GrLabeledStatement) {
        pparent = pparent.getParent();
      }
      if (pparent instanceof GrIfStatement
          || pparent instanceof GrControlStatement
          || pparent instanceof GrTryCatchStatement) {
        return isCertainlyReturnStatement((GrStatement) pparent);
      }
    } else if (parent instanceof GrClosableBlock) {
      return st == ArrayUtil.getLastElement(((GrClosableBlock) parent).getStatements());
    } else if (parent instanceof GroovyFileBase) {
      return st == ArrayUtil.getLastElement(((GroovyFileBase) parent).getStatements());
    } else if (parent instanceof GrForStatement
        || parent instanceof GrIfStatement && st != ((GrIfStatement) parent).getCondition()
        || parent instanceof GrSynchronizedStatement
            && st != ((GrSynchronizedStatement) parent).getMonitor()
        || parent instanceof GrWhileStatement && st != ((GrWhileStatement) parent).getCondition()
        || parent instanceof GrConditionalExpression
            && st != ((GrConditionalExpression) parent).getCondition()
        || parent instanceof GrElvisExpression) {
      return isCertainlyReturnStatement((GrStatement) parent);
    } else if (parent instanceof GrCaseSection) {
      final GrStatement[] statements = ((GrCaseSection) parent).getStatements();
      final GrStatement last = ArrayUtil.getLastElement(statements);
      final GrSwitchStatement switchStatement = (GrSwitchStatement) parent.getParent();

      if (last instanceof GrBreakStatement
          && statements.length > 1
          && statements[statements.length - 2] == st) {
        return isCertainlyReturnStatement(switchStatement);
      } else if (st == last) {
        if (st instanceof GrBreakStatement
            || isLastStatementInCaseSection((GrCaseSection) parent, switchStatement)) {
          return isCertainlyReturnStatement(switchStatement);
        }
      }
    }
    return false;
  }
コード例 #3
0
  @NotNull
  public GrStatement addStatementBefore(@NotNull GrStatement element, @Nullable GrStatement anchor)
      throws IncorrectOperationException {
    if (anchor == null && getRBrace() == null) {
      throw new IncorrectOperationException();
    }

    if (anchor != null && !this.equals(anchor.getParent())) {
      throw new IncorrectOperationException();
    }

    final LeafElement nls =
        Factory.createSingleLeafElement(GroovyTokenTypes.mNLS, "\n", 0, 1, null, getManager());

    PsiElement actualAnchor = anchor == null ? getRBrace() : anchor;
    if (mayUseNewLinesAsSeparators()) {
      PsiElement prev = actualAnchor.getPrevSibling();
      if (prev instanceof GrParameterList
          && prev.getTextLength() == 0
          && prev.getPrevSibling() != null) {
        prev = prev.getPrevSibling();
      }
      if (!PsiUtil.isLineFeed(prev)) {
        addBefore(nls.getPsi(), actualAnchor);
      }
    }
    element = (GrStatement) addBefore(element, actualAnchor);
    if (mayUseNewLinesAsSeparators()) {
      addBefore(nls.getPsi(), actualAnchor);
    } else {
      addBefore(
          Factory.createSingleLeafElement(GroovyTokenTypes.mNLS, "\n", 0, 1, null, getManager())
              .getPsi(),
          actualAnchor);
    }
    return element;
  }