Search in sources :

Example 11 with GrLiteral

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral in project intellij-community by JetBrains.

the class ConvertToDollarSlashRegexIntention method processIntention.

@Override
protected void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
    if (!(element instanceof GrLiteral))
        return;
    String rawText = GrStringUtil.removeQuotes(element.getText());
    GrExpression newRegexp = GroovyPsiElementFactory.getInstance(project).createExpressionFromText("$/" + rawText + "/$");
    //don't use replaceWithExpression() since it can revert conversion if regexp brakes syntax
    element.replace(newRegexp);
}
Also used : GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrLiteral(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral)

Example 12 with GrLiteral

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral in project intellij-community by JetBrains.

the class RemoveUnnecessaryEscapeCharactersIntention method processIntention.

@Override
protected void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
    final Document document = editor.getDocument();
    final TextRange range = element.getTextRange();
    document.replaceString(range.getStartOffset(), range.getEndOffset(), removeUnnecessaryEscapeSymbols((GrLiteral) element));
}
Also used : TextRange(com.intellij.openapi.util.TextRange) Document(com.intellij.openapi.editor.Document) GrLiteral(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral)

Example 13 with GrLiteral

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral in project intellij-community by JetBrains.

the class ConvertIntegerToBinaryPredicate 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 false;
    }
    if ("0".equals(text) || "0L".equals(text)) {
        return true;
    }
    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 14 with GrLiteral

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral in project intellij-community by JetBrains.

the class ConvertIntegerToDecimalIntention 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 rawIntString = textString.substring(2);
        val = new BigInteger(rawIntString, 16);
    } else if (textString.startsWith("0b") || textString.startsWith("0B")) {
        final String rawString = textString.substring(2);
        val = new BigInteger(rawString, 2);
    } else {
        final String rawIntString = textString.substring(1);
        val = new BigInteger(rawIntString, 8);
    }
    String decimalString = val.toString(10);
    if (isLong) {
        decimalString += 'L';
    }
    PsiImplUtil.replaceExpression(decimalString, 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 15 with GrLiteral

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral in project intellij-community by JetBrains.

the class GradleDocumentationProvider method findDoc.

@Nullable
private static String findDoc(@Nullable PsiElement element, Object argValue) {
    String result = null;
    if (element instanceof GrLiteral) {
        GrLiteral grLiteral = (GrLiteral) element;
        PsiElement stmt = PsiTreeUtil.findFirstParent(grLiteral, psiElement -> psiElement instanceof GrCall);
        if (stmt instanceof GrCall) {
            GrCall grCall = (GrCall) stmt;
            PsiMethod psiMethod = grCall.resolveMethod();
            if (psiMethod != null && psiMethod.getContainingClass() != null) {
                //noinspection ConstantConditions
                String qualifiedName = psiMethod.getContainingClass().getQualifiedName();
                if (grLiteral.getParent() instanceof GrNamedArgument) {
                    GrNamedArgument namedArgument = (GrNamedArgument) grLiteral.getParent();
                    String key = StringUtil.join(new String[] { "gradle.documentation", qualifiedName, psiMethod.getName(), namedArgument.getLabelName(), String.valueOf(argValue) }, ".");
                    result = GradleDocumentationBundle.messageOrDefault(key, "");
                }
            }
        }
    }
    return result;
}
Also used : GrNamedArgument(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument) PsiMethod(com.intellij.psi.PsiMethod) GrCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCall) GrLiteral(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

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