Search in sources :

Example 1 with PsiJavaToken

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

the class ConstantSubexpressionIntention method processIntention.

@Override
public void processIntention(@NotNull PsiElement element) throws IncorrectOperationException {
    final PsiJavaToken token;
    if (element instanceof PsiJavaToken) {
        token = (PsiJavaToken) element;
    } else {
        final PsiElement prevSibling = element.getPrevSibling();
        if (prevSibling instanceof PsiJavaToken) {
            token = (PsiJavaToken) prevSibling;
        } else {
            throw new AssertionError();
        }
    }
    final PsiPolyadicExpression polyadicExpression = (PsiPolyadicExpression) element.getParent();
    final PsiPolyadicExpression subexpression = ConstantSubexpressionPredicate.getSubexpression(polyadicExpression, token);
    if (subexpression == null) {
        return;
    }
    final Object value = ExpressionUtils.computeConstantExpression(subexpression);
    @NonNls final StringBuilder newExpressionText = new StringBuilder();
    final PsiExpression[] operands = polyadicExpression.getOperands();
    PsiExpression prevOperand = null;
    PsiJavaToken prevToken = null;
    for (PsiExpression operand : operands) {
        final PsiJavaToken currentToken = polyadicExpression.getTokenBeforeOperand(operand);
        if (token == currentToken) {
            if (prevToken != null) {
                newExpressionText.append(prevToken.getText());
            }
            if (newExpressionText.length() > 0) {
                newExpressionText.append(' ');
            }
            if (value instanceof Long) {
                newExpressionText.append(value).append('L');
            } else if (value instanceof Double) {
                final double v = ((Double) value).doubleValue();
                if (Double.isNaN(v)) {
                    newExpressionText.append("java.lang.Double.NaN");
                } else if (Double.isInfinite(v)) {
                    if (v > 0.0) {
                        newExpressionText.append("java.lang.Double.POSITIVE_INFINITY");
                    } else {
                        newExpressionText.append("java.lang.Double.NEGATIVE_INFINITY");
                    }
                } else {
                    newExpressionText.append(Double.toString(v));
                }
            } else if (value instanceof Float) {
                final float v = ((Float) value).floatValue();
                if (Float.isNaN(v)) {
                    newExpressionText.append("java.lang.Float.NaN");
                } else if (Float.isInfinite(v)) {
                    if (v > 0.0F) {
                        newExpressionText.append("java.lang.Float.POSITIVE_INFINITY");
                    } else {
                        newExpressionText.append("java.lang.Float.NEGATIVE_INFINITY");
                    }
                } else {
                    newExpressionText.append(Float.toString(v)).append('f');
                }
            } else {
                newExpressionText.append(value);
            }
            prevOperand = null;
            prevToken = null;
        } else {
            if (prevToken != null) {
                newExpressionText.append(prevToken.getText());
            }
            if (prevOperand != null) {
                newExpressionText.append(prevOperand.getText());
            }
            prevOperand = operand;
            prevToken = currentToken;
        }
    }
    if (prevToken != null) {
        newExpressionText.append(prevToken.getText());
    }
    if (prevOperand != null) {
        newExpressionText.append(prevOperand.getText());
    }
    PsiReplacementUtil.replaceExpression(polyadicExpression, newExpressionText.toString());
}
Also used : NonNls(org.jetbrains.annotations.NonNls) PsiJavaToken(com.intellij.psi.PsiJavaToken) PsiExpression(com.intellij.psi.PsiExpression) PsiPolyadicExpression(com.intellij.psi.PsiPolyadicExpression) PsiElement(com.intellij.psi.PsiElement)

Example 2 with PsiJavaToken

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

the class FlipExpressionIntention method processIntention.

@Override
public void processIntention(@NotNull PsiElement element) {
    final PsiJavaToken token = (PsiJavaToken) element;
    final PsiElement parent = token.getParent();
    if (!(parent instanceof PsiPolyadicExpression)) {
        return;
    }
    final PsiPolyadicExpression polyadicExpression = (PsiPolyadicExpression) parent;
    final PsiExpression[] operands = polyadicExpression.getOperands();
    final StringBuilder newExpression = new StringBuilder();
    String prevOperand = null;
    // 2- -1 without the space is not legal
    final String tokenText = token.getText() + ' ';
    for (PsiExpression operand : operands) {
        final PsiJavaToken token1 = polyadicExpression.getTokenBeforeOperand(operand);
        if (token == token1) {
            newExpression.append(operand.getText()).append(tokenText);
            continue;
        }
        if (prevOperand != null) {
            newExpression.append(prevOperand).append(tokenText);
        }
        prevOperand = operand.getText();
    }
    newExpression.append(prevOperand);
    PsiReplacementUtil.replaceExpression(polyadicExpression, newExpression.toString());
}
Also used : PsiJavaToken(com.intellij.psi.PsiJavaToken) PsiExpression(com.intellij.psi.PsiExpression) PsiPolyadicExpression(com.intellij.psi.PsiPolyadicExpression) PsiElement(com.intellij.psi.PsiElement)

