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