use of org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock in project intellij-community by JetBrains.
the class GroovyGenerationInfo method positionCaret.
@Override
public void positionCaret(@NotNull Editor editor, boolean toEditMethodBody) {
final T firstMember = getPsiMember();
LOG.assertTrue(firstMember.isValid());
if (toEditMethodBody) {
GrMethod method = (GrMethod) firstMember;
GrOpenBlock body = method.getBlock();
if (body != null) {
PsiElement l = body.getLBrace();
if (l != null)
l = l.getNextSibling();
while (PsiImplUtil.isWhiteSpaceOrNls(l)) l = l.getNextSibling();
if (l == null)
l = body;
PsiElement r = body.getRBrace();
if (r != null)
r = r.getPrevSibling();
while (PsiImplUtil.isWhiteSpaceOrNls(r)) r = r.getPrevSibling();
if (r == null)
r = body;
int start = l.getTextRange().getStartOffset();
int end = r.getTextRange().getEndOffset();
editor.getCaretModel().moveToOffset(Math.min(start, end));
editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE);
if (start < end) {
//Not an empty body
editor.getSelectionModel().setSelection(start, end);
}
return;
}
}
int offset;
if (firstMember instanceof GrMethod) {
GrMethod method = (GrMethod) firstMember;
GrCodeBlock body = method.getBlock();
if (body == null) {
offset = method.getTextRange().getStartOffset();
} else {
offset = body.getLBrace().getTextRange().getEndOffset();
}
} else {
offset = firstMember.getTextRange().getStartOffset();
}
editor.getCaretModel().moveToOffset(offset);
editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE);
editor.getSelectionModel().removeSelection();
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock in project intellij-community by JetBrains.
the class GrIntroduceClosureParameterProcessor method insertDeclaration.
private GrVariableDeclaration insertDeclaration(GrVariable original, GrVariableDeclaration declaration) {
if (original instanceof GrField) {
final PsiClass containingClass = ((GrField) original).getContainingClass();
LOG.assertTrue(containingClass != null);
return (GrVariableDeclaration) containingClass.addBefore(declaration, original.getParent());
}
final GrStatementOwner block;
if (original instanceof PsiParameter) {
final PsiElement container = original.getParent().getParent();
if (container instanceof GrMethod) {
block = ((GrMethod) container).getBlock();
} else if (container instanceof GrClosableBlock) {
block = (GrCodeBlock) container;
} else if (container instanceof GrForStatement) {
final GrStatement body = ((GrForStatement) container).getBody();
if (body instanceof GrBlockStatement) {
block = ((GrBlockStatement) body).getBlock();
} else {
GrBlockStatement blockStatement = myFactory.createBlockStatement();
LOG.assertTrue(blockStatement != null);
if (body != null) {
blockStatement.getBlock().addStatementBefore((GrStatement) body.copy(), null);
blockStatement = (GrBlockStatement) body.replace(blockStatement);
} else {
blockStatement = (GrBlockStatement) container.add(blockStatement);
}
block = blockStatement.getBlock();
}
} else {
throw new IncorrectOperationException();
}
LOG.assertTrue(block != null);
return (GrVariableDeclaration) block.addStatementBefore(declaration, null);
}
PsiElement parent = original.getParent();
LOG.assertTrue(parent instanceof GrVariableDeclaration);
final PsiElement pparent = parent.getParent();
if (pparent instanceof GrIfStatement) {
if (((GrIfStatement) pparent).getThenBranch() == parent) {
block = ((GrIfStatement) pparent).replaceThenBranch(myFactory.createBlockStatement()).getBlock();
} else {
block = ((GrIfStatement) pparent).replaceElseBranch(myFactory.createBlockStatement()).getBlock();
}
parent = block.addStatementBefore(((GrVariableDeclaration) parent), null);
} else if (pparent instanceof GrLoopStatement) {
block = ((GrLoopStatement) pparent).replaceBody(myFactory.createBlockStatement()).getBlock();
parent = block.addStatementBefore(((GrVariableDeclaration) parent), null);
} else {
LOG.assertTrue(pparent instanceof GrStatementOwner);
block = (GrStatementOwner) pparent;
}
return (GrVariableDeclaration) block.addStatementBefore(declaration, (GrStatement) parent);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock in project intellij-community by JetBrains.
the class GrBracesSurrounder method doSurroundElements.
@Override
protected GroovyPsiElement doSurroundElements(PsiElement[] elements, PsiElement context) throws IncorrectOperationException {
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(elements[0].getProject());
final PsiElement e0 = elements[0];
final PsiElement parent = e0.getParent();
final GrCodeBlock block;
if (parent instanceof GrControlStatement) {
block = factory.createMethodBodyFromText("\n");
final PsiElement prev = e0.getPrevSibling();
if (prev != null && prev.getNode().getElementType().equals(GroovyTokenTypes.mNLS)) {
final ASTNode parentNode = e0.getParent().getNode();
parentNode.addLeaf(TokenType.WHITE_SPACE, " ", prev.getNode());
parentNode.removeChild(prev.getNode());
}
} else {
block = factory.createClosureFromText("{}");
}
GroovyManyStatementsSurrounder.addStatements(block, elements);
return block;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock in project intellij-community by JetBrains.
the class RecursionUtils method tryStatementMayReturnBeforeRecursing.
private static boolean tryStatementMayReturnBeforeRecursing(GrTryCatchStatement tryStatement, GrMethod method) {
final GrFinallyClause finallyBlock = tryStatement.getFinallyClause();
if (finallyBlock != null) {
final GrOpenBlock body = finallyBlock.getBody();
if (codeBlockMayReturnBeforeRecursing(body, method, false)) {
return true;
}
if (codeBlockDefinitelyRecurses(body, method)) {
return false;
}
}
final GrCodeBlock tryBlock = tryStatement.getTryBlock();
if (codeBlockMayReturnBeforeRecursing(tryBlock, method, false)) {
return true;
}
final GrCatchClause[] catchBlocks = tryStatement.getCatchClauses();
for (final GrCatchClause catchBlock : catchBlocks) {
if (codeBlockMayReturnBeforeRecursing(catchBlock.getBody(), method, false)) {
return true;
}
}
return false;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock in project intellij-community by JetBrains.
the class ControlFlowUtils method openBlockCompletesWithStatement.
public static boolean openBlockCompletesWithStatement(@NotNull GrCodeBlock body, @NotNull GrStatement statement) {
GroovyPsiElement elementToCheck = statement;
while (true) {
if (elementToCheck == null)
return false;
final GroovyPsiElement container = PsiTreeUtil.getParentOfType(elementToCheck, GrStatement.class, GrCodeBlock.class, GrCaseSection.class);
if (container == null)
return false;
if (isLoop(container))
return false;
if (container instanceof GrCaseSection) {
final GrSwitchStatement switchStatement = (GrSwitchStatement) container.getParent();
final GrCaseSection[] sections = switchStatement.getCaseSections();
if (container == sections[sections.length - 1])
return false;
}
if (container instanceof GrCodeBlock) {
if (elementToCheck instanceof GrStatement) {
final GrCodeBlock codeBlock = (GrCodeBlock) container;
if (!statementIsLastInBlock(codeBlock, (GrStatement) elementToCheck)) {
return false;
}
}
if (container instanceof GrOpenBlock || container instanceof GrClosableBlock) {
if (container.equals(body)) {
return true;
}
elementToCheck = PsiTreeUtil.getParentOfType(container, GrStatement.class);
} else {
elementToCheck = container;
}
} else {
elementToCheck = container;
}
}
}
Aggregations