use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrBinaryExpression in project intellij-community by JetBrains.
the class ComparisonUtils method isComparison.
public static boolean isComparison(PsiElement exp) {
if (!(exp instanceof GrBinaryExpression)) {
return false;
}
final GrBinaryExpression binaryExpression = (GrBinaryExpression) exp;
final IElementType sign = binaryExpression.getOperationTokenType();
return s_comparisonStrings.containsKey(sign);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrBinaryExpression in project intellij-community by JetBrains.
the class StringPartInfo method replaceLiteralWithConcatenation.
@NotNull
public GrExpression replaceLiteralWithConcatenation(@Nullable String varName) {
String prefix = preparePrefix();
String suffix = prepareSuffix();
StringBuilder buffer = new StringBuilder();
boolean prefixExists = !GrStringUtil.removeQuotes(prefix).isEmpty();
if (prefixExists) {
buffer.append(prefix).append('+');
}
buffer.append(varName != null ? varName : prepareSelected());
boolean suffixExists = !GrStringUtil.removeQuotes(suffix).isEmpty();
if (suffixExists) {
buffer.append('+').append(suffix);
}
final GrExpression concatenation = GroovyPsiElementFactory.getInstance(myLiteral.getProject()).createExpressionFromText(buffer);
final GrExpression replaced = getLiteral().replaceWithExpression(concatenation, false);
try {
if (prefixExists && suffixExists) {
return ((GrBinaryExpression) ((GrBinaryExpression) replaced).getLeftOperand()).getRightOperand();
}
if (!prefixExists && suffixExists) {
return ((GrBinaryExpression) replaced).getLeftOperand();
}
if (prefixExists && !suffixExists) {
return ((GrBinaryExpression) replaced).getRightOperand();
}
if (!prefixExists && !suffixExists) {
return replaced;
}
} catch (ClassCastException c) {
throw new IncorrectOperationException(buffer.toString());
}
throw new IncorrectOperationException(buffer.toString());
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrBinaryExpression in project intellij-community by JetBrains.
the class GroovyPointlessBooleanInspection method createStringForNegatedExpression.
private static String createStringForNegatedExpression(GrExpression exp) {
if (ComparisonUtils.isComparison(exp)) {
final GrBinaryExpression binaryExpression = (GrBinaryExpression) exp;
final IElementType sign = binaryExpression.getOperationTokenType();
final String negatedComparison = ComparisonUtils.getNegatedComparison(sign);
final GrExpression lhs = binaryExpression.getLeftOperand();
final GrExpression rhs = binaryExpression.getRightOperand();
if (rhs == null) {
return lhs.getText() + negatedComparison;
}
return lhs.getText() + negatedComparison + rhs.getText();
} else {
final String baseText = exp.getText();
if (ParenthesesUtils.getPrecedence(exp) > ParenthesesUtils.PREFIX_PRECEDENCE) {
return "!(" + baseText + ')';
} else {
return '!' + baseText;
}
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrBinaryExpression in project intellij-community by JetBrains.
the class GroovyAssignmentCanBeOperatorAssignmentInspection method calculateReplacementExpression.
static String calculateReplacementExpression(GrAssignmentExpression expression) {
final GrExpression rhs = expression.getRValue();
final GrBinaryExpression binaryExpression = (GrBinaryExpression) PsiUtil.skipParentheses(rhs, false);
final GrExpression lhs = expression.getLValue();
assert binaryExpression != null;
final IElementType sign = binaryExpression.getOperationTokenType();
final GrExpression rhsRhs = binaryExpression.getRightOperand();
assert rhsRhs != null;
String signText = getTextForOperator(sign);
if ("&&".equals(signText)) {
signText = "&";
} else if ("||".equals(signText)) {
signText = "|";
}
return lhs.getText() + ' ' + signText + "= " + rhsRhs.getText();
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrBinaryExpression in project intellij-community by JetBrains.
the class DemorgansLawIntention method convertConjunctionExpression.
private static String convertConjunctionExpression(GrBinaryExpression exp, IElementType tokenType) {
final GrExpression lhs = exp.getLeftOperand();
final String lhsText;
if (isConjunctionExpression(lhs, tokenType)) {
lhsText = convertConjunctionExpression((GrBinaryExpression) lhs, tokenType);
} else {
lhsText = convertLeafExpression(lhs);
}
final GrExpression rhs = exp.getRightOperand();
final String rhsText;
if (isConjunctionExpression(rhs, tokenType)) {
rhsText = convertConjunctionExpression((GrBinaryExpression) rhs, tokenType);
} else {
rhsText = convertLeafExpression(rhs);
}
final String flippedConjunction;
if (tokenType.equals(GroovyTokenTypes.mLAND)) {
flippedConjunction = "||";
} else {
flippedConjunction = "&&";
}
return lhsText + flippedConjunction + rhsText;
}
Aggregations