Search in sources :

Example 16 with GrLiteral

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

the class PsiImplUtil method replaceExpression.

@Nullable
public static GrExpression replaceExpression(GrExpression oldExpr, GrExpression newExpr, boolean removeUnnecessaryParentheses) {
    PsiElement oldParent = oldExpr.getParent();
    if (oldParent == null)
        throw new PsiInvalidElementAccessException(oldExpr);
    if (!(oldExpr instanceof GrApplicationStatement)) {
        newExpr = ApplicationStatementUtil.convertToMethodCallExpression(newExpr);
    }
    // Remove unnecessary parentheses
    if (removeUnnecessaryParentheses && oldParent instanceof GrParenthesizedExpression && !(oldParent.getParent() instanceof GrArgumentLabel)) {
        return ((GrExpression) oldParent).replaceWithExpression(newExpr, true);
    }
    //regexes cannot be after identifier , try to replace it with simple string
    if (getRegexAtTheBeginning(newExpr) != null && isAfterIdentifier(oldExpr)) {
        final PsiElement copy = newExpr.copy();
        final GrLiteral regex = getRegexAtTheBeginning(copy);
        LOG.assertTrue(regex != null);
        final GrLiteral stringLiteral = GrStringUtil.createStringFromRegex(regex);
        if (regex == copy) {
            return oldExpr.replaceWithExpression(stringLiteral, removeUnnecessaryParentheses);
        } else {
            regex.replace(stringLiteral);
            return oldExpr.replaceWithExpression((GrExpression) copy, removeUnnecessaryParentheses);
        }
    }
    GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(oldExpr.getProject());
    if (oldParent instanceof GrStringInjection) {
        if (newExpr instanceof GrString || newExpr instanceof GrLiteral && ((GrLiteral) newExpr).getValue() instanceof String) {
            return GrStringUtil.replaceStringInjectionByLiteral((GrStringInjection) oldParent, (GrLiteral) newExpr);
        } else {
            GrClosableBlock block = factory.createClosureFromText("{foo}");
            oldParent.getNode().replaceChild(oldExpr.getNode(), block.getNode());
            GrStatement[] statements = block.getStatements();
            return ((GrExpression) statements[0]).replaceWithExpression(newExpr, removeUnnecessaryParentheses);
        }
    }
    if (PsiTreeUtil.getParentOfType(oldExpr, GrStringInjection.class, false, GrCodeBlock.class) != null) {
        final GrStringInjection stringInjection = PsiTreeUtil.getParentOfType(oldExpr, GrStringInjection.class);
        GrStringUtil.wrapInjection(stringInjection);
        assert stringInjection != null;
        final PsiElement replaced = oldExpr.replaceWithExpression(newExpr, removeUnnecessaryParentheses);
        return (GrExpression) replaced;
    }
    //check priorities    
    if (oldParent instanceof GrExpression && !(oldParent instanceof GrParenthesizedExpression)) {
        GrExpression addedParenth = checkPrecedence(newExpr, oldExpr) ? parenthesize(newExpr) : newExpr;
        if (newExpr != addedParenth) {
            return oldExpr.replaceWithExpression(addedParenth, removeUnnecessaryParentheses);
        }
    }
    //we should add the expression in arg list
    if (oldExpr instanceof GrClosableBlock && !(newExpr instanceof GrClosableBlock) && oldParent instanceof GrMethodCallExpression && ArrayUtil.contains(oldExpr, ((GrMethodCallExpression) oldParent).getClosureArguments())) {
        return ((GrMethodCallExpression) oldParent).replaceClosureArgument((GrClosableBlock) oldExpr, newExpr);
    }
    newExpr = (GrExpression) oldExpr.replace(newExpr);
    // to find target parenthesised expression.
    if (newExpr instanceof GrParenthesizedExpression && isFirstChild(newExpr)) {
        int parentCount = 0;
        PsiElement element = oldParent;
        while (element != null && !(element instanceof GrCommandArgumentList)) {
            if (element instanceof GrCodeBlock || element instanceof GrParenthesizedExpression)
                break;
            if (element instanceof PsiFile)
                break;
            final PsiElement parent = element.getParent();
            if (parent == null)
                break;
            if (!isFirstChild(element))
                break;
            element = parent;
            parentCount++;
        }
        if (element instanceof GrCommandArgumentList) {
            final GrCommandArgumentList commandArgList = (GrCommandArgumentList) element;
            final PsiElement parent = commandArgList.getParent();
            LOG.assertTrue(parent instanceof GrApplicationStatement);
            final GrMethodCall methodCall = factory.createMethodCallByAppCall((GrApplicationStatement) parent);
            final GrMethodCall newCall = (GrMethodCall) parent.replace(methodCall);
            PsiElement result = newCall.getArgumentList().getAllArguments()[0];
            for (int i = 0; i < parentCount; i++) {
                result = PsiUtil.skipWhitespacesAndComments(result.getFirstChild(), true);
            }
            LOG.assertTrue(result instanceof GrParenthesizedExpression);
            return (GrExpression) result;
        }
    }
    return newExpr;
}
Also used : GrArgumentLabel(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentLabel) GrString(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrString) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrString(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrString) GrStringInjection(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrStringInjection) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement) GrMethodCallExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression) GrLiteral(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral) GrCodeBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock) Nullable(org.jetbrains.annotations.Nullable)

