Search in sources :

Example 16 with PerlValue

use of com.perl5.lang.perl.idea.codeInsight.typeInference.value.PerlValue in project Perl5-IDEA by Camelcade.

the class PerlNameSuggestionProvider method suggestAndGetForCall.

private static String suggestAndGetForCall(@NotNull PerlSubCallElement subCall, @NotNull Set<String> result, String recommendation) {
    PsiPerlMethod method = subCall.getMethod();
    if (method == null) {
        return recommendation;
    }
    PerlCallValue callValue = PerlCallValue.from(method);
    if (callValue == null) {
        return recommendation;
    }
    PerlValue subNameValue = callValue.getSubNameValue();
    if (subNameValue.canRepresentSubName("new")) {
        PerlValue namespaceNameValue = callValue.getNamespaceNameValue();
        Collection<String> variantsFromObjectClass = getVariantsFromPerlValueNamespaces(method, namespaceNameValue);
        result.addAll(variantsFromObjectClass);
        if (!variantsFromObjectClass.isEmpty()) {
            recommendation = variantsFromObjectClass.iterator().next();
        }
    } else {
        Ref<String> recommendationRef = Ref.create(recommendation);
        for (String subName : subNameValue.resolve(method).getSubNames()) {
            String normalizedName = join(subName.replaceAll("^(_+|get_*|set_*)", ""));
            if (StringUtil.isNotEmpty(normalizedName)) {
                result.add(normalizedName);
                recommendationRef.set(normalizedName);
            }
        }
        recommendation = recommendationRef.get();
        result.addAll(getVariantsFromEntityValueNamespaces(subCall));
    }
    return recommendation;
}
Also used : PerlValue(com.perl5.lang.perl.idea.codeInsight.typeInference.value.PerlValue) PerlCallValue(com.perl5.lang.perl.idea.codeInsight.typeInference.value.PerlCallValue)

Example 17 with PerlValue

use of com.perl5.lang.perl.idea.codeInsight.typeInference.value.PerlValue in project Perl5-IDEA by Camelcade.

the class PerlValuesManager method computeValue.

