use of org.jetbrains.plugins.groovy.intentions.base.PsiElementPredicate in project intellij-community by JetBrains.
the class GrRedundantElseIntention method getElementPredicate.
@NotNull
@Override
protected PsiElementPredicate getElementPredicate() {
return new PsiElementPredicate() {
@Override
public boolean satisfiedBy(PsiElement element) {
if (!(element.getNode().getElementType() == GroovyTokenTypes.kELSE))
return false;
final PsiElement parent = element.getParent();
if (!(parent instanceof GrIfStatement))
return false;
final GrIfStatement ifStatement = (GrIfStatement) parent;
final GrStatement branch = ifStatement.getThenBranch();
final GrControlFlowOwner flowOwner = ControlFlowUtils.findControlFlowOwner(ifStatement);
if (flowOwner == null)
return false;
final Instruction[] flow = flowOwner.getControlFlow();
for (Instruction instruction : flow) {
if (instruction instanceof IfEndInstruction && instruction.getElement() == ifStatement) {
for (Instruction pred : instruction.allPredecessors()) {
final PsiElement predElement = pred.getElement();
if (predElement != null && PsiTreeUtil.isAncestor(branch, predElement, false)) {
return false;
}
}
}
}
return true;
}
};
}
use of org.jetbrains.plugins.groovy.intentions.base.PsiElementPredicate in project intellij-community by JetBrains.
the class ConvertSimpleGetterToPropertyIntention method getElementPredicate.
@NotNull
@Override
protected PsiElementPredicate getElementPredicate() {
return new PsiElementPredicate() {
@Override
public boolean satisfiedBy(PsiElement element) {
PsiElement parent = element.getParent();
if (!(parent instanceof GrMethod) || ((GrMethod) parent).getNameIdentifierGroovy() != element)
return false;
GrMethod method = (GrMethod) parent;
GrOpenBlock block = method.getBlock();
if (block == null)
return false;
GrStatement[] statements = block.getStatements();
if (statements.length != 1)
return false;
if (!GroovyPropertyUtils.isSimplePropertyGetter(method))
return false;
if (GroovyPropertyUtils.findFieldForAccessor(method, true) != null)
return false;
GrStatement statement = statements[0];
if (!(statement instanceof GrReturnStatement && ((GrReturnStatement) statement).getReturnValue() != null || statement instanceof GrExpression && !PsiType.VOID.equals(((GrExpression) statement).getType()))) {
return false;
}
return true;
}
};
}
use of org.jetbrains.plugins.groovy.intentions.base.PsiElementPredicate in project intellij-community by JetBrains.
the class ReplaceIfWithTernaryIntention method getElementPredicate.
@NotNull
@Override
protected PsiElementPredicate getElementPredicate() {
return new PsiElementPredicate() {
@Override
public boolean satisfiedBy(PsiElement e) {
if (!e.getNode().getElementType().equals(GroovyTokenTypes.kIF))
return false;
if (!(e.getParent() instanceof GrIfStatement))
return false;
final GrIfStatement ifStatement = (GrIfStatement) e.getParent();
final PsiElement thenBranch = skipBlock(ifStatement.getThenBranch());
final PsiElement elseBranch = skipBlock(ifStatement.getElseBranch());
if (thenBranch instanceof GrAssignmentExpression && elseBranch instanceof GrAssignmentExpression && ((GrAssignmentExpression) thenBranch).getRValue() != null && ((GrAssignmentExpression) elseBranch).getRValue() != null) {
final GrExpression lvalue1 = ((GrAssignmentExpression) thenBranch).getLValue();
final GrExpression lvalue2 = ((GrAssignmentExpression) elseBranch).getLValue();
return EquivalenceChecker.expressionsAreEquivalent(lvalue1, lvalue2);
}
if (thenBranch instanceof GrReturnStatement && elseBranch instanceof GrReturnStatement && ((GrReturnStatement) thenBranch).getReturnValue() != null && ((GrReturnStatement) elseBranch).getReturnValue() != null) {
return true;
}
return false;
}
};
}
use of org.jetbrains.plugins.groovy.intentions.base.PsiElementPredicate in project intellij-community by JetBrains.
the class SimplifyTernaryOperatorIntention method getElementPredicate.
@NotNull
@Override
protected PsiElementPredicate getElementPredicate() {
return new PsiElementPredicate() {
@Override
public boolean satisfiedBy(PsiElement element) {
if (!(element instanceof GrConditionalExpression)) {
return false;
}
GrConditionalExpression condExp = (GrConditionalExpression) element;
PsiType condType = condExp.getType();
if (condType == null || !PsiType.BOOLEAN.isConvertibleFrom(condType)) {
return false;
}
GrExpression thenBranch = condExp.getThenBranch();
GrExpression elseBranch = condExp.getElseBranch();
Object thenVal = GroovyConstantExpressionEvaluator.evaluate(thenBranch);
if (Boolean.TRUE.equals(thenVal) && elseBranch != null) {
return true;
}
Object elseVal = GroovyConstantExpressionEvaluator.evaluate(elseBranch);
if (thenBranch != null && Boolean.FALSE.equals(elseVal)) {
return true;
}
return false;
}
};
}
use of org.jetbrains.plugins.groovy.intentions.base.PsiElementPredicate in project intellij-community by JetBrains.
the class GrSplitDeclarationIntention method getElementPredicate.
@NotNull
@Override
protected PsiElementPredicate getElementPredicate() {
return new PsiElementPredicate() {
@Override
public boolean satisfiedBy(PsiElement element) {
if (element instanceof GrVariableDeclaration) {
GrVariableDeclaration decl = (GrVariableDeclaration) element;
GrVariable[] variables = decl.getVariables();
if (variables.length > 1 && PsiUtil.isLocalVariable(variables[0])) {
if (!decl.isTuple() || decl.getTupleInitializer() instanceof GrListOrMap) {
myText = GroovyIntentionsBundle.message("split.into.separate.declaration");
} else {
myText = GroovyIntentionsBundle.message("split.into.declaration.and.assignment");
}
return true;
} else if (variables.length == 1 && PsiUtil.isLocalVariable(variables[0]) && variables[0].getInitializerGroovy() != null) {
myText = GroovyIntentionsBundle.message("split.into.declaration.and.assignment");
return true;
}
}
return false;
}
};
}
Aggregations