use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral in project intellij-community by JetBrains.
the class ConvertIntegerToDecimalPredicate method satisfiedBy.
@Override
public boolean satisfiedBy(PsiElement element) {
if (!(element instanceof GrLiteral)) {
return false;
}
final GrLiteral expression = (GrLiteral) element;
final PsiType type = expression.getType();
if (type == null) {
return false;
}
if (!PsiType.INT.equals(type) && !PsiType.LONG.equals(type) && !type.equalsToText("java.lang.Integer") && !type.equalsToText("java.lang.Long")) {
return false;
}
@NonNls final String text = expression.getText().replaceAll("_", "");
if (text == null || text.length() < 2) {
return false;
}
if ("0".equals(text) || "0L".equals(text) || "0l".equals(text)) {
return false;
}
return text.charAt(0) == '0';
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral in project intellij-community by JetBrains.
the class ConvertIntegerToHexIntention method processIntention.
@Override
public void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
final GrLiteral exp = (GrLiteral) element;
String textString = exp.getText().replaceAll("_", "");
final int textLength = textString.length();
final char lastChar = textString.charAt(textLength - 1);
final boolean isLong = lastChar == 'l' || lastChar == 'L';
if (isLong) {
textString = textString.substring(0, textLength - 1);
}
final BigInteger val;
if (textString.startsWith("0b") || textString.startsWith("0B")) {
val = new BigInteger(textString.substring(2), 2);
} else if (textString.charAt(0) == '0') {
val = new BigInteger(textString, 8);
} else {
val = new BigInteger(textString, 10);
}
@NonNls String hexString = "0x" + val.toString(16);
if (isLong) {
hexString += 'L';
}
PsiImplUtil.replaceExpression(hexString, exp);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral in project intellij-community by JetBrains.
the class ConvertIntegerToOctalIntention method processIntention.
@Override
public void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
final GrLiteral exp = (GrLiteral) element;
@NonNls String textString = exp.getText().replaceAll("_", "");
final int textLength = textString.length();
final char lastChar = textString.charAt(textLength - 1);
final boolean isLong = lastChar == 'l' || lastChar == 'L';
if (isLong) {
textString = textString.substring(0, textLength - 1);
}
final BigInteger val;
if (textString.startsWith("0x") || textString.startsWith("0X")) {
final String rawTextString = textString.substring(2);
val = new BigInteger(rawTextString, 16);
} else if (textString.startsWith("0b") || textString.startsWith("0B")) {
final String rawTextString = textString.substring(2);
val = new BigInteger(rawTextString, 2);
} else {
val = new BigInteger(textString, 10);
}
String octString = '0' + val.toString(8);
if (isLong) {
octString += 'L';
}
PsiImplUtil.replaceExpression(octString, exp);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral in project intellij-community by JetBrains.
the class ConvertIntegerToOctalPredicate method satisfiedBy.
@Override
public boolean satisfiedBy(PsiElement element) {
if (!(element instanceof GrLiteral))
return false;
final GrLiteral expression = (GrLiteral) element;
final PsiType type = expression.getType();
if (type == null)
return false;
if (!PsiType.INT.equals(type) && !PsiType.LONG.equals(type) && !type.equalsToText("java.lang.Integer") && !type.equalsToText("java.lang.Long")) {
return false;
}
@NonNls final String text = expression.getText();
if (text == null || text.isEmpty()) {
return false;
}
if (text.startsWith("0x") || text.startsWith("0X")) {
return true;
}
if (text.startsWith("0b") || text.startsWith("0B")) {
return true;
}
if ("0".equals(text) || "0L".equals(text)) {
return false;
}
return text.charAt(0) != '0';
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral in project intellij-community by JetBrains.
the class ConvertStringToMultilineIntention method prepareNewLiteralText.
private static StringBuilder prepareNewLiteralText(List<GrLiteral> literals) {
String quote = !containsInjections(literals) && literals.get(0).getText().startsWith("'") ? "'''" : "\"\"\"";
final StringBuilder buffer = new StringBuilder();
buffer.append(quote);
for (GrLiteral literal : literals) {
if (literal instanceof GrLiteralImpl) {
appendSimpleStringValue(literal, buffer, quote);
} else {
final GrStringImpl gstring = (GrStringImpl) literal;
for (PsiElement child : gstring.getAllContentParts()) {
if (child instanceof GrStringContent) {
appendSimpleStringValue(child, buffer, "\"\"\"");
} else if (child instanceof GrStringInjection) {
buffer.append(child.getText());
}
}
}
}
buffer.append(quote);
return buffer;
}
Aggregations