Search in sources :

Example 51 with PsiExpression

use of com.intellij.psi.PsiExpression in project intellij-community by JetBrains.

the class ConstantExpressionIntention method processIntention.

@Override
public void processIntention(PsiElement element) throws IncorrectOperationException {
    final PsiExpression expression = (PsiExpression) element;
    final Object value = ExpressionUtils.computeConstantExpression(expression);
    @NonNls final String newExpression;
    if (value instanceof String) {
        final String string = (String) value;
        newExpression = '"' + StringUtil.escapeStringCharacters(string) + '"';
    } else if (value instanceof Character) {
        newExpression = '\'' + StringUtil.escapeStringCharacters(value.toString()) + '\'';
    } else if (value instanceof Long) {
        newExpression = value.toString() + 'L';
    } else if (value instanceof Double) {
        final double v = ((Double) value).doubleValue();
        if (Double.isNaN(v)) {
            newExpression = "java.lang.Double.NaN";
        } else if (Double.isInfinite(v)) {
            if (v > 0.0) {
                newExpression = "java.lang.Double.POSITIVE_INFINITY";
            } else {
                newExpression = "java.lang.Double.NEGATIVE_INFINITY";
            }
        } else {
            newExpression = Double.toString(v);
        }
    } else if (value instanceof Float) {
        final float v = ((Float) value).floatValue();
        if (Float.isNaN(v)) {
            newExpression = "java.lang.Float.NaN";
        } else if (Float.isInfinite(v)) {
            if (v > 0.0F) {
                newExpression = "java.lang.Float.POSITIVE_INFINITY";
            } else {
                newExpression = "java.lang.Float.NEGATIVE_INFINITY";
            }
        } else {
            newExpression = Float.toString(v) + 'f';
        }
    } else if (value == null) {
        newExpression = "null";
    } else {
        newExpression = String.valueOf(value);
    }
    PsiReplacementUtil.replaceExpression(expression, newExpression);
}
Also used : NonNls(org.jetbrains.annotations.NonNls) PsiExpression(com.intellij.psi.PsiExpression)

Example 52 with PsiExpression

use of com.intellij.psi.PsiExpression in project intellij-community by JetBrains.

the class ExpressionPredicate method satisfiedBy.

public boolean satisfiedBy(PsiElement element) {
    if (!(element instanceof PsiJavaToken)) {
        return false;
    }
    final PsiElement parent = element.getParent();
    if (!(parent instanceof PsiPolyadicExpression)) {
        return false;
    }
    final PsiPolyadicExpression expression = (PsiPolyadicExpression) parent;
    final PsiExpression[] operands = expression.getOperands();
    if (operands.length < 2) {
        return false;
    }
    PsiExpression prevOperand = null;
    for (PsiExpression operand : operands) {
        final PsiJavaToken token = expression.getTokenBeforeOperand(operand);
        if (element == token) {
            if (prevOperand == null || operand.getText().equals(prevOperand.getText())) {
                return false;
            }
            break;
        }
        prevOperand = operand;
    }
    return !ComparisonUtils.isComparison(expression);
}
Also used : PsiJavaToken(com.intellij.psi.PsiJavaToken) PsiExpression(com.intellij.psi.PsiExpression) PsiPolyadicExpression(com.intellij.psi.PsiPolyadicExpression) PsiElement(com.intellij.psi.PsiElement)

Example 53 with PsiExpression

use of com.intellij.psi.PsiExpression in project intellij-community by JetBrains.

the class FlipExpressionIntention method getTextForElement.

@Override
public String getTextForElement(PsiElement element) {
    final PsiPolyadicExpression expression = (PsiPolyadicExpression) element.getParent();
    final PsiExpression[] operands = expression.getOperands();
    final PsiJavaToken sign = expression.getTokenBeforeOperand(operands[1]);
    final String operatorText = sign == null ? "" : sign.getText();
    final IElementType tokenType = expression.getOperationTokenType();
    final boolean commutative = ParenthesesUtils.isCommutativeOperator(tokenType);
    if (commutative && !ExpressionUtils.isConcatenation(expression)) {
        return IntentionPowerPackBundle.message("flip.smth.intention.name", operatorText);
    } else {
        return IntentionPowerPackBundle.message("flip.smth.intention.name1", operatorText);
    }
}
Also used : IElementType(com.intellij.psi.tree.IElementType) PsiJavaToken(com.intellij.psi.PsiJavaToken) PsiExpression(com.intellij.psi.PsiExpression) PsiPolyadicExpression(com.intellij.psi.PsiPolyadicExpression)

Example 54 with PsiExpression

use of com.intellij.psi.PsiExpression in project intellij-community by JetBrains.

the class JavaPolyadicExpressionUnwrapper method doUnwrap.

@Override
protected void doUnwrap(PsiElement element, Context context) throws IncorrectOperationException {
    final PsiPolyadicExpression parent = (PsiPolyadicExpression) element.getParent();
    final PsiExpression operand = findOperand(element, parent);
    if (operand == null) {
        return;
    }
    context.extractElement(operand, parent);
    context.delete(parent);
}
Also used : PsiExpression(com.intellij.psi.PsiExpression) PsiPolyadicExpression(com.intellij.psi.PsiPolyadicExpression)

Example 55 with PsiExpression

use of com.intellij.psi.PsiExpression in project intellij-community by JetBrains.

the class GuessElementTypeMacro method guessTypes.

@Nullable
private static PsiType[] guessTypes(Expression[] params, ExpressionContext context) {
    if (params.length != 1)
        return null;
    final Result result = params[0].calculateResult(context);
    if (result == null)
        return null;
    Project project = context.getProject();
    PsiExpression expr = MacroUtil.resultToPsiExpression(result, context);
    if (expr == null)
        return null;
    PsiType[] types = GuessManager.getInstance(project).guessContainerElementType(expr, new TextRange(context.getTemplateStartOffset(), context.getTemplateEndOffset()));
    for (int i = 0; i < types.length; i++) {
        types[i] = GenericsUtil.getVariableTypeByExpressionType(types[i]);
    }
    return types;
}
Also used : Project(com.intellij.openapi.project.Project) PsiExpression(com.intellij.psi.PsiExpression) TextRange(com.intellij.openapi.util.TextRange) PsiType(com.intellij.psi.PsiType) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

PsiExpression (com.intellij.psi.PsiExpression)67 PsiElement (com.intellij.psi.PsiElement)24 PsiReferenceExpression (com.intellij.psi.PsiReferenceExpression)11 PsiMethodCallExpression (com.intellij.psi.PsiMethodCallExpression)8 NonNls (org.jetbrains.annotations.NonNls)8 PsiLocalVariable (com.intellij.psi.PsiLocalVariable)7 PsiType (com.intellij.psi.PsiType)7 PsiAssignmentExpression (com.intellij.psi.PsiAssignmentExpression)6 PsiExpressionStatement (com.intellij.psi.PsiExpressionStatement)6 PsiPolyadicExpression (com.intellij.psi.PsiPolyadicExpression)6 PsiStatement (com.intellij.psi.PsiStatement)6 Nullable (org.jetbrains.annotations.Nullable)6 PsiBinaryExpression (com.intellij.psi.PsiBinaryExpression)5 PsiClass (com.intellij.psi.PsiClass)5 PsiDeclarationStatement (com.intellij.psi.PsiDeclarationStatement)5 PsiElementFactory (com.intellij.psi.PsiElementFactory)5 PsiField (com.intellij.psi.PsiField)5 PsiReference (com.intellij.psi.PsiReference)5 Nullable (com.android.annotations.Nullable)4 Project (com.intellij.openapi.project.Project)4