@NotNull
private static PerlValue computeValue(@NotNull PsiElement element) {
    if (element instanceof PerlVariableDeclarationExpr) {
        if (((PerlVariableDeclarationExpr) element).isParenthesized()) {
            return PerlArrayValue.builder().addPsiElements(Arrays.asList(element.getChildren())).build();
        }
        PsiElement[] children = element.getChildren();
        return children.length == 1 ? from(children[0]) : UNKNOWN_VALUE;
    }
    if (element instanceof PerlReturnExpr) {
        PsiPerlExpr expr = ((PerlReturnExpr) element).getReturnValueExpr();
        return expr == null ? UNDEF_VALUE : from(expr);
    } else if (element instanceof PerlValuableEntity) {
        return ((PerlValuableEntity) element).computePerlValue();
    }
    IElementType elementType = PsiUtilCore.getElementType(element);
    if (elementType == UNDEF_EXPR) {
        return UNDEF_VALUE;
    } else if (elementType == SCALAR_EXPR) {
        PsiElement[] children = element.getChildren();
        return children.length == 0 ? UNKNOWN_VALUE : PerlScalarContextValue.create(from(children[0]));
    } else if (elementType == TERNARY_EXPR) {
        PerlOneOfValue.Builder builder = PerlOneOfValue.builder();
        PsiElement[] children = element.getChildren();
        for (int i = 1; i < children.length; i++) {
            builder.addVariant(children[i]);
        }
        return builder.build();
    } else if (ONE_OF_VALUES.contains(elementType)) {
        return PerlOneOfValue.builder().addVariants(element.getChildren()).build();
    } else if (LIST_VALUES.contains(elementType)) {
        return PerlArrayValue.builder().addPsiElements(PerlArrayUtil.collectListElements(element)).build();
    } else if (elementType == ANON_ARRAY) {
        return PerlReferenceValue.create(PerlArrayValue.builder().addPsiElements(Arrays.asList(element.getChildren())).build());
    } else if (elementType == ANON_HASH) {
        return PerlReferenceValue.create(PerlMapValue.builder().addPsiElements(Arrays.asList(element.getChildren())).build());
    } else if (elementType == NUMBER_CONSTANT) {
        return PerlScalarValue.create(element.getText());
    } else if (element instanceof PsiPerlHashIndex) {
        PsiElement parent = element.getParent();
        if (parent instanceof PsiPerlDerefExpr) {
            return PerlHashElementValue.create(PerlHashDereferenceValue.create(from(((PsiPerlDerefExpr) parent).getPreviousElement(element))), from(((PsiPerlHashIndex) element).getExpr()));
        }
    } else if (element instanceof PsiPerlArrayIndex) {
        PsiElement parent = element.getParent();
        if (parent instanceof PsiPerlDerefExpr) {
            return PerlArrayElementValue.create(PerlArrayDereferenceValue.create(from(((PsiPerlDerefExpr) parent).getPreviousElement(element))), from(((PsiPerlArrayIndex) element).getExpr()));
        }
    } else if (element instanceof PsiPerlPrefixUnaryExpr) {
        PsiPerlExpr target = ((PsiPerlPrefixUnaryExpr) element).getExpr();
        IElementType firstChildElementType = PsiUtilCore.getElementType(element.getFirstChild());
        if (firstChildElementType == OPERATOR_MINUS) {
            return PerlArithmeticNegationValue.create(from(target));
        } else if (firstChildElementType == OPERATOR_PLUS) {
            return from(target);
        }
    } else if (element instanceof PsiPerlRefExpr) {
        return PerlReferenceValue.create(from(((PsiPerlRefExpr) element).getExpr()));
    } else if (element instanceof PsiPerlHashElement) {
        return PerlHashElementValue.create(from(((PsiPerlHashElement) element).getExpr()), from(((PsiPerlHashElement) element).getHashIndex().getExpr()));
    } else if (element instanceof PsiPerlArrayElement) {
        return PerlArrayElementValue.create(from(((PsiPerlArrayElement) element).getExpr()), from(((PsiPerlArrayElement) element).getArrayIndex().getExpr()));
    } else if (element instanceof PsiPerlHashSlice) {
        return PerlHashSliceValue.create(from(((PsiPerlHashSlice) element).getExpr()), listContext(from(((PsiPerlHashSlice) element).getHashIndex().getExpr())));
    } else if (element instanceof PsiPerlArraySlice) {
        return PerlArraySliceValue.create(from(((PsiPerlArraySlice) element).getExpr()), listContext(from(((PsiPerlArraySlice) element).getArrayIndex().getExpr())));
    } else if (element instanceof PsiPerlScalarCastExpr) {
        PsiPerlExpr expr = ((PsiPerlScalarCastExpr) element).getExpr();
        if (expr == null) {
            expr = PerlPsiUtil.getSingleBlockExpression(((PsiPerlScalarCastExpr) element).getBlock());
        }
        return PerlScalarDereferenceValue.create(from(expr));
    } else if (element instanceof PsiPerlArrayCastExpr) {
        PsiPerlExpr expr = ((PsiPerlArrayCastExpr) element).getExpr();
        if (expr == null) {
            expr = PerlPsiUtil.getSingleBlockExpression(((PsiPerlArrayCastExpr) element).getBlock());
        }
        return PerlArrayDereferenceValue.create(from(expr));
    } else if (element instanceof PsiPerlHashCastExpr) {
        PsiPerlExpr expr = ((PsiPerlHashCastExpr) element).getExpr();
        if (expr == null) {
            expr = PerlPsiUtil.getSingleBlockExpression(((PsiPerlHashCastExpr) element).getBlock());
        }
        return PerlHashDereferenceValue.create(from(expr));
    } else if (element instanceof PsiPerlBlessExpr) {
        PerlValue targetValue = from(((PsiPerlBlessExpr) element).getReferenceExpression());
        PsiElement blessExpression = ((PsiPerlBlessExpr) element).getBlessExpression();
        PerlValue blessValue = blessExpression != null ? from(blessExpression) : PerlPackageUtil.getContextType(element);
        return PerlBlessedValue.create(targetValue, blessValue);
    } else if (element instanceof PsiPerlArrayUnshiftExpr) {
        return PerlScalarContextValue.create(PerlUnshiftValue.create((PsiPerlArrayUnshiftExpr) element));
    } else if (element instanceof PsiPerlArrayPushExpr) {
        return PerlScalarContextValue.create(PerlPushValue.create((PsiPerlArrayPushExpr) element));
    } else if (element instanceof PsiPerlArrayShiftExpr) {
        return createShiftPopValue((PsiPerlArrayShiftExpr) element, FIRST_ELEMENT_INDEX_VALUE);
    } else if (element instanceof PsiPerlArrayPopExpr) {
        return createShiftPopValue((PsiPerlArrayPopExpr) element, LAST_ELEMENT_INDEX_VALUE);
    }
    return UNKNOWN_VALUE;
}
Also used : PerlValuableEntity(com.perl5.lang.perl.psi.properties.PerlValuableEntity) PsiElement(com.intellij.psi.PsiElement) IElementType(com.intellij.psi.tree.IElementType) NotNull(org.jetbrains.annotations.NotNull)