Example 3 with PsiJavaToken

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

the class ForLoopPredicate method satisfiedBy.

public boolean satisfiedBy(PsiElement element) {
    if (!(element instanceof PsiJavaToken)) {
        return false;
    }
    final PsiJavaToken token = (PsiJavaToken) element;
    final IElementType tokenType = token.getTokenType();
    if (!JavaTokenType.FOR_KEYWORD.equals(tokenType)) {
        return false;
    }
    final PsiElement parent = element.getParent();
    return parent instanceof PsiForStatement && !ErrorUtil.containsError(parent);
}
Also used : IElementType(com.intellij.psi.tree.IElementType) PsiJavaToken(com.intellij.psi.PsiJavaToken) PsiForStatement(com.intellij.psi.PsiForStatement) PsiElement(com.intellij.psi.PsiElement)

Example 4 with PsiJavaToken

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

the class FlipComparisonIntention method getTextForElement.

public String getTextForElement(PsiElement element) {
    String operatorText = "";
    String flippedOperatorText = "";
    final PsiBinaryExpression expression = (PsiBinaryExpression) element;
    if (expression != null) {
        final PsiJavaToken sign = expression.getOperationSign();
        operatorText = sign.getText();
        flippedOperatorText = ComparisonUtils.getFlippedComparison(sign.getTokenType());
    }
    if (operatorText.equals(flippedOperatorText)) {
        return IntentionPowerPackBundle.message("flip.smth.intention.name", operatorText);
    } else {
        return IntentionPowerPackBundle.message("flip.comparison.intention.name", operatorText, flippedOperatorText);
    }
}
Also used : PsiJavaToken(com.intellij.psi.PsiJavaToken) PsiBinaryExpression(com.intellij.psi.PsiBinaryExpression)

Example 5 with PsiJavaToken

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

the class DoWhileLoopPredicate method satisfiedBy.

public boolean satisfiedBy(PsiElement element) {
    if (!(element instanceof PsiJavaToken)) {
        return false;
    }
    final PsiJavaToken token = (PsiJavaToken) element;
    final IElementType tokenType = token.getTokenType();
    if (!JavaTokenType.DO_KEYWORD.equals(tokenType)) {
        return false;
    }
    final PsiElement parent = element.getParent();
    if (!(parent instanceof PsiDoWhileStatement)) {
        return false;
    }
    final PsiDoWhileStatement doWhileStatement = (PsiDoWhileStatement) parent;
    return !(doWhileStatement.getCondition() == null || doWhileStatement.getBody() == null);
}
Also used : IElementType(com.intellij.psi.tree.IElementType) PsiJavaToken(com.intellij.psi.PsiJavaToken) PsiDoWhileStatement(com.intellij.psi.PsiDoWhileStatement) PsiElement(com.intellij.psi.PsiElement)

Aggregations

PsiJavaToken (com.intellij.psi.PsiJavaToken)19 PsiElement (com.intellij.psi.PsiElement)12 PsiPolyadicExpression (com.intellij.psi.PsiPolyadicExpression)5 PsiExpression (com.intellij.psi.PsiExpression)4 PsiIfStatement (com.intellij.psi.PsiIfStatement)4 IElementType (com.intellij.psi.tree.IElementType)4 Document (com.intellij.openapi.editor.Document)2 TextRange (com.intellij.openapi.util.TextRange)2 PsiBinaryExpression (com.intellij.psi.PsiBinaryExpression)2 PsiForStatement (com.intellij.psi.PsiForStatement)2 PsiStatement (com.intellij.psi.PsiStatement)2 PsiWhileStatement (com.intellij.psi.PsiWhileStatement)2 PsiAssignmentExpression (com.intellij.psi.PsiAssignmentExpression)1 PsiBlockStatement (com.intellij.psi.PsiBlockStatement)1 PsiCodeBlock (com.intellij.psi.PsiCodeBlock)1 PsiDoWhileStatement (com.intellij.psi.PsiDoWhileStatement)1 PsiForeachStatement (com.intellij.psi.PsiForeachStatement)1 PsiSwitchStatement (com.intellij.psi.PsiSwitchStatement)1 PsiTypeCastExpression (com.intellij.psi.PsiTypeCastExpression)1 PsiElementPredicate (com.siyeh.ipp.base.PsiElementPredicate)1