use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrBlockStatement in project intellij-community by JetBrains.
the class InvertIfIntention method generateElseBranchTextAndRemoveTailStatements.
private static void generateElseBranchTextAndRemoveTailStatements(@NotNull GrIfStatement ifStatement, @NotNull GrIfStatement newIf) {
final GrStatement thenBranch = newIf.getThenBranch();
assert thenBranch != null;
GrStatement elseBranch = ifStatement.getElseBranch();
if (elseBranch != null) {
thenBranch.replaceWithStatement(elseBranch);
return;
}
PsiElement parent = ifStatement.getParent();
if (!(parent instanceof GrStatementOwner))
return;
if (!isTailAfterIf(ifStatement, ((GrStatementOwner) parent)))
return;
final PsiElement start = ifStatement.getNextSibling();
PsiElement end = parent instanceof GrCodeBlock ? ((GrCodeBlock) parent).getRBrace().getPrevSibling() : parent.getLastChild();
final GrOpenBlock block = ((GrBlockStatement) thenBranch).getBlock();
block.addRangeAfter(start, end, block.getLBrace());
parent.deleteChildRange(start, end);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrBlockStatement in project intellij-community by JetBrains.
the class SplitElseIfIntention method processIntention.
@Override
public void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
final GrIfStatement parentStatement = (GrIfStatement) element;
final GrStatement elseBranch = parentStatement.getElseBranch();
GrIfStatement ifStatement = (GrIfStatement) parentStatement.copy();
GrBlockStatement blockStatement = GroovyPsiElementFactory.getInstance(project).createBlockStatementFromText("{\nabc()\n}", null);
GrBlockStatement newBlock = ifStatement.replaceElseBranch(blockStatement);
newBlock.getBlock().getStatements()[0].replace(elseBranch);
parentStatement.replaceWithStatement(ifStatement);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrBlockStatement in project intellij-community by JetBrains.
the class IfElseExprSurrounder method surroundExpression.
@Override
protected TextRange surroundExpression(@NotNull GrExpression expression, PsiElement context) {
GrIfStatement ifStatement = (GrIfStatement) GroovyPsiElementFactory.getInstance(expression.getProject()).createStatementFromText("if(a){4\n} else{\n}", context);
replaceToOldExpression(ifStatement.getCondition(), expression);
ifStatement = expression.replaceWithStatement(ifStatement);
GrStatement psiElement = ifStatement.getThenBranch();
assert psiElement instanceof GrBlockStatement;
GrStatement[] statements = ((GrBlockStatement) psiElement).getBlock().getStatements();
assert statements.length > 0;
GrStatement statement = statements[0];
int endOffset = statement.getTextRange().getStartOffset();
statement.getNode().getTreeParent().removeChild(statement.getNode());
return new TextRange(endOffset, endOffset);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrBlockStatement in project intellij-community by JetBrains.
the class GrMissingIfStatement method apply.
@Override
public void apply(@NotNull Editor editor, @NotNull GroovySmartEnterProcessor processor, @NotNull PsiElement psiElement) {
if (!(psiElement instanceof GrIfStatement))
return;
GrIfStatement ifStatement = (GrIfStatement) psiElement;
final Document document = editor.getDocument();
final GrStatement elseBranch = ifStatement.getElseBranch();
final PsiElement elseElement = ifStatement.getElseKeyword();
if (elseElement != null && (elseBranch == null || !(elseBranch instanceof GrBlockStatement) && GrForBodyFixer.startLine(editor.getDocument(), elseBranch) > GrForBodyFixer.startLine(editor.getDocument(), elseElement))) {
document.insertString(elseElement.getTextRange().getEndOffset(), "{}");
}
GrStatement thenBranch = ifStatement.getThenBranch();
if (thenBranch instanceof GrBlockStatement)
return;
boolean transformingOneLiner = false;
if (thenBranch != null && GrForBodyFixer.startLine(editor.getDocument(), thenBranch) == GrForBodyFixer.startLine(editor.getDocument(), ifStatement)) {
if (ifStatement.getCondition() != null) {
return;
}
transformingOneLiner = true;
}
final PsiElement rParenth = ifStatement.getRParenth();
if (rParenth == null)
return;
if (elseBranch == null && !transformingOneLiner || thenBranch == null) {
document.insertString(rParenth.getTextRange().getEndOffset(), "{}");
} else {
document.insertString(rParenth.getTextRange().getEndOffset(), "{");
document.insertString(thenBranch.getTextRange().getEndOffset() + 1, "}");
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrBlockStatement in project intellij-community by JetBrains.
the class IfExprSurrounder method surroundExpression.
@Override
protected TextRange surroundExpression(@NotNull GrExpression expression, PsiElement context) {
GrIfStatement ifStatement = (GrIfStatement) GroovyPsiElementFactory.getInstance(expression.getProject()).createStatementFromText("if(a){4\n}", context);
replaceToOldExpression(ifStatement.getCondition(), expression);
ifStatement = expression.replaceWithStatement(ifStatement);
GrStatement thenBranch = ifStatement.getThenBranch();
assert thenBranch instanceof GrBlockStatement;
GrStatement[] statements = ((GrBlockStatement) thenBranch).getBlock().getStatements();
assert statements.length > 0;
GrStatement statement = statements[0];
int endOffset = statement.getTextRange().getStartOffset();
statement.getNode().getTreeParent().removeChild(statement.getNode());
return new TextRange(endOffset, endOffset);
}
Aggregations