use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrBinaryExpression in project intellij-community by JetBrains.
the class BoolUtils method getNegatedExpressionText.
public static String getNegatedExpressionText(@NotNull GrExpression condition) {
if (condition instanceof GrParenthesizedExpression) {
final GrExpression contentExpression = ((GrParenthesizedExpression) condition).getOperand();
if (contentExpression == null)
return "()";
return '(' + getNegatedExpressionText(contentExpression) + ')';
} else if (isNegation(condition)) {
final GrExpression negated = getNegated(condition);
return negated.getText();
} else if (ComparisonUtils.isComparison(condition)) {
final GrBinaryExpression binaryExpression = (GrBinaryExpression) condition;
final IElementType sign = binaryExpression.getOperationTokenType();
final String negatedComparison = ComparisonUtils.getNegatedComparison(sign);
final GrExpression lhs = binaryExpression.getLeftOperand();
final GrExpression rhs = binaryExpression.getRightOperand();
assert rhs != null;
return lhs.getText() + negatedComparison + rhs.getText();
} else if (ParenthesesUtils.getPrecedence(condition) > ParenthesesUtils.PREFIX_PRECEDENCE) {
return "!(" + condition.getText() + ')';
} else {
return '!' + condition.getText();
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrBinaryExpression in project intellij-community by JetBrains.
the class SpockUtils method splitOr.
// See org.spockframework.compiler.WhereBlockRewriter#splitRow()
private static void splitOr(List<GrExpression> res, GrExpression element) {
if (isOrStatement(element)) {
GrBinaryExpression be = (GrBinaryExpression) element;
splitOr(res, be.getLeftOperand());
splitOr(res, be.getRightOperand());
} else {
res.add(element);
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrBinaryExpression in project intellij-community by JetBrains.
the class ConvertConcatenationToGstringIntention method getOperandText.
private static void getOperandText(@Nullable GrExpression operand, StringBuilder builder, boolean multiline) {
if (operand instanceof GrRegex) {
for (GroovyPsiElement element : ((GrRegex) operand).getAllContentParts()) {
if (element instanceof GrStringInjection) {
builder.append(element.getText());
} else if (element instanceof GrStringContent) {
if (GrStringUtil.isDollarSlashyString((GrLiteral) operand)) {
processDollarSlashyContent(builder, multiline, element.getText());
} else {
processSlashyContent(builder, multiline, element.getText());
}
}
}
} else if (operand instanceof GrString) {
boolean isMultiline = GrStringUtil.isMultilineStringLiteral((GrLiteral) operand);
for (GroovyPsiElement element : ((GrString) operand).getAllContentParts()) {
if (element instanceof GrStringInjection) {
builder.append(element.getText());
} else if (element instanceof GrStringContent) {
if (isMultiline) {
processMultilineGString(builder, element.getText());
} else {
processSinglelineGString(builder, element.getText());
}
}
}
} else if (operand instanceof GrLiteral) {
String text = GrStringUtil.removeQuotes(operand.getText());
GrLiteral literal = (GrLiteral) operand;
if (GrStringUtil.isSingleQuoteString(literal)) {
processSinglelineString(builder, text);
} else if (GrStringUtil.isTripleQuoteString(literal)) {
processMultilineString(builder, text);
} else if (GrStringUtil.isDoubleQuoteString(literal)) {
processSinglelineGString(builder, text);
} else if (GrStringUtil.isTripleDoubleQuoteString(literal)) {
processMultilineGString(builder, text);
} else if (GrStringUtil.isSlashyString(literal)) {
processSlashyContent(builder, multiline, text);
} else if (GrStringUtil.isDollarSlashyString(literal)) {
processDollarSlashyContent(builder, multiline, text);
}
} else if (MyPredicate.satisfied(operand)) {
performIntention((GrBinaryExpression) operand, builder, multiline);
} else if (isToStringMethod(operand, builder)) {
//nothing to do
} else {
builder.append(START_BRACE).append(operand == null ? "" : operand.getText()).append(END_BRACE);
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrBinaryExpression in project intellij-community by JetBrains.
the class ConvertConcatenationToGstringIntention method invokeImpl.
private static void invokeImpl(final PsiElement element, Document document) {
boolean isMultiline = containsMultilineStrings((GrExpression) element);
StringBuilder builder = new StringBuilder(element.getTextLength());
if (element instanceof GrBinaryExpression) {
performIntention((GrBinaryExpression) element, builder, isMultiline);
} else if (element instanceof GrLiteral) {
getOperandText((GrExpression) element, builder, isMultiline);
} else {
return;
}
String text = builder.toString();
final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(element.getProject());
final GrExpression newExpr = factory.createExpressionFromText(GrStringUtil.addQuotes(text, true));
CommandProcessor.getInstance().executeCommand(element.getProject(), () -> WriteAction.run(() -> {
final GrExpression expression = ((GrExpression) element).replaceWithExpression(newExpr, true);
if (expression instanceof GrString) {
GrStringUtil.removeUnnecessaryBracesInGString((GrString) expression);
}
}), null, null, document);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrBinaryExpression in project intellij-community by JetBrains.
the class ConvertGStringToStringIntention method prepareExpression.
private static String prepareExpression(GrExpression expr) {
if (PsiUtil.isThisOrSuperRef(expr))
return expr.getText();
String text = expr.getText();
final PsiType type = expr.getType();
if (type != null && CommonClassNames.JAVA_LANG_STRING.equals(type.getCanonicalText())) {
if (expr instanceof GrBinaryExpression && GroovyTokenTypes.mPLUS.equals(((GrBinaryExpression) expr).getOperationTokenType())) {
return '(' + text + ')';
} else {
return text;
}
} else {
return "String.valueOf(" + text + ")";
}
}
Aggregations