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;
}
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;
}
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;
}
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());
}
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);
}
Aggregations