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());
}
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());
}
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);
}
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);
}
}
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);
}
Aggregations