Search in sources :

Example 46 with GrClosableBlock

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock 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 47 with GrClosableBlock

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock in project intellij-community by JetBrains.

the class GrClosureSignatureUtil method mapParametersToArguments.

@Nullable
public static ArgInfo<PsiElement>[] mapParametersToArguments(@NotNull GrClosureSignature signature, @NotNull GrNamedArgument[] namedArgs, @NotNull GrExpression[] expressionArgs, @NotNull GrClosableBlock[] closureArguments, @NotNull PsiElement context, boolean partial, boolean eraseArgs) {
    List<InnerArg> innerArgs = new ArrayList<>();
    boolean hasNamedArgs = namedArgs.length > 0;
    GrClosureParameter[] params = signature.getParameters();
    if (hasNamedArgs) {
        if (params.length == 0)
            return null;
        PsiType type = params[0].getType();
        if (InheritanceUtil.isInheritor(type, CommonClassNames.JAVA_UTIL_MAP) || type == null || type.equalsToText(CommonClassNames.JAVA_LANG_OBJECT)) {
            innerArgs.add(new InnerArg(GrMapType.create(context.getResolveScope()), namedArgs));
        } else {
            return null;
        }
    }
    for (GrExpression expression : expressionArgs) {
        PsiType type = expression.getType();
        if (partial && expression instanceof GrNewExpression && com.intellij.psi.util.PsiUtil.resolveClassInType(type) == null) {
            type = null;
        }
        if (eraseArgs) {
            type = TypeConversionUtil.erasure(type);
        }
        innerArgs.add(new InnerArg(type, expression));
    }
    for (GrClosableBlock closureArgument : closureArguments) {
        innerArgs.add(new InnerArg(TypeConversionUtil.erasure(closureArgument.getType()), closureArgument));
    }
    return mapParametersToArguments(signature, innerArgs, hasNamedArgs, partial, context);
}
Also used : GrNewExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrNewExpression) GrClosureParameter(org.jetbrains.plugins.groovy.lang.psi.api.types.GrClosureParameter) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) Nullable(org.jetbrains.annotations.Nullable)

Example 48 with GrClosableBlock

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock in project intellij-community by JetBrains.

the class GrClosureSignatureUtil method mapArgumentsToParameters.

