use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement 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.GrStatement 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.GrStatement 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.GrStatement 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.GrStatement in project intellij-community by JetBrains.
the class OldReferencesResolver method inlineParam.
private PsiElement inlineParam(PsiElement newExpr, GrExpression actualArg, PsiParameter parameter) {
if (myParamsToNotInline.contains(parameter))
return newExpr;
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(myProject);
if (myExpr instanceof GrClosableBlock) {
int count = 0;
for (PsiReference reference : ReferencesSearch.search(parameter, new LocalSearchScope(myParameterInitializer))) {
count++;
if (count > 1)
break;
}
if (count > 1) {
myParamsToNotInline.add(parameter);
final PsiType type;
if (parameter instanceof GrParameter) {
type = ((GrParameter) parameter).getDeclaredType();
} else {
type = parameter.getType();
}
final GrVariableDeclaration declaration = factory.createVariableDeclaration(ArrayUtil.EMPTY_STRING_ARRAY, actualArg, type, parameter.getName());
final GrStatement[] statements = ((GrClosableBlock) myExpr).getStatements();
GrStatement anchor = statements.length > 0 ? statements[0] : null;
return ((GrClosableBlock) myExpr).addStatementBefore(declaration, anchor);
}
}
int copyingSafetyLevel = GroovyRefactoringUtil.verifySafeCopyExpression(actualArg);
if (copyingSafetyLevel == RefactoringUtil.EXPR_COPY_PROHIBITED) {
actualArg = factory.createExpressionFromText(getTempVar(actualArg));
}
newExpr = newExpr.replace(actualArg);
return newExpr;
}
Aggregations