use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrUnaryExpression in project intellij-community by JetBrains.
the class BoolUtils method getNegated.
public static GrExpression getNegated(GrExpression exp) {
final GrUnaryExpression prefixExp = (GrUnaryExpression) exp;
final GrExpression operand = prefixExp.getOperand();
return (GrExpression) PsiUtil.skipParentheses(operand, false);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrUnaryExpression in project intellij-community by JetBrains.
the class BoolUtils method isNegation.
public static boolean isNegation(@Nullable PsiElement exp) {
if (!(exp instanceof GrUnaryExpression)) {
return false;
}
final GrUnaryExpression prefixExp = (GrUnaryExpression) exp;
final IElementType sign = prefixExp.getOperationTokenType();
return GroovyTokenTypes.mLNOT.equals(sign);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrUnaryExpression in project intellij-community by JetBrains.
the class BoolUtils method isNegated.
public static boolean isNegated(GrExpression exp) {
GrExpression ancestor = exp;
while (ancestor.getParent() instanceof GrParenthesizedExpression) {
ancestor = (GrExpression) ancestor.getParent();
}
if (ancestor.getParent() instanceof GrUnaryExpression) {
final GrUnaryExpression prefixAncestor = (GrUnaryExpression) ancestor.getParent();
final IElementType sign = prefixAncestor.getOperationTokenType();
if (GroovyTokenTypes.mLNOT.equals(sign)) {
return true;
}
}
return false;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrUnaryExpression in project intellij-community by JetBrains.
the class BoolUtils method findNegation.
@Nullable
public static GrExpression findNegation(GrExpression exp) {
GrExpression ancestor = exp;
while (ancestor.getParent() instanceof GrParenthesizedExpression) {
ancestor = (GrExpression) ancestor.getParent();
}
if (ancestor.getParent() instanceof GrUnaryExpression) {
final GrUnaryExpression prefixAncestor = (GrUnaryExpression) ancestor.getParent();
final IElementType sign = prefixAncestor.getOperationTokenType();
if (GroovyTokenTypes.mLNOT.equals(sign)) {
return prefixAncestor;
}
}
return null;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrUnaryExpression 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);
}
Aggregations