Java 类com.intellij.psi.PsiBlockStatement 实例源码

项目:intellij-ce-playground    文件:DoWhileConditionFixer.java   
@Override
public void apply(Editor editor, JavaSmartEnterProcessor processor, PsiElement psiElement) throws IncorrectOperationException {
  if (psiElement instanceof PsiDoWhileStatement) {
    final Document doc = editor.getDocument();
    final PsiDoWhileStatement stmt = (PsiDoWhileStatement) psiElement;
    if (stmt.getBody() == null || !(stmt.getBody() instanceof PsiBlockStatement) && stmt.getWhileKeyword() == null) {
      final int startOffset = stmt.getTextRange().getStartOffset();
      doc.replaceString(startOffset, startOffset + "do".length(), "do {} while()");
      return;
    }

    if (stmt.getCondition() == null) {
      if (stmt.getWhileKeyword() == null) {
        final int endOffset = stmt.getTextRange().getEndOffset();
        doc.insertString(endOffset, "while()");
      } else if (stmt.getLParenth() == null || stmt.getRParenth() == null) {
        final TextRange whileRange = stmt.getWhileKeyword().getTextRange();
        doc.replaceString(whileRange.getStartOffset(), whileRange.getEndOffset(), "while()");
      } else {
        processor.registerUnresolvedError(stmt.getLParenth().getTextRange().getEndOffset());
      }
    }
  }
}
项目:tools-idea    文件:DoWhileConditionFixer.java   
@Override
public void apply(Editor editor, JavaSmartEnterProcessor processor, PsiElement psiElement) throws IncorrectOperationException {
  if (psiElement instanceof PsiDoWhileStatement) {
    final Document doc = editor.getDocument();
    final PsiDoWhileStatement stmt = (PsiDoWhileStatement) psiElement;
    if (stmt.getBody() == null || !(stmt.getBody() instanceof PsiBlockStatement) && stmt.getWhileKeyword() == null) {
      final int startOffset = stmt.getTextRange().getStartOffset();
      doc.replaceString(startOffset, startOffset + "do".length(), "do {} while()");
      return;
    }

    if (stmt.getCondition() == null) {
      if (stmt.getWhileKeyword() == null) {
        final int endOffset = stmt.getTextRange().getEndOffset();
        doc.insertString(endOffset, "while()");
      } else if (stmt.getLParenth() == null || stmt.getRParenth() == null) {
        final TextRange whileRange = stmt.getWhileKeyword().getTextRange();
        doc.replaceString(whileRange.getStartOffset(), whileRange.getEndOffset(), "while()");
      } else {
        processor.registerUnresolvedError(stmt.getLParenth().getTextRange().getEndOffset());
      }
    }
  }
}
项目:consulo-java    文件:BlockUtils.java   
/**
 * Add new statement after given anchor statement creating code block, if necessary
 *
 * @param anchor       existing statement
 * @param newStatement a new statement which should be added after an existing one
 * @return added physical statement
 */
