public void extractFromCodeBlock(GrCodeBlock block, PsiElement from)
        throws IncorrectOperationException {
      if (block == null) return;

      PsiElement rBrace = block.getRBrace();
      PsiElement lBrace = block.getLBrace();

      PsiElement firstBodyElement;
      if (lBrace == null) {
        firstBodyElement = null;
      } else {
        firstBodyElement = lBrace.getNextSibling();
        if (firstBodyElement == rBrace) {
          firstBodyElement = null;
        }
      }

      PsiElement lastBodyElement;
      if (rBrace == null) {
        lastBodyElement = null;
      } else {
        lastBodyElement = rBrace.getPrevSibling();
        if (lastBodyElement == lBrace) {
          lastBodyElement = null;
        }
      }

      extract(firstBodyElement, lastBodyElement, from);
    }
  @Nullable
  protected PsiElement getStatementAtCaret(Editor editor, PsiFile psiFile) {
    final PsiElement atCaret = super.getStatementAtCaret(editor, psiFile);

    if (atCaret instanceof PsiWhiteSpace) return null;
    if (atCaret == null) return null;

    final GrCodeBlock codeBlock =
        PsiTreeUtil.getParentOfType(atCaret, GrCodeBlock.class, false, GrControlStatement.class);
    if (codeBlock != null) {
      for (GrStatement statement : codeBlock.getStatements()) {
        if (PsiTreeUtil.isAncestor(statement, atCaret, true)) {
          return statement;
        }
      }
    }

    PsiElement statementAtCaret =
        PsiTreeUtil.getParentOfType(
            atCaret, GrStatement.class, GrCodeBlock.class, PsiMember.class, GrDocComment.class);

    if (statementAtCaret instanceof GrBlockStatement) return null;
    if (statementAtCaret == null) return null;

    GrControlStatement controlStatement =
        PsiTreeUtil.getParentOfType(statementAtCaret, GrControlStatement.class);

    if (controlStatement != null && !PsiTreeUtil.hasErrorElements(statementAtCaret)) {
      return controlStatement;
    }

    return statementAtCaret instanceof GrStatement || statementAtCaret instanceof GrMember
        ? statementAtCaret
        : null;
  }
  @Override
  protected void reformat(PsiElement atCaret) throws IncorrectOperationException {
    PsiElement parent = atCaret.getParent();
    if (parent instanceof GrCodeBlock) {
      final GrCodeBlock block = (GrCodeBlock) parent;
      if (block.getStatements().length > 0 && block.getStatements()[0] == atCaret) {
        atCaret = block;
      }
    } else if (parent instanceof GrForStatement) {
      atCaret = parent;
    }

    super.reformat(atCaret);
  }
 @Override
 public void setBlock(GrCodeBlock newBlock) {
   ASTNode newNode = newBlock.getNode().copyElement();
   final GrOpenBlock oldBlock = getBlock();
   if (oldBlock == null) {
     getNode().addChild(newNode);
     return;
   }
   getNode().replaceChild(oldBlock.getNode(), newNode);
 }
 public void fix() throws IncorrectOperationException {
   if (myField == null) return;
   final PsiManager manager = myScope.getManager();
   for (GrReferenceExpression referenceExpression : myReferenceExpressions) {
     if (!referenceExpression.isValid()) continue;
     final PsiElement newlyResolved = referenceExpression.resolve();
     if (!manager.areElementsEquivalent(newlyResolved, myField)) {
       qualifyReference(referenceExpression, myField, myQualifyingClass);
     }
   }
 }
  public void generateCodeBlock(GrCodeBlock block, boolean shouldInsertReturnNull) {
    builder.append("{");
    GrParameter[] parameters;
    if (block.getParent() instanceof GrMethod) {
      GrMethod method = (GrMethod) block.getParent();
      parameters = method.getParameters();
    } else if (block instanceof GrClosableBlock) {
      parameters = ((GrClosableBlock) block).getAllParameters();
    } else {
      parameters = GrParameter.EMPTY_ARRAY;
    }

    for (GrParameter parameter : parameters) {
      if (context.analyzedVars.toWrap(parameter)) {
        StringBuilder typeText =
            new StringBuilder().append(GroovyCommonClassNames.GROOVY_LANG_REFERENCE);
        writeTypeParameters(
            typeText,
            new PsiType[] {context.typeProvider.getParameterType(parameter)},
            parameter,
            new GeneratorClassNameProvider());
        builder
            .append("final ")
            .append(typeText)
            .append(' ')
            .append(context.analyzedVars.toVarName(parameter))
            .append(" = new ")
            .append(typeText)
            .append('(')
            .append(parameter.getName())
            .append(");\n");
      }
    }
    visitStatementOwner(block, shouldInsertReturnNull);
    builder.append("}\n");
  }
 @Nullable
 private static GrStatement getFirstStatement(GrCodeBlock block) {
   GrStatement[] statements = block.getStatements();
   if (statements.length == 0) return null;
   return statements[0];
 }