use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrIfStatement in project intellij-community by JetBrains.
the class GrRedundantElseIntention method processIntention.
@Override
protected void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
final PsiElement parent = element.getParent();
if (!(parent instanceof GrIfStatement))
return;
final GrIfStatement ifStatement = GroovyRefactoringUtil.addBlockIntoParent((GrIfStatement) parent);
assert ifStatement.getParent() instanceof GrStatementOwner;
final PsiElement statementOwner = ifStatement.getParent();
final GrStatement branch = ifStatement.getElseBranch();
if (branch == null)
return;
final PsiElement pos;
if (branch instanceof GrBlockStatement) {
final GrOpenBlock block = ((GrBlockStatement) branch).getBlock();
final PsiElement first = inferFirst(block.getLBrace());
final PsiElement last = inferLast(block.getRBrace());
pos = statementOwner.addRangeAfter(first, last, ifStatement);
} else {
pos = statementOwner.addAfter(branch, ifStatement);
}
branch.delete();
editor.getCaretModel().moveToOffset(pos.getTextRange().getStartOffset());
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrIfStatement in project intellij-community by JetBrains.
the class InvertIfIntention method processIntention.
@Override
protected void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
PsiElement parent = element.getParent();
if (!"if".equals(element.getText()) || !(parent instanceof GrIfStatement)) {
throw new IncorrectOperationException("Not invoked on an if");
}
GrIfStatement parentIf = (GrIfStatement) parent;
GroovyPsiElementFactory groovyPsiElementFactory = GroovyPsiElementFactory.getInstance(project);
GrExpression condition = parentIf.getCondition();
if (condition == null) {
throw new IncorrectOperationException("Invoked on an if with empty condition");
}
GrExpression negatedCondition = null;
if (condition instanceof GrUnaryExpression) {
GrUnaryExpression unaryCondition = (GrUnaryExpression) condition;
if ("!".equals(unaryCondition.getOperationToken().getText())) {
negatedCondition = stripParenthesis(unaryCondition.getOperand());
}
}
if (negatedCondition == null) {
// Now check whether this is a simple expression
condition = stripParenthesis(condition);
String negatedExpressionText;
if (condition instanceof GrCallExpression || condition instanceof GrReferenceExpression) {
negatedExpressionText = "!" + condition.getText();
} else {
negatedExpressionText = "!(" + condition.getText() + ")";
}
negatedCondition = groovyPsiElementFactory.createExpressionFromText(negatedExpressionText, parentIf);
}
GrStatement thenBranch = parentIf.getThenBranch();
final boolean thenIsNotEmpty = isNotEmpty(thenBranch);
String newIfText = "if (" + negatedCondition.getText() + ") {}";
if (thenIsNotEmpty) {
newIfText += " else {}";
}
GrIfStatement newIf = (GrIfStatement) groovyPsiElementFactory.createStatementFromText(newIfText, parentIf.getContext());
generateElseBranchTextAndRemoveTailStatements(parentIf, newIf);
if (thenIsNotEmpty) {
final GrStatement elseBranch = newIf.getElseBranch();
assert elseBranch != null;
elseBranch.replaceWithStatement(thenBranch);
}
parentIf.replace(newIf);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrIfStatement 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.GrIfStatement in project intellij-community by JetBrains.
the class SplitElseIfPredicate method satisfiedBy.
@Override
public boolean satisfiedBy(PsiElement element) {
if (!(element instanceof GrIfStatement)) {
return false;
}
final GrIfStatement ifStatement = (GrIfStatement) element;
final GrStatement thenBranch = ifStatement.getThenBranch();
if (thenBranch == null) {
return false;
}
final GrStatement elseBranch = ifStatement.getElseBranch();
return elseBranch instanceof GrIfStatement;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrIfStatement in project intellij-community by JetBrains.
the class ReplaceIfWithTernaryIntention method processIntention.
@Override
protected void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
final GrIfStatement ifStatement = (GrIfStatement) element.getParent();
final PsiElement thenBranch = skipBlock(ifStatement.getThenBranch());
final PsiElement elseBranch = skipBlock(ifStatement.getElseBranch());
if (thenBranch instanceof GrAssignmentExpression && elseBranch instanceof GrAssignmentExpression) {
final GrAssignmentExpression assignment = (GrAssignmentExpression) GroovyPsiElementFactory.getInstance(project).createStatementFromText("a = b ? c : d");
assignment.getLValue().replaceWithExpression(((GrAssignmentExpression) thenBranch).getLValue(), true);
final GrConditionalExpression conditional = (GrConditionalExpression) assignment.getRValue();
replaceConditional(conditional, ifStatement.getCondition(), ((GrAssignmentExpression) thenBranch).getRValue(), ((GrAssignmentExpression) elseBranch).getRValue());
ifStatement.replaceWithStatement(assignment);
}
if (thenBranch instanceof GrReturnStatement && elseBranch instanceof GrReturnStatement) {
final GrReturnStatement returnSt = (GrReturnStatement) GroovyPsiElementFactory.getInstance(project).createStatementFromText("return a ? b : c");
final GrConditionalExpression conditional = (GrConditionalExpression) returnSt.getReturnValue();
replaceConditional(conditional, ifStatement.getCondition(), ((GrReturnStatement) thenBranch).getReturnValue(), ((GrReturnStatement) elseBranch).getReturnValue());
ifStatement.replaceWithStatement(returnSt);
}
}
Aggregations