public static PsiStatement addAfter(PsiStatement anchor, PsiStatement newStatement)
{
    PsiElement oldStatement = anchor;
    PsiElement parent = oldStatement.getParent();
    while(parent instanceof PsiLabeledStatement)
    {
        oldStatement = parent;
        parent = oldStatement.getParent();
    }
    final PsiElement result;
    if(parent instanceof PsiCodeBlock)
    {
        result = parent.addAfter(newStatement, oldStatement);
    }
    else
    {
        PsiElementFactory factory = JavaPsiFacade.getElementFactory(anchor.getProject());
        final PsiBlockStatement newBlockStatement = (PsiBlockStatement) factory.createStatementFromText("{}", oldStatement);
        final PsiElement codeBlock = newBlockStatement.getCodeBlock();
        codeBlock.add(oldStatement);
        codeBlock.add(newStatement);
        result = ((PsiBlockStatement) oldStatement.replace(newBlockStatement)).getCodeBlock().getStatements()[1];
    }
    return (PsiStatement) result;
}
项目:Android_Lint_SRP_Practice_Example    文件:ElementUtil.java   
private static void seekElderBrother(PsiElement element, PsiIdentifier target, Find find){
    PsiElement parent = LintUtils.skipParentheses(element.getParent());
    if (parent == null) return;

    int myIndex = getMyBrotherIndex(element);
    PsiElement[] brothers = parent.getChildren();
    for (int index = myIndex -1; index >= 0; index--) {
        PsiElement brother = brothers[index];
        // 兄弟のブロック内のノードは、アクセス不能なので除外
        if (brother instanceof PsiBlockStatement) continue;
        if (brother instanceof PsiLocalVariable) {
            PsiLocalVariable localVariable = (PsiLocalVariable) brother;
            if (isSameName(target, localVariable.getNameIdentifier())) {
                find.findLocalVariable = localVariable;
                return;
            }
        }
        if (brother instanceof PsiParameter) {
            PsiParameter parameter = (PsiParameter) brother;
            if (isSameName(target, parameter.getNameIdentifier())) {
                find.findParameter = parameter;
                return;
            }
        }
        seekChildren(brother, target, find);
        if (!find.isEmpty()) return;
    }
}
项目:consulo-java    文件:DoWhileConditionFixer.java   
@Override
public void apply(Editor editor, JavaSmartEnterProcessor processor, PsiElement psiElement) throws IncorrectOperationException
{
    if(psiElement instanceof PsiDoWhileStatement)
    {
        final Document doc = editor.getDocument();
        final PsiDoWhileStatement stmt = (PsiDoWhileStatement) psiElement;
        if(stmt.getBody() == null || !(stmt.getBody() instanceof PsiBlockStatement) && stmt.getWhileKeyword() == null)
        {
            final int startOffset = stmt.getTextRange().getStartOffset();
            doc.replaceString(startOffset, startOffset + "do".length(), "do {} while()");
            return;
        }

        if(stmt.getCondition() == null)
        {
            if(stmt.getWhileKeyword() == null)
            {
                final int endOffset = stmt.getTextRange().getEndOffset();
                doc.insertString(endOffset, "while()");
            }
            else if(stmt.getLParenth() == null || stmt.getRParenth() == null)
            {
                final TextRange whileRange = stmt.getWhileKeyword().getTextRange();
                doc.replaceString(whileRange.getStartOffset(), whileRange.getEndOffset(), "while()");
            }
            else
            {
                processor.registerUnresolvedError(stmt.getLParenth().getTextRange().getEndOffset());
            }
        }
    }
}
项目:consulo-java    文件:MissingForBodyFixer.java   
@Override
public void apply(Editor editor, JavaSmartEnterProcessor processor, PsiElement psiElement) throws IncorrectOperationException
{
    PsiForStatement forStatement = getForStatementParent(psiElement);
    if(forStatement == null)
    {
        return;
    }

    final Document doc = editor.getDocument();

    PsiElement body = forStatement.getBody();
    if(body instanceof PsiBlockStatement)
    {
        return;
    }
    if(body != null && startLine(doc, body) == startLine(doc, forStatement))
    {
        return;
    }

    PsiElement eltToInsertAfter = forStatement.getRParenth();
    String text = "{}";
    if(eltToInsertAfter == null)
    {
        eltToInsertAfter = forStatement;
        text = "){}";
    }
    doc.insertString(eltToInsertAfter.getTextRange().getEndOffset(), text);
}
项目:consulo-java    文件:MissingForeachBodyFixer.java   
@Override
public void apply(Editor editor, JavaSmartEnterProcessor processor, PsiElement psiElement) throws IncorrectOperationException
{
    PsiForeachStatement forStatement = getForeachStatementParent(psiElement);
    if(forStatement == null)
    {
        return;
    }

    final Document doc = editor.getDocument();

    PsiElement body = forStatement.getBody();
    if(body instanceof PsiBlockStatement)
    {
        return;
    }
    if(body != null && startLine(doc, body) == startLine(doc, forStatement))
    {
        return;
    }

    PsiElement eltToInsertAfter = forStatement.getRParenth();
    String text = "{}";
    if(eltToInsertAfter == null)
    {
        eltToInsertAfter = forStatement;
        text = "){}";
    }
    doc.insertString(eltToInsertAfter.getTextRange().getEndOffset(), text);
}
项目:consulo-java    文件:IfStatementWithIdenticalBranchesInspection.java   
private void checkIfStatementWithoutElseBranch(
  PsiIfStatement ifStatement) {
  final PsiStatement thenBranch = ifStatement.getThenBranch();
  if (ControlFlowUtils.statementMayCompleteNormally(thenBranch)) {
    return;
  }
  PsiStatement nextStatement = getNextStatement(ifStatement);
  if (thenBranch instanceof PsiBlockStatement) {
    final PsiBlockStatement blockStatement =
      (PsiBlockStatement)thenBranch;
    final PsiCodeBlock codeBlock = blockStatement.getCodeBlock();
    final PsiStatement[] statements = codeBlock.getStatements();
    final PsiStatement lastStatement =
      statements[statements.length - 1];
    for (PsiStatement statement : statements) {
      if (nextStatement == null) {
        if (statement == lastStatement &&
            statement instanceof PsiReturnStatement) {
          final PsiReturnStatement returnStatement =
            (PsiReturnStatement)statement;
          if (returnStatement.getReturnValue() == null) {
            registerStatementError(ifStatement);
          }
        }
        return;
      }
      else if (!EquivalenceChecker.getCanonicalPsiEquivalence().statementsAreEquivalent(
        statement, nextStatement)) {
        return;
      }
      nextStatement = getNextStatement(nextStatement);
    }
  }
  else if (!EquivalenceChecker.getCanonicalPsiEquivalence().statementsAreEquivalent(
    thenBranch, nextStatement)) {
    return;
  }
  registerStatementError(ifStatement);
}
项目:Android_Lint_SRP_Practice_Example    文件:PsiClassStructureDetector.java   
@Override
public void visitBlockStatement(PsiBlockStatement statement) {
    mVisitor.report("PsiBlockStatement", statement.getText(), statement);
    super.visitStatement(statement);
}
项目:intellij-ce-playground    文件:BlockFilter.java   
@Override
public void visitBlockStatement(PsiBlockStatement psiBlockStatement) {
  result = true;
}
项目:consulo-java    文件:BlockUtils.java   
/**
 * Adds new statements before given anchor statement creating a new code block, if necessary
 *
 * @param anchor        existing statement
 * @param newStatements the new statements which should be added before the existing one
 * @return last added physical statement
 */
