use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrBlockStatement in project intellij-community by JetBrains.
the class ForToEachIntention method processIntention.
@Override
public void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
final GrForStatement parentStatement = (GrForStatement) element;
final GrForInClause clause = (GrForInClause) parentStatement.getClause();
final GrVariable var = clause.getDeclaredVariable();
final GrStatement body = parentStatement.getBody();
final String bodyText;
if (body instanceof GrBlockStatement) {
final String text = body.getText();
bodyText = text.substring(1, text.length() - 1);
} else {
bodyText = body.getText();
}
GrExpression collection = clause.getIteratedExpression();
assert collection != null;
@NonNls final String statement = "x.each{" + var.getText() + " -> " + bodyText + " }";
final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(parentStatement.getProject());
final GrMethodCallExpression eachExpression = (GrMethodCallExpression) factory.createTopElementFromText(statement);
((GrReferenceExpression) eachExpression.getInvokedExpression()).getQualifierExpression().replaceWithExpression(collection, true);
parentStatement.replaceWithStatement(eachExpression);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrBlockStatement in project intellij-community by JetBrains.
the class WhileExprSurrounder method surroundExpression.
@Override
protected TextRange surroundExpression(@NotNull GrExpression expression, PsiElement context) {
GrWhileStatement whileStatement = (GrWhileStatement) GroovyPsiElementFactory.getInstance(expression.getProject()).createStatementFromText("while(a){4\n}", context);
replaceToOldExpression((GrExpression) whileStatement.getCondition(), expression);
whileStatement = expression.replaceWithStatement(whileStatement);
GrStatement body = whileStatement.getBody();
assert body instanceof GrBlockStatement;
GrStatement[] statements = ((GrBlockStatement) body).getBlock().getStatements();
assert statements.length > 0;
GrStatement statement = statements[0];
int offset = statement.getTextRange().getStartOffset();
statement.getNode().getTreeParent().removeChild(statement.getNode());
return new TextRange(offset, offset);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrBlockStatement in project intellij-community by JetBrains.
the class MergeElseIfIntention method processIntention.
@Override
public void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
final GrIfStatement parentStatement = (GrIfStatement) element;
GrBlockStatement elseBlockStatement = (GrBlockStatement) parentStatement.getElseBranch();
assert elseBlockStatement != null;
final GrOpenBlock elseBranch = elseBlockStatement.getBlock();
final GrStatement elseBranchContents = elseBranch.getStatements()[0];
PsiImplUtil.replaceStatement("if(" + parentStatement.getCondition().getText() + ")" + parentStatement.getThenBranch().getText() + "else " + elseBranchContents.getText(), parentStatement);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrBlockStatement in project intellij-community by JetBrains.
the class GroovyInlineMethodUtil method checkTailIfStatement.
public static boolean checkTailIfStatement(GrIfStatement ifStatement, Collection<GrStatement> returnStatements) {
GrStatement thenBranch = ifStatement.getThenBranch();
GrStatement elseBranch = ifStatement.getElseBranch();
if (elseBranch == null)
return false;
boolean tb = false;
boolean eb = false;
if (thenBranch instanceof GrReturnStatement) {
tb = returnStatements.remove(thenBranch);
} else if (thenBranch instanceof GrBlockStatement) {
tb = checkTailOpenBlock(((GrBlockStatement) thenBranch).getBlock(), returnStatements);
}
if (elseBranch instanceof GrReturnStatement) {
eb = returnStatements.remove(elseBranch);
} else if (elseBranch instanceof GrBlockStatement) {
eb = checkTailOpenBlock(((GrBlockStatement) elseBranch).getBlock(), returnStatements);
}
return tb && eb;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrBlockStatement in project intellij-community by JetBrains.
the class GrForBodyFixer method apply.
@Override
public void apply(@NotNull Editor editor, @NotNull GroovySmartEnterProcessor processor, @NotNull PsiElement psiElement) {
GrForStatement forStatement = PsiTreeUtil.getParentOfType(psiElement, GrForStatement.class);
if (forStatement == null)
return;
final Document doc = editor.getDocument();
PsiElement body = forStatement.getBody();
if (body instanceof GrBlockStatement)
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);
}
Aggregations