@Nullable
public static Map<GrExpression, Pair<PsiParameter, PsiType>> mapArgumentsToParameters(@NotNull GroovyResolveResult resolveResult, @NotNull PsiElement context, final boolean partial, final boolean eraseArgs, @NotNull final GrNamedArgument[] namedArgs, @NotNull final GrExpression[] expressionArgs, @NotNull GrClosableBlock[] closureArguments) {
    final GrClosureSignature signature;
    final PsiParameter[] parameters;
    final PsiElement element = resolveResult.getElement();
    final PsiSubstitutor substitutor = resolveResult.getSubstitutor();
    if (element instanceof PsiMethod) {
        signature = createSignature((PsiMethod) element, substitutor, eraseArgs);
        parameters = ((PsiMethod) element).getParameterList().getParameters();
    } else if (element instanceof GrClosableBlock) {
        signature = eraseArgs ? createSignatureWithErasedParameterTypes((GrClosableBlock) element) : createSignature(((GrClosableBlock) element));
        parameters = ((GrClosableBlock) element).getAllParameters();
    } else {
        return null;
    }
    final ArgInfo<PsiElement>[] argInfos = mapParametersToArguments(signature, namedArgs, expressionArgs, closureArguments, context, partial, eraseArgs);
    if (argInfos == null) {
        return null;
    }
    final HashMap<GrExpression, Pair<PsiParameter, PsiType>> result = new HashMap<>();
    for (int i = 0; i < argInfos.length; i++) {
        ArgInfo<PsiElement> info = argInfos[i];
        if (info == null)
            continue;
        for (PsiElement arg : info.args) {
            if (arg instanceof GrNamedArgument) {
                arg = ((GrNamedArgument) arg).getExpression();
            }
            final GrExpression expression = (GrExpression) arg;
            PsiType type = parameters[i].getType();
            if (info.isMultiArg && type instanceof PsiArrayType) {
                type = ((PsiArrayType) type).getComponentType();
            }
            result.put(expression, Pair.create(parameters[i], substitutor.substitute(type)));
        }
    }
    return result;
}
Also used : GrNamedArgument(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrClosureSignature(org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) Pair(com.intellij.openapi.util.Pair) Nullable(org.jetbrains.annotations.Nullable)

Example 49 with GrClosableBlock

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock in project intellij-community by JetBrains.

the class GrClosableBlockImpl method getOwner.

private PsiVariable getOwner() {
    return CachedValuesManager.getCachedValue(this, () -> {
        final GroovyPsiElement context = PsiTreeUtil.getParentOfType(this, GrTypeDefinition.class, GrClosableBlock.class, GroovyFile.class);
        final PsiElementFactory factory = JavaPsiFacade.getInstance(getProject()).getElementFactory();
        PsiType type = null;
        if (context instanceof GrTypeDefinition) {
            type = factory.createType((PsiClass) context);
        } else if (context instanceof GrClosableBlock) {
            type = GrClosureType.create((GrClosableBlock) context, true);
        } else if (context instanceof GroovyFile) {
            final PsiClass scriptClass = ((GroovyFile) context).getScriptClass();
            if (scriptClass != null && GroovyNamesUtil.isIdentifier(scriptClass.getName()))
                type = factory.createType(scriptClass);
        }
        if (type == null) {
            type = TypesUtil.getJavaLangObject(this);
        }
        PsiVariable owner = new GrLightVariable(getManager(), OWNER_NAME, type, this);
        return CachedValueProvider.Result.create(owner, PsiModificationTracker.MODIFICATION_COUNT);
    });
}
Also used : GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrLightVariable(org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GrLightVariable) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile)

Example 50 with GrClosableBlock

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock in project intellij-community by JetBrains.

the class GrStaticChecker method isInStaticContext.

public static boolean isInStaticContext(@NotNull PsiElement place, @Nullable PsiClass targetClass) {
    if (place instanceof GrReferenceExpression) {
        GrQualifiedReference reference = (GrQualifiedReference) place;
        PsiElement qualifier = reference.getQualifier();
        if (qualifier != null && !PsiUtil.isThisOrSuperRef(reference)) {
            if (PsiUtil.isInstanceThisRef(qualifier) || PsiUtil.isSuperReference(qualifier)) {
                return false;
            } else if (PsiUtil.isThisReference(qualifier)) {
                //instance 'this' already is processed. So it static 'this'
                return true;
            }
            return qualifier instanceof GrQualifiedReference && ResolveUtil.resolvesToClass(qualifier);
        }
        if (PsiUtil.isSuperReference(reference))
            return false;
    //this reference should be checked as all other refs
    }
    PsiElement run = place;
    while (run != null && run != targetClass) {
        if (targetClass == null && run instanceof PsiClass)
            return false;
        if (run instanceof GrClosableBlock)
            return false;
        if (run instanceof PsiModifierListOwner && ((PsiModifierListOwner) run).hasModifierProperty(PsiModifier.STATIC))
            return true;
        run = run.getParent();
    }
    return false;
}
Also used : GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrQualifiedReference(org.jetbrains.plugins.groovy.lang.psi.GrQualifiedReference) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Aggregations

GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)116 PsiElement (com.intellij.psi.PsiElement)32 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)31 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)26 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)26 Nullable (org.jetbrains.annotations.Nullable)23 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)23 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)18 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)17 GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)15 GrMethodCallExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression)15 GrNamedArgument (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument)14 GrMethodCall (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall)13 GrField (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField)12 ArrayList (java.util.ArrayList)11 NotNull (org.jetbrains.annotations.NotNull)10 GroovyRecursiveElementVisitor (org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor)10 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)10 GrOpenBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock)10 GroovyFile (org.jetbrains.plugins.groovy.lang.psi.GroovyFile)9