public static PsiStatement addBefore(PsiStatement anchor, PsiStatement... newStatements)
{
    if(newStatements.length == 0)
    {
        throw new IllegalArgumentException();
    }
    PsiElement oldStatement = anchor;
    PsiElement parent = oldStatement.getParent();
    while(parent instanceof PsiLabeledStatement)
    {
        oldStatement = parent;
        parent = oldStatement.getParent();
    }
    if(newStatements.length == 1 && oldStatement instanceof PsiEmptyStatement)
    {
        return (PsiStatement) oldStatement.replace(newStatements[0]);
    }
    PsiElement result = null;
    if(parent instanceof PsiCodeBlock)
    {
        for(PsiStatement statement : newStatements)
        {
            result = parent.addBefore(statement, oldStatement);
        }
    }
    else
    {
        final PsiElementFactory factory = JavaPsiFacade.getElementFactory(anchor.getProject());
        final PsiBlockStatement newBlockStatement = (PsiBlockStatement) factory.createStatementFromText("{}", oldStatement);
        final PsiElement codeBlock = newBlockStatement.getCodeBlock();
        for(PsiStatement newStatement : newStatements)
        {
            codeBlock.add(newStatement);
        }
        codeBlock.add(oldStatement);
        final PsiStatement[] statements = ((PsiBlockStatement) oldStatement.replace(newBlockStatement)).getCodeBlock().getStatements();
        result = statements[statements.length - 2];
    }
    return (PsiStatement) result;
}