Example 17 with GrLiteral

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

the class GrLiteralImpl method updateText.

@Override
public GrLiteralImpl updateText(@NotNull final String text) {
    final GrExpression newExpr = GroovyPsiElementFactory.getInstance(getProject()).createExpressionFromText(text);
    LOG.assertTrue(newExpr instanceof GrLiteral, text);
    LOG.assertTrue(newExpr.getFirstChild() != null, text);
    final ASTNode valueNode = getNode().getFirstChildNode();
    getNode().replaceChild(valueNode, newExpr.getFirstChild().getNode());
    return this;
}
Also used : ASTNode(com.intellij.lang.ASTNode) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrLiteral(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral)

Example 18 with GrLiteral

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

the class DefaultCallExpressionTypeCalculator method getClosureMethodsReturnType.

@Nullable
private static PsiType getClosureMethodsReturnType(GrMethodCall callExpression, GrReferenceExpression refExpr, PsiMethod resolved) {
    PsiClass clazz = resolved.getContainingClass();
    if (clazz == null || !GroovyCommonClassNames.GROOVY_LANG_CLOSURE.equals(clazz.getQualifiedName()))
        return null;
    if (!CLOSURE_METHODS.contains(resolved.getName()))
        return null;
    GrExpression qualifier = refExpr.getQualifierExpression();
    if (qualifier == null)
        return null;
    PsiType qType = qualifier.getType();
    if (!(qType instanceof GrClosureType))
        return null;
    if ("call".equals(resolved.getName())) {
        return GrClosureSignatureUtil.getReturnType(((GrClosureType) qType).getSignature(), callExpression);
    } else if ("curry".equals(resolved.getName()) || "trampoline".equals(resolved.getName())) {
        return ((GrClosureType) qType).curry(PsiUtil.getArgumentTypes(refExpr, false), 0, callExpression);
    } else if ("memoize".equals(resolved.getName())) {
        return qType;
    } else if ("rcurry".equals(resolved.getName())) {
        return ((GrClosureType) qType).curry(PsiUtil.getArgumentTypes(refExpr, false), -1, callExpression);
    } else if ("ncurry".equals(resolved.getName())) {
        final GrArgumentList argList = callExpression.getArgumentList();
        final GrExpression[] arguments = argList.getExpressionArguments();
        if (arguments.length > 0) {
            final GrExpression first = arguments[0];
            if (first instanceof GrLiteral) {
                final Object value = ((GrLiteral) first).getValue();
                if (value instanceof Integer) {
                    final PsiType[] argTypes = PsiUtil.getArgumentTypes(refExpr, false);
                    if (argTypes != null) {
                        return ((GrClosureType) qType).curry(ArrayUtil.remove(argTypes, 0), (Integer) value, callExpression);
                    }
                }
            }
        }
        return qType;
    }
    return null;
}
Also used : GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrLiteral(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral) GrClosureType(org.jetbrains.plugins.groovy.lang.psi.impl.GrClosureType) Nullable(org.jetbrains.annotations.Nullable)

Example 19 with GrLiteral

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

the class GrCharConverter method checkSingleSymbolLiteral.

public static boolean checkSingleSymbolLiteral(GroovyPsiElement context) {
    final GrLiteral literal = getLiteral(context);
    final Object value = literal == null ? null : literal.getValue();
    return value != null && value.toString().length() == 1;
}
Also used : GrLiteral(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral)

Example 20 with GrLiteral

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

the class GrClassConverter method isConvertibleEx.

@Nullable
@Override
public ConversionResult isConvertibleEx(@NotNull PsiType targetType, @NotNull PsiType actualType, @NotNull GroovyPsiElement context, @NotNull ApplicableTo currentPosition) {
    if (!(targetType instanceof PsiClassType) || !((PsiClassType) targetType).rawType().equalsToText(CommonClassNames.JAVA_LANG_CLASS)) {
        return null;
    }
    if (actualType == PsiType.NULL)
        return ConversionResult.OK;
    final GrLiteral literal = getLiteral(context);
    final Object value = literal == null ? null : literal.getValue();
    final String fqn = value == null ? null : value.toString();
    final PsiClass psiClass = fqn == null ? null : JavaPsiFacade.getInstance(context.getProject()).findClass(fqn, context.getResolveScope());
    return psiClass == null ? ConversionResult.WARNING : ConversionResult.OK;
}
Also used : GrLiteral(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral) 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