Search in sources :

Example 6 with GrLiteral

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';
}
Also used : NonNls(org.jetbrains.annotations.NonNls) GrLiteral(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral) PsiType(com.intellij.psi.PsiType)

Example 7 with GrLiteral

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);
}
Also used : NonNls(org.jetbrains.annotations.NonNls) BigInteger(java.math.BigInteger) GrLiteral(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral)

Example 8 with GrLiteral

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);
}
Also used : NonNls(org.jetbrains.annotations.NonNls) BigInteger(java.math.BigInteger) GrLiteral(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral)

Example 9 with GrLiteral

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';
}
Also used : NonNls(org.jetbrains.annotations.NonNls) GrLiteral(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral) PsiType(com.intellij.psi.PsiType)

Example 10 with GrLiteral

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;
}
Also used : GrStringContent(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrStringContent) GrLiteralImpl(org.jetbrains.plugins.groovy.lang.psi.impl.statements.expressions.literals.GrLiteralImpl) GrStringImpl(org.jetbrains.plugins.groovy.lang.psi.impl.statements.expressions.literals.GrStringImpl) GrString(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrString) GrStringInjection(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrStringInjection) GrLiteral(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral) PsiElement(com.intellij.psi.PsiElement)

Aggregations

GrLiteral (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral)45 Nullable (org.jetbrains.annotations.Nullable)14 PsiElement (com.intellij.psi.PsiElement)13 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)10 NonNls (org.jetbrains.annotations.NonNls)9 PsiType (com.intellij.psi.PsiType)6 GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)6 GrString (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrString)6 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)5 TextRange (com.intellij.openapi.util.TextRange)4 BigInteger (java.math.BigInteger)4 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)4 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)4 GrStringInjection (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrStringInjection)4 Document (com.intellij.openapi.editor.Document)3 Project (com.intellij.openapi.project.Project)3 VirtualFile (com.intellij.openapi.vfs.VirtualFile)3 GrNamedArgument (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument)3 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)3 GrMethodCallExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression)3