Example 18 with PerlValue

use of com.perl5.lang.perl.idea.codeInsight.typeInference.value.PerlValue in project Perl5-IDEA by Camelcade.

the class PerlValuesManager method createShiftPopArgumentValue.

/**
 * @return value of the argument for shift/pop operations before operation been performed
 */
@NotNull
public static PerlValue createShiftPopArgumentValue(@NotNull PerlShiftPopExpr shiftPopExpr) {
    PsiElement target = shiftPopExpr.getTarget();
    PerlValue targetValue;
    if (target instanceof PerlBuiltInVariable) {
        targetValue = PerlResolveUtil.inferVariableValue((PerlBuiltInVariable) target, shiftPopExpr);
    } else {
        targetValue = from(target);
    }
    return targetValue;
}
Also used : PerlBuiltInVariable(com.perl5.lang.perl.psi.impl.PerlBuiltInVariable) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 19 with PerlValue

use of com.perl5.lang.perl.idea.codeInsight.typeInference.value.PerlValue in project Perl5-IDEA by Camelcade.

the class PerlSubCallCompletionProvider method addCompletions.

@Override
public void addCompletions(@NotNull CompletionParameters parameters, @NotNull ProcessingContext context, @NotNull CompletionResultSet resultSet) {
    PsiElement position = parameters.getPosition();
    PsiElement method = position.getParent();
    assert method instanceof PsiPerlMethod;
    PerlCallValue perlValue = PerlCallValue.from(method);
    if (perlValue == null) {
        return;
    }
    PerlSimpleCompletionProcessor completionProcessor = new PerlSimpleCompletionProcessor(parameters, resultSet, position);
    PerlTimeLogger logger = PerlTimeLogger.create(LOG);
    if (perlValue instanceof PerlCallStaticValue && !((PerlCallStaticValue) perlValue).hasExplicitNamespace()) {
        PerlSubCompletionUtil.processBuiltInSubsLookupElements(completionProcessor);
        logger.debug("Processed built-in subs");
    }
    PerlSubCompletionUtil.processSubsCompletionsForCallValue(completionProcessor, perlValue, perlValue instanceof PerlCallStaticValue);
    logger.debug("Processed subs for call value");
    completionProcessor.logStatus(getClass());
}
Also used : PerlCallStaticValue(com.perl5.lang.perl.idea.codeInsight.typeInference.value.PerlCallStaticValue) PsiPerlMethod(com.perl5.lang.perl.psi.PsiPerlMethod) PerlCallValue(com.perl5.lang.perl.idea.codeInsight.typeInference.value.PerlCallValue) PerlTimeLogger(com.perl5.lang.perl.util.PerlTimeLogger) PerlSimpleCompletionProcessor(com.perl5.lang.perl.idea.completion.providers.processors.PerlSimpleCompletionProcessor) PsiElement(com.intellij.psi.PsiElement)

