private void replaceWithBlock(@NotNull GrStatement statement, GrStatement blockCandidate) {
    if (!statement.isValid()) {
      LOG.assertTrue(false);
    }

    if (!checkRangeContainsElement(blockCandidate)) return;

    final PsiManager manager = statement.getManager();
    LOG.assertTrue(manager != null);
    GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(manager.getProject());

    String oldText = blockCandidate.getText();
    // There is a possible case that target block to wrap ends with single-line comment. Example:
    //     if (true) i = 1; // Cool assignment
    // We can't just surround target block of code with curly braces because the closing one will be treated as comment as well.
    // Hence, we perform a check if we have such situation at the moment and insert new line before the closing brace.
    StringBuilder buf = new StringBuilder(oldText.length() + 5);
    buf.append("{\n").append(oldText);
    buf.append("\n}");
    final int oldTextLength = statement.getTextLength();
    try {
      ASTNode newChild = SourceTreeToPsiMap.psiElementToTree(factory.createBlockStatementFromText(buf.toString(), null));
      ASTNode parent = SourceTreeToPsiMap.psiElementToTree(statement);
      ASTNode childToReplace = SourceTreeToPsiMap.psiElementToTree(blockCandidate);
      CodeEditUtil.replaceChild(parent, childToReplace, newChild);

      removeTailSemicolon(newChild, parent);
      statement = forcePsiPostprocessAndRestoreElement(statement);
      if (statement != null) {
        CodeStyleManager.getInstance(statement.getProject()).reformat(statement, true);
      }
    }
    catch (IncorrectOperationException e) {
      LOG.error(e);
    }
    finally {
      updateResultRange(oldTextLength, statement.getTextLength());
    }
  }
private void replaceWithBlock(@NotNull GrStatement statement, GrStatement blockCandidate)