Example 20 with PerlValue

use of com.perl5.lang.perl.idea.codeInsight.typeInference.value.PerlValue in project Perl5-IDEA by Camelcade.

the class PerlSubReference method resolveInner.

@Override
@NotNull
protected ResolveResult[] resolveInner(boolean incompleteCode) {
    PsiElement element = getElement();
    assert element instanceof PerlSubNameElement;
    PsiElement parent = element.getParent();
    PerlCallValue perlValue = null;
    if (parent instanceof PerlMethod) {
        perlValue = PerlCallValue.from(parent);
    } else {
        var parentElementType = PsiUtilCore.getElementType(parent);
        if (MODIFIER_DECLARATIONS_TOKENSET.contains(parentElementType) || MooseTokenSets.MOOSE_STATEMENTS.contains(parentElementType)) {
            perlValue = PerlCallObjectValue.create(PerlPackageUtil.getContextNamespaceName(myElement), myElement.getText(), Collections.emptyList());
        }
    }
    if (perlValue == null) {
        return ResolveResult.EMPTY_ARRAY;
    }
    List<PsiElement> relatedItems = new ArrayList<>();
    perlValue.processCallTargets(element, relatedItems::add);
    return getResolveResults(relatedItems).toArray(ResolveResult.EMPTY_ARRAY);
}
Also used : PerlSubNameElement(com.perl5.lang.perl.psi.PerlSubNameElement) PerlMethod(com.perl5.lang.perl.psi.PerlMethod) PerlCallValue(com.perl5.lang.perl.idea.codeInsight.typeInference.value.PerlCallValue) ArrayList(java.util.ArrayList) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

NotNull (org.jetbrains.annotations.NotNull)13 PerlValue (com.perl5.lang.perl.idea.codeInsight.typeInference.value.PerlValue)11 PsiElement (com.intellij.psi.PsiElement)8 PerlCallValue (com.perl5.lang.perl.idea.codeInsight.typeInference.value.PerlCallValue)3 Instruction (com.intellij.codeInsight.controlflow.Instruction)2 IElementType (com.intellij.psi.tree.IElementType)2 PerlAssignInstruction (com.perl5.lang.perl.idea.codeInsight.controlFlow.PerlAssignInstruction)2 PerlMutationInstruction (com.perl5.lang.perl.idea.codeInsight.controlFlow.PerlMutationInstruction)2 PerlOneOfValue (com.perl5.lang.perl.idea.codeInsight.typeInference.value.PerlOneOfValue)2 PerlBuiltInVariable (com.perl5.lang.perl.psi.impl.PerlBuiltInVariable)2 PerlSubCallElement (com.perl5.lang.perl.psi.impl.PerlSubCallElement)2 PerlDelegatingLightNamedElement (com.perl5.lang.perl.psi.light.PerlDelegatingLightNamedElement)2 PerlLightMethodDefinitionElement (com.perl5.lang.perl.psi.light.PerlLightMethodDefinitionElement)2 PerlContextType (com.perl5.lang.perl.psi.utils.PerlContextType)2 PerlSubAnnotations (com.perl5.lang.perl.psi.utils.PerlSubAnnotations)2 PerlSubArgument (com.perl5.lang.perl.psi.utils.PerlSubArgument)2 ArrayList (java.util.ArrayList)2 Nullable (org.jetbrains.annotations.Nullable)2 VirtualFileWindow (com.intellij.injected.editor.VirtualFileWindow)1 Application (com.intellij.openapi